Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two-letter Variable Names in Javascript?

I was looking at an answer to an SO question today where the variable names are ua, rv, etc. And I thought, "Man, when will people learn to use full-size variable names, memory is not a problem any more" but then, it is Javascript so it has to come across the wire and perhaps long variable names even slow down interpretation.

Is using short variable names in Javascript premature optimization?

While I'm here, are there any libraries for Rails or PHP that will compress Javscript on the fly, so I can keep my Javascript with long names on the server?

like image 677
Dan Rosenstark Avatar asked Dec 04 '22 15:12

Dan Rosenstark


1 Answers

The only reason to use short variable names in JS is to save bytes over the wire. However, developing like that is ridiculous. Do they write JS without whitespace, too? There are tools which optimize finished JS. Crockford's is one of the most popular (though it does not shorten variable names). I can't recall offhand one that does obfuscate/shorten variable names, but they do exist and it's not that hard to write one, either. Google Closure is a very impressive JavaScript compiler that turns this:

var myFunction = function(arg1, arg2) {
    var foo = getValue(arg2);
    for(var count = 0; count < arg1.length; count++) {
        alert(foo);
    }
};

into this:

function a(b,c){var d=e(c);for(var f=0;f<b.length;f++){alert(d)}}
like image 67
Rex M Avatar answered Dec 19 '22 12:12

Rex M