2

What is the semantic difference between these two snippets of code?

  • Example 1

    var x = (function () {} ());

  • Example 2

    var x = (function () {} )();

The difference in writing is where we put our parenthesis pair () which tells the JavaScript interpreter to execute the code block at once. Technically, there is no difference. If I may rephrase the wording of my question and eliminate the word "semantic", then the new question has already been answered and elaborated upon here: https://stackoverflow.com/a/6645915/1268003

But, the semantic part is my dilemma and what I hope this thread could shine some light on. Due to it being "semantic", there can be no objective answer and my question is admittedly on the borderline of not being valid according to the FAQ of Stackoverflow. However, I can argue for the validity of this question which just might happen in the comments. For now, lets move on.

Defining the problem

The absolute vast majority of all JavaScripters out there seems to use example 2. Actually, I haven't yet found one exception to that rule (or where have I been lately?). jQuery plugin developers in particular, write code like so:

My bible, JavaScript: The Definitive Guide , doesn't use this practice at all - not once. Author David Flanagan sticks to example 1 (page 249 is the place where author comments this practice).

Never mind which construct, added parenthesis all have one and only one intention: To maximize readability. To clarify. Then, may I ask, clarify what?

My reasoning

When writing a closure in JavaScript, we're defining a local variable in a function that is not supposed to be accessible from code outside. For the most part and due to context, our function is an anonymous one that isn't bound to an identifier. Like so:

  • Example 4

    function () { var local = "Local variable!"; }

Again due to context and what is usually the practice, we want to execute that snippet of code at once. So we throw in a couple of parenthesis. This will tell the JavaScript interpreter to run the function:

  • Example 5

    function () {} ()

To clarify this in code, best practice is to add a couple of parenthesis, in a way this also clarifies the closure:

  • Example 6

    (function () {} ())

This is an exact replica of example 1. My reasoning thus gives that example 1is the way we "should" write code. Whenever I choose to use example 2, I only clarify the use of a closure, not the fact that our anonymous function is executed at once. But by the smallest of efforts to place our clarification-parenthesis on the outside of the entire code snippet, we can catch both.

Yes I can critique my reasoning. In example 3, I find it appealing to think "hey, outside reference is copied to an alias on the inside". The alternative:

  • Example 7

    (function ($) {} (jQuery))

..isn't as clear in this particular regard. However, example 2 doesn't use this pass-by-value technique. Personally, for the most part when I write closures, I don't have to pass anything in. And still, if we are allowed to be fundamentalist about it, the jQuery plugin practice never clarifies the immediate execution of our anonymous function.

Example 1 users

If my reasoning holds true, why is it then that the entire community out there use example 1? Is their sole intention to only cast one of two clarifications? Maybe just maybe, they don't even have a purpose but are following the lead of a mere tradition of which purpose we might not be able to explain? I haven't yet written a jQuery plugin, or used example 2 because I began my career reading the bible. But if you are among the many who use example 1 and have a reasoning for it, please share =)

Community
  • 1
  • 1
Martin Andersson
  • 18,072
  • 9
  • 87
  • 115

1 Answers1

1

Why? Readability

Many things are done in code because of the mantra of "Readability". It is invaluable in writing code. Your code should be easily inherited by another developer.

You forgot one

Or zero, as it were.

  • Example 0
    var x = function(){ }();
    

    And here is why people don't use that: it is hard to read. Usually these closures become very large, very quick. Someone shouldn't have to get to the very end of the closure to determine that it is just a variable named x with no self execution - such as var x = function(){}; To this end, developers wrapped their self executing closures with parenthesis: var selfExecuting = (function(){})();, or even plainly (function(){})(); so that from line 1 it was obvious that this was self executing. It also means that if you come across a function like this: var notExecuting = function(){}; you can generally assume at line 1 that it is not self executing.

    Readability is very important when coding. It is important for maintenance. It is important for consistency. It is important for documentation.

    Although, keep in mind, development code should not be production code.

  • Travis J
    • 81,153
    • 41
    • 202
    • 273
    • Love your answer, in particular example 0 =) I think you gave way for an important detail: `self executing closures`. It's the fundamentalist in me that want to extend the parenthesis to embrace everything and not just the closure. In the end, readability is what the reader makes of it. And due to the practice of the entire community, I think it would be safe to say that which alternative one chooses doesn't matter. I'm more concerned with the intent of the programmer, but as your answer gives example of, the intent is probably both. To convey a "self executing closure". – Martin Andersson Oct 05 '12 at 04:58
    • The intent is definitely the point :) Everything gets http://jscompress.com/ (minified) in the end anyway. Thus, in a minified script, there are no parenthesis. – Travis J Oct 05 '12 at 05:06
    • You wrote: `Usually these closures become very large, very quick. Someone shouldn't have to get to the very end of the closure to determine that [there is] no self execution [..].` I have two objections. **1)** Your anonymous function must not be anonymous. Give him an identifier with a semantic meaning even though we'll never use it. Thus we can increase readability without added parenthesis. Example: `function invocation {} ();` **2)** Added parenthesis might obscure what we're doing. For a long time when I was new, I thought the parenthesis in "some way" was what defined the closure. – Martin Andersson Oct 06 '12 at 01:54