17

I was looking at the source code to qTip 2 and saw the following:

// Munge the primitives - Paul Irish tip
var TRUE = true,
   FALSE = false,
    NULL = null;

I can't come up with a reason you should ever do this, and have a strong feeling that it would just encourage bad coding habits. Say a developer makes a typo in a Yoda condition like if (TRUE = someCondition()), then TRUE could very well end up actually meaning false, or you might end up assigning someObject to NULL.

I guess I'm just wondering if there's some redeeming quality for this practice that I'm missing, or if this is just a plain old Bad Idea™

skaffman
  • 398,947
  • 96
  • 818
  • 769
Tristan
  • 2,617
  • 3
  • 15
  • 9

3 Answers3

16

The goal of this is just to improve compression, Paul Irish himself calls it as an "Anti-Pattern".

He describes it as "Good for compression and scope chain traversal" on the following presentation:

On scope chain traversal, we won't see any improvement on literals as null, false, true, since the scope chain is not inspected, they are just literals.

On other identifiers as undefined or windows the scope chain traversal is indeed inspected.

Paul Irish Anti-patterns slide 55

roberkules
  • 6,557
  • 2
  • 44
  • 52
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 1
    I wonder if it's worth it at all if you gzip. I seem to recall cases where the obfuscated result was indeed smaller, but the gzipped result was larger than the equivalent gzip without the munging. –  Jan 05 '12 at 17:25
  • 1
    @missingno - qTip got the idea from this slide. They put it write there in the comment (see the first line in the OP's code). – Joseph Silber Jan 05 '12 at 17:29
  • @amnotiam: that's a great question, I'd be interested in seeing some real world comparisons – Tristan Jan 05 '12 at 18:38
  • @cms, may I request to have a look at a jquery question on a different topic here : http://stackoverflow.com/questions/13137404/jquery-find-div-class-name-at-a-certain-position-while-scrolling ? – Istiaque Ahmed Oct 31 '12 at 07:06
4

You could do this for the sake of code compression. For example, YUI Compressor is not going to touch true and false, but it could replace all occurrences of, for example, TRUE with A, saving four characters per occurrence. For example, before compression:

    if (foo === null) {
        bar = true;
    }

After compression, assuming the compressor replaces TRUE with a and NULL with c:

if(foo===c){bar=a;}

Versus this, after compression with no "munging of primitives":

if(foo===null){bar=true;}

The bad-coding-habits danger that you quite correctly cite in your question may outweigh the small savings in additional compression. It depends on how desperate you are to save a few dozen or perhaps a few hundred bytes.

Personally, I would (almost) never do this. Too dangerous.

dgvid
  • 26,293
  • 5
  • 40
  • 57
2

I believe this is recommended for compression.

These shortcut variables will be compressed when munged, resulting in smaller files. However, your noted drawbacks are most certainly valid points!

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326