Reputation: 4945
Look at this piece for code in ArcGIS 3.0 for javascript. https://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.0
Inside there is "if(0)" and "if(1)", why is there a need for this? Isn't if(0) always false and if(1) always true?
Upvotes: 5
Views: 5957
Reputation: 41
The Dojo build tools are what does that (under given build options), but not for obfuscation. If you look at non-built dojo.js and corresponding built dojo.js.uncompressed.js files, you can see that the build tool is replacing has("somefeature") calls with hardwired true/false tests. As noticed, this can and does create unreachable code. Why do this? Because then a smart optimizing compiler (e.g. Google Closure) can prune all that dead code out, resulting in a smaller file (sometimes MUCH smaller...that's the point).
Conceptually, it goes something like this:
Check out current "Dojo Build System" documentation and http://jamesthom.as/blog/2012/08/03/finding-nano/ for more info. Also, here's a good low/code-level description of this process.
P.S. "if(0)/if(1)" isn't really obfuscation...kinda the opposite. If someone wanted to confuse, they'd more likely have "if(a)...if(b)...if(c)..." with vars set far, far away. However, minifiers produce more obfuscated code than that on their own. Check out dojo.js source before and after it's been run through Closure; the end product bears little resemblance to original.
Upvotes: 4
Reputation: 33412
Yes, 0
is always false and 1
is always true.
However as you can see in the code, the company considers it their trade secret:
COPYRIGHT 2009 ESRI
TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
Unpublished material - all rights reserved under the
Copyright Laws of the United States and applicable international
laws, treaties, and conventions.
It is common to obfuscate such code (i.e. making it harder to read). One of the ways is inserting useless statements like the if(1)
and if(0)
you have seen.
You can read more about Obfuscation here.
Another possible explanation is that these if
statements are used in place of real logic that has yet to be implemented, as @mvbl fst mentioned.
Upvotes: 2
Reputation: 5263
This may be used in place of real if() statement for which actual logic has not been implemented yet. And as @houbysoft mentioned, they are interpreted as boolean false and true. So for the mean time they use false or true to make sure statements inside always execute (or not) and intend to add actual checks later.
Upvotes: 1