Dariusz Chowański
Dariusz Chowański

Reputation: 3269

Function arguments names JavaScript

I wonder why pepole use letters for names of arguments of function (i see that mostly in JS). For example that function:

function showMessage(e, t, i) {
    var n = e.find(".admin-message");
    n.empty().removeClass("error success").addClass(i ? "success" : "error");
    if ($.isArray(t)) {
        $.each(t, function(i,item) {
            n.append(item+"<br>");
        });
    } else {
        n.html(t + "</br>");
    }
    n.fadeIn("fast")
}

This is short example and it's easy to remember, but i see much longer fuctions with e.g. six arguments (a, b, c, d, e, f, g). Why people use letters, not for example camelCase name?

Upvotes: 0

Views: 134

Answers (2)

GPicazo
GPicazo

Reputation: 6676

Javascript is usually (or should be) concantenated, uglified and minified before being deployed to production.

Concantenated - All source files are merged into a single code file. Uglified - Variables, parameters and function names are rewritten so they are shorter and more difficult to read. Minified - All redundant space characters are removed.

All 3 actions above are performed to make the source smaller and thus faster to load (sometimes also paired with IoC). Uglification is also done for the purpose of 'encoding' proprietary algorithms, making it more difficult to reverse engineer.

What you posted is probably code that was uglified for the purpose of shrinking the size of the code file and was very likely not done manually, but rather as part of the build/distribution process.

Upvotes: 1

Francisco Vilchez
Francisco Vilchez

Reputation: 41

They minify the code, so it takes less time to load when the User enters to the website.

You can write a normal function and try to minify it with this simple online tool:

http://jscompress.com/

It will turn this:

function fact(num){
   if(num<0)
      return "Undefined";
   var fact=1;
   for(var i=num;i>1;i--)
     fact*=i;
    return fact;
}

Into this:

function fact(n){if(0>n)return"Undefined";for(var r=1,f=n;f>1;f--)r*=f;return r}

Upvotes: 3

Related Questions