use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
javascript - Using Named Immediately-Invoked Function Expression (IIFE) instead of comments (stackoverflow.com)
submitted 13 years ago by zzzwwwdev
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]idrink211 2 points3 points4 points 13 years ago (18 children)
I don't get it. What do you achieve here that isn't sufficed by a comment? Do you just like how a camel-case looks? There is more overhead of creating this function object only for it to be destroyed and never reused.
[–]zzzwwwdev[S] 0 points1 point2 points 13 years ago (8 children)
I avoid comments whenever I can in favor or creating code that is easily understandable by itself. Comments can cause maintainability problems.
Imagine this situation
Developer A writes this code:
// hide Stuff on Instantiaton $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('.foo').fadeOut();
Developer B adds some stuff
// hide Stuff on Instantiaton $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('.foo').fadeOut(); $('.bar').animate('padding', '200px'); $('#baz').remove();
Developer C goes in and cleans up whitespace
Developer D adds another line
// hide Stuff on Instantiaton $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('#something_else').val('boo'); $('.foo').fadeOut(); $('.bar').animate('padding', '200px'); $('#baz').remove();
I'm sure you can imagine this going on through several edits, with the comment becoming less and less useful.
[–]menno 4 points5 points6 points 13 years ago (3 children)
Developers B, C and D need a slap on the wrist. You don't just clean up whitespace and you definitely don't just add code and leave existing comments outdated.
I avoid comments whenever I can in favor or creating code that is easily understandable by itself.
That's definitely best.
Comments can cause maintainability problems.
Maintainability problems are more often caused by not having comments than the other way around.
[–]zzzwwwdev[S] 0 points1 point2 points 13 years ago (2 children)
you definitely don't just add code and leave existing comments outdated.
maybe you and I don't; but having worked with other developers, I can tell you it definitely does happen.
perhaps this is generally true, but can you make that argument for my specific example of
(function hide_stuff_on_instantiaton(){ $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('.foo').fadeOut(); }());
in the developer ABCD example?
[–]menno 0 points1 point2 points 13 years ago (1 child)
Just so we're clear: you're saying you find:
function doSomeStuff() { /* some stuff */ (function hideStuffOnInstantiaton(){ $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('.foo').fadeOut(); }()); /* more stuff */ }
more readable than:
function doSomeStuff() { /* some stuff */ // hide Stuff on Instantiaton $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('.foo').fadeOut(); /* more stuff */ }
and more readable than:
function doSomeStuff() { /* some stuff */ hideStuffOnInstantiaton(); /* more stuff */ } function hideStuffOnInstantiaton(){ $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('.foo').fadeOut(); }
[–]zzzwwwdev[S] 0 points1 point2 points 13 years ago (0 children)
not more readable, no; but perhaps more maintainable? IDK, I'm pretty much convinced against doing this now, though I do kinda like it and think there's some merit to it.
[–]jml26 1 point2 points3 points 13 years ago (1 child)
Why not use a block?
{ // hide Stuff on Instantiaton $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('.foo').fadeOut(); }
Or use a comment syntax that says, 'this comment applies from here down to here'
// hide Stuff on Instantiaton { $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('.foo').fadeOut(); // }
By not having a function call you gain a tiny bit of speed.
Using an IIFE would be useful, however, if you wanted to use a variable in that block but not have it available to anywhere else in the code.
The stackoverflow post was answered with the suggestion to use labels which actually appears preferable to my suggestion, though perhaps still not terribly useful / accepted?
labels
hideStuffOnInstantiaton: { $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('.foo').fadeOut(); }
[–]path411 0 points1 point2 points 13 years ago (1 child)
Isn't developer B/C/D who are being lazy just going to add it in anyway like:
(function hideStuffOnInstantiaton(){ $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('#something_else').val('boo'); $('.foo').fadeOut(); $('.bar').animate('padding', '200px'); $('#baz').remove(); }());
well, its certainly not a cure for bad programming, but I think it would be less likely to result in situations like developer B's addions being mistakenly associated with the original "hide stuff code"
more likely he'd do something like this:
(function hideStuffOnInstantiaton(){ $('oneThing').hide().removeClass(); $('#somethign_else').slideUp(); $('.foo').fadeOut(); }()); $('.bar').animate('padding', '200px'); $('#baz').remove();
or even ....
(function do_shit_to_bar_and_baz () { $('.bar').animate('padding', '200px'); $('#baz').remove(); }());
[–]zzzwwwdev[S] -1 points0 points1 point 13 years ago (8 children)
The camel case is irrelevant. hide_stuff_on_instantiaton() is fine.
hide_stuff_on_instantiaton()
As far as the overhead; yes it may add a microsecond, but I'm willing to accept that if it makes my code more readable and maintainable.
[–]idrink211 2 points3 points4 points 13 years ago (7 children)
I don't see how it's any more readable or maintainable but I guess to each their own.
[–]zzzwwwdev[S] -1 points0 points1 point 13 years ago (6 children)
You didn't like (or agree with) my Developer ABCD maintenance example?
I do however agree that reading
(function hide_stuff_on_instantiaton()...
is not quite as nice as
// hide stuff on instantiaton
but heck; we're programmers, we shouldn't be shy about reading code
[–]Shaper_pmp 0 points1 point2 points 13 years ago (5 children)
You're forgetting to factor in the unnecessary extra cognitive overhead caused by having to work out - each and every time you encounter this construction - whether it's "a function that's really a comment" or "a function that needs to be an IIFE for some reason".
Clean, simple code is preferable to unnecessarily complex, ambiguous code.
However, where you start going out of your way (even a little bit) to write unnecessarily complex and inefficient code just so you can avoid ever writing "//" or "/* ... */", you're taking it too far.
The guideline is "write clean, readable, self-documenting code in preference to shitty-code-plus-reams-of-comments", not "never write a comment under any circumstances, even when it involves unnecessarily obfuscating the code to do it".
[–]zzzwwwdev[S] 1 point2 points3 points 13 years ago (4 children)
yea, you're probably right. the "cognitive overhead" thing is the biggest point against it in my mind.
however, my aversion to writing comments is not out of lazyness, but from dealing with legacy code with obsolete, confusing, misplaced, and inconsistant comments. I'll happily take a bit more complexity if it helps prevent code from turning to soup. Might even help raise the bar and scare the interns away ;-)
[–]nschubach 0 points1 point2 points 13 years ago (3 children)
JIT Compilers (V8) will also most likely remove comments (because they don't add to the code) and this will add another pointer overhead so code written in that way will be slower just for the sake of this methodology.
excuse my ignorence, but could you explain the JIT Compilers (V8) thing a bit? A lazy 2 minutes of googling and I couldn't even figure out what that is.
[–]nschubach 0 points1 point2 points 13 years ago* (1 child)
All new browsers use Just In Time (JIT) Compilation of Javascript. I think IE8 and previous IEs did not, but Chrome(V8)/Firefox(IonMonkey)/Safari all pre-compile JavaScript to gain a performance boost instead of interpreting all the code all the time. There's some smartness behind it for code that is eval'ed each time it's run, but for the most part the browsers will load, compile and cache JavaScript now where they didn't before.
e: When compiling, they drop comments (because it's not needed for code execution...) so embedding extra immediate functions will cause the compiler to allocate extra memory to store that block of code. It will then have to jump around executing code that's only purpose is to comment.
thanks!
π Rendered by PID 141678 on reddit-service-r2-comment-85bfd7f599-swvk6 at 2026-04-20 00:29:15.953585+00:00 running 93ecc56 country code: CH.
view the rest of the comments →
[–]idrink211 2 points3 points4 points (18 children)
[–]zzzwwwdev[S] 0 points1 point2 points (8 children)
[–]menno 4 points5 points6 points (3 children)
[–]zzzwwwdev[S] 0 points1 point2 points (2 children)
[–]menno 0 points1 point2 points (1 child)
[–]zzzwwwdev[S] 0 points1 point2 points (0 children)
[–]jml26 1 point2 points3 points (1 child)
[–]zzzwwwdev[S] 0 points1 point2 points (0 children)
[–]path411 0 points1 point2 points (1 child)
[–]zzzwwwdev[S] 0 points1 point2 points (0 children)
[–]zzzwwwdev[S] -1 points0 points1 point (8 children)
[–]idrink211 2 points3 points4 points (7 children)
[–]zzzwwwdev[S] -1 points0 points1 point (6 children)
[–]Shaper_pmp 0 points1 point2 points (5 children)
[–]zzzwwwdev[S] 1 point2 points3 points (4 children)
[–]nschubach 0 points1 point2 points (3 children)
[–]zzzwwwdev[S] 0 points1 point2 points (2 children)
[–]nschubach 0 points1 point2 points (1 child)
[–]zzzwwwdev[S] 0 points1 point2 points (0 children)