Using Named Immediately-Invoked Function Expression (IIFE) instead of comments
Asked Answered
K

2

11

What are the pros and cons of utilizing Named IIFEs within JS code to describe and group related code?

I've been using this "pattern" to lend structure to my more procedural code that gets executed only in one place.


Example

(function hideStuffOnInstantiaton(){
    $('oneThing').hide().removeClass();
    $('#somethign_else').slideUp();
    $('.foo').fadeOut();
}());

I find this preferable to both:

// hide Stuff on Instantiaton
$('oneThing').hide().removeClass();
$('#somethign_else').slideUp();
$('.foo').fadeOut();

since over time the comment may get separated from the code and its not immediately obvious what line(s) the comment applies to

and to:

function hideStuffOnInstantiaton(){
    $('oneThing').hide().removeClass();
    $('#somethign_else').slideUp();
    $('.foo').fadeOut();
};

hideStuffOnInstantiaton();

becuase why separate the function and its execution if its only executed in one place?


Are there any performance, maintainability, testability, or cross-browser considerations when using this pattern? I don't believe I've seen many people use this in the wild butI feel as though it could be very useful

Karrikarrie answered 20/3, 2013 at 17:17 Comment(8)
I would not name my immediately-invoked function. If I see a code like yours I would not expect it to be immediately invoked, because you can have (function a(){ .. });. I would perfer a comment instead.Hydracid
It might be useful for debugging, as the function name will show on the stack trace. Also, this is a compulsory reading on this topic: kangax.github.com/nfeMilliken
@Milliken - Thanks for the link I'll start digging in.Karrikarrie
@Hydracid - Never seen that before, I'm sure the above link will have the answer, but can you explain briefly why you would ever do that without invoking the function?Karrikarrie
Okey, I would never have code like I wrote - but you can. @Milliken have a good point with the stacktrace.Hydracid
Can we open the discussion up to why this question is not percieved as constructive? I feel like we already have a good, and useful answer brewing - "Its probably not a good idea because it may be confusing for X reason, but there is some benefit to having named IIFE since it shows up in the stack trace. Here is a good resource on this topic..." To me, that's a good fit for a SO Q&A; am I wrong?Karrikarrie
Zach - I've never seen this form (however I am familiar with IIFE's). I think it's great. Additional detail in a stacktrace is incredibly helpful. To support the argument that this is a constructive/useful question, maybe we should get into specifics like performance, maintainability, testability, compatibility (stacktrace support everywhere?), etc. The kangax link from @Milliken is incredibly valuable, thanks!Sorrento
@BishopZ where specifically? Also, why not link directly to coding.smashingmagazine.com/2012/11/05/… ?Sorrento
G
6

why separate the function and its execution if its only executed in one place?

In that case, there's no reason to use a function (with its overhead) at all - just inline the code. And don't hide comments in function names, I'd call that a bad practise. If someone separates the comment from the code, it's his fault not yours - actually he could pack completely unrelated stuff in your IENFE as well.

While this pattern could be useful if you'd reuse the function (recursively) or need it to build a closure around something, and the named function makes stacktraces easier to debug, there are various bugs in IE. So avoid it if you don't really need it - and you do not.

If you want to express that your comment applies to a block of code, you can explicitly use a block statement for that and put your comment at its head:

{ // hide Stuff on Instantiaton
    $('oneThing').hide().removeClass();
    $('#somethign_else').slideUp();
    $('.foo').fadeOut();
}

…although that'll likely confuse your readers as much as a superfluous IEFE.

Germanism answered 21/3, 2013 at 15:57 Comment(1)
That sucks, I love this pattern myself and use it often. Thanks for the info.Waterless
T
8

I always thought labels were cool:

hideStuffOnInstantiaton: {
  $('oneThing').hide().removeClass();
  $('#somethign_else').slideUp();
  $('.foo').fadeOut();
}

In reality, it's usually silly to do this. Instead, grouped functionality generally belongs in its own function.

Talaria answered 21/3, 2013 at 18:14 Comment(2)
wow, I sort of remember reading about labels a while ago. They are cool, and seem to make better sense than what I'm suggesting.Karrikarrie
Make sure you weigh the pros and cons of using 'cool' language features. On the pro side, it might be objectively better than a comment. On the other hand, rare language features can be confusing for less experienced developers.Ecg
G
6

why separate the function and its execution if its only executed in one place?

In that case, there's no reason to use a function (with its overhead) at all - just inline the code. And don't hide comments in function names, I'd call that a bad practise. If someone separates the comment from the code, it's his fault not yours - actually he could pack completely unrelated stuff in your IENFE as well.

While this pattern could be useful if you'd reuse the function (recursively) or need it to build a closure around something, and the named function makes stacktraces easier to debug, there are various bugs in IE. So avoid it if you don't really need it - and you do not.

If you want to express that your comment applies to a block of code, you can explicitly use a block statement for that and put your comment at its head:

{ // hide Stuff on Instantiaton
    $('oneThing').hide().removeClass();
    $('#somethign_else').slideUp();
    $('.foo').fadeOut();
}

…although that'll likely confuse your readers as much as a superfluous IEFE.

Germanism answered 21/3, 2013 at 15:57 Comment(1)
That sucks, I love this pattern myself and use it often. Thanks for the info.Waterless

© 2022 - 2024 — McMap. All rights reserved.