6 ms·
"Name your functions": Easier to know what the code does, flattens the "Christmas tree from hell", no need for ugly closures, + Makes it's easier to debug. Anon
by z3t4 11y ago
"Name your functions": Easier to know what the code does, flattens the "Christmas tree from hell", no need for ugly closures, + Makes it's easier to debug.
Anonymous functions is probably the most abused syntax in JavaScript.
- jtmarmon 11y agoeven more abused now w/ es6 arrow shorthand
- lewisl9029 11y agoI wish named arrow functions made it into the spec.
- gotchange 11y agoA workaround for this to get the best of the two worlds: let func = () => {};
- ehynds 11y agoThat seems to do it. let func = () => {}; console.log(func.name); // func and in stack traces: let func = () => foo(); func() results in: Uncaught ReferenceError: foo is not defined func @ test.js:1
- lewisl9029 11y agoWow. I had no idea that would work. All this time I've been using the full named function expressions over arrow functions just for the stack traces.
- ehynds 11y agoStill though, in situations where I care about the stack trace, I don't see myself writing: let doStuffCallback = () => {}; doStuff(doStuffCallback); over: doStuff(function doStuffCallback() { });
- spamizbad 11y agoDoes that only work with something like babel? Because using the node 5.1 repl (and chrome console) you get: > let func = () => {} undefined > func.name '' > function longfunc() {} undefined > longfunc.name 'longfunc'
- Scarbutt 11y agosadly, babel is taken for granted these days.
- lewisl9029 11y agoThe .name property doesn't seem to get set in Firefox either, but error stack traces do show the variable name as the function name. That's the important part IMO.
- gotchange 11y agoHave you tried it with a « var » instead as Chrome imposes certain restrictions on the new ES6 keywords? I also checked the online Babel REPL and verified that it works fine.
- spamizbad 11y agovar results in the same behavior in chrome (empty string for a function name) It looks like babel does this little nicety for stack-trace readability.
- gotchange 11y agoI think Babel converts the code to ES5 and then execute the statements and therefore the function's name is printed to the console just like what you expect from good old ES5 code.
- spamizbad 11y agoThe most direct ES5 translation of let func = () => {} would be var func = function() {} But even in that case, func.name is going to be "".
- merpnderp 11y agoWhich is the same as var func = function(){...}.bind(this); no es6 required.
- ehynds 11y agoOP is saying that `let func = () => {};` creates a named function where func.name equals "func" and "func" appears as part of the stack trace. Your example is still an anonymous function just bound to a different context.
- gotchange 11y agoand more verbose as well which was the concern of person to whom I replied.
- snappy173 11y agoon the other hand, es6 classes encourage named methods ...
- ameen 11y agoI've never understood JScripters' insistence on anonymous functions instead of named ones. As someone just delving into Node from another platform what're "proper best practices"-based JS patterns?
- aus_ 11y agohttp://callbackhell.com http://callbackhell.com is a good starting point.
- riebschlager 11y agoI was on board with that website until I couldn't find a single semicolon. Then my eyeballs started twitching.
- Killswitch 11y agoLove me some non-semicolon JavaScript.
- aswanson 11y agoYeah, I've always been suspicious of anon functions in any language, but jscripters take it beyond absurdity.
- aaronem 11y agoBest (professional) practice is to use named functions, with lambdas reserved for one-offs which are so genuinely trivial that promoting them to named functions would do more to clutter your code than to streamline it. It's telling that, while you do often see Javascript (not JScript, which is a Microsoft product and quite obsolete) developers pull some crazy nonsense with deeply nested lambdas, you almost never see Javascript developers do that in an environment that requires peer code review.
- ameen 11y agoI used JScripters as a short-hand. I'm well aware of the stigma surrounding JScript :P I've come across many JavaScript devs casually using and advising interns, etc to use anon functions to avoid cluttering code. It was almost preached as a design pattern. I was taken aback (as a back-end dev), but given the crazy stuff most JS devs did I just shrugged it is one of the platforms' quirks.
- nawitus 11y agoThe argument that stack traces are easier to debug when functions are named is always brought up when talking about the issue of naming functions. However, I don't quite understand why it's such a big issue. Whenever I get a stack trace the issue is almost always easy to debug. That has been my experience working with JavaScript full time for two and a half years. And every difficult to debug bug hasn't been related to knowing which functions are in the stack, but are instead related to figuring out why frameworks do something or race conditions and so on. I just find it surprising that people are having problems with anonymous functions in stack traces, when I've never seen that.
- jtchang 11y agoHow do you easily glance at a stack trace and know if it is your own code or a 3rd party library's code? Named functions definitely help a lot.
- Killswitch 11y agoBecause at a glance, it's easier to see the path of the file as an identifier than the name of the function. Function names are not unique throughout the codebase and dependencies. Lots of dependencies have the same named functions.
- AgentME 11y agoYeah I don't understand why "name your (short callback) functions" gets repeated as a truism so much. Do people have stacktraces that are missing line numbers?
- alessioalex 11y agoLine numbers don't help you with memory leaks, seeing the name of your function there does.
- vkjv 11y agoThis. They don't show up in heap dumps and sometimes CPU profiles.
- cookiecaper 11y agoSurely you jest! Impossible-to-parse trees of anonymous function is a crucial part of the JavaScript revolution. Signed: someone who occasionally has to maintain a massive CoffeeScript clusterfuck. If you want to be trendy that bad, buy a turtleneck. Don't use CoffeeScript.
- Osiris 11y agoI rarely look at function names. I almost exclusively rely on the stack trace's filenames and line numbers to find out where the code is that I need to look at.