0

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?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Dariusz Chowański
  • 3,129
  • 2
  • 14
  • 18
  • 2
    This looks like minified code, it wasn't written by a person. – elclanrs Jun 30 '15 at 21:43
  • …though a machine would have properly indented the code. – Bergi Jun 30 '15 at 21:45
  • well, coders are lazy, and one letter is all that's required. aside: they're "parameters", not "arguments". – dandavis Jun 30 '15 at 21:45
  • This is just poorly written code. Variable names should be meaningful to humans, and there's no reason for parameters to be different. Exceptions can be made for common, simple parameters like `e` for `event`, `i` for `index`, etc. – Barmar Jun 30 '15 at 21:52
  • @Bergi ... though minified code would be all on one line. – Barmar Jun 30 '15 at 21:53

2 Answers2

3

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}
1

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.

GPicazo
  • 6,516
  • 3
  • 21
  • 24