Why is it necessary that (0) and if (1)

Take a look at this part for code in ArcGIS 3.0 for javascript. https://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.0

Inside there are "if (0)" and "if (1)", why is this needed? Is (0) always false and if (1) is always true?

+4
source share
3 answers

Dojo assembly tools - this is what it does (according to the given assembly parameters), but not for obfuscation. If you look at the non-built dojo.js and the corresponding built-in dojo.js.uncompressed.js files, you will see that the build tool replaces calls ("somefeature") with verified true / false tests. As already noted, this may not create unreachable code. Why do this? Since then an intelligent optimizing compiler (e.g. Google Closure) can cut all this dead code, the result is a smaller file (sometimes MUCH less ... that the point).

Conceptually, it looks something like this:

  • Non-string code has a “kitchen sink” with dynamically evaluated has () calls.
  • You customize the assembly profile with options that indicate what you do / do not want in your custom assembly.
  • The build process replaces dynamic () calls [for the corresponding build options] with validated true / false tests (or the best way to verify that these are I / O tests).
  • The Closure compiler removes the "out" code during minimization.

Check out the current Dojo Build System documentation and http://jamesthom.as/blog/2012/08/03/finding-nano/ for more information. Also, here is a good low-level / code description of this process.

PS "if (0) / if (1)" is not really obfuscation ... sort of like the opposite. If someone wanted to embarrass, they would most likely have “if (a) ... if (b) ... if (c) ...” with the wars set far, far away. However, minifiers creates more confusing code than the one in and of itself. Check the dojo.js source before and after launching it through Closure; the final product is not much like the original.

+3
source

Yes, 0 always false, and 1 always true.

However, as you can see in the code, the company considers this to be its 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. 

Usually obfuscating such code (i.e. makes it difficult to read). One way is to insert useless statements like the if(1) and if(0) that you saw.

Read more about Obfuscation here .

Another possible explanation is that these if are used instead of real logic that has not yet been implemented, as mentioned in @mvbl.

+2
source

This can be used instead of the real if () operator, for which the actual logic has not yet been implemented. And, as @houbysoft mentioned, they are interpreted as logical false and true. Thus, for average time, they use false or true to ensure that statements inside are always executed (or not) and intend to add actual checks later.

+1
source

All Articles