News for hackers. Stuff that matters.
Re:Debugging Node.js in Production by z3t4 on Friday December 04, 2015 @10:25AM (#10676769)
"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.
Re:Debugging Node.js in Production by nawitus on Friday December 04, 2015 @01:36PM (#10678127)
The 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.

Re:Debugging Node.js in Production by AgentME on Friday December 04, 2015 @03:32PM (#10678931)
Yeah 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?
Re:Debugging Node.js in Production by alessioalex on Friday December 04, 2015 @04:17PM (#10679199)
Line numbers don't help you with memory leaks, seeing the name of your function there does.
Re:Debugging Node.js in Production by vkjv on Friday December 04, 2015 @05:54PM (#10679769)
This. They don't show up in heap dumps and sometimes CPU profiles.
Re:Debugging Node.js in Production by jtchang on Friday December 04, 2015 @01:38PM (#10678138)
How 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.
Re:Debugging Node.js in Production by Killswitch on Friday December 04, 2015 @04:16PM (#10679191)
Because 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.
Re:Debugging Node.js in Production by ameen on Friday December 04, 2015 @11:04AM (#10677053)
I'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?

Re:Debugging Node.js in Production by nawitus on Friday December 04, 2015 @01:42PM (#10678170)
Lambdas make code really easy to read compared to making the reader skip around the code. In addition, it reduces boilerplate code and requires less time to be spent on naming functions. Of course, it all depends on situation. Simple logic which is not beneficial elsewhere can be implemented as an anonymous function in my opinion.

For example: if(objects.find(object => object.id === id)) {}

Re:Debugging Node.js in Production by tragic on Friday December 04, 2015 @02:11PM (#10678364)
Also: anonymous function, file x, line y tends to be enough for me.
Re:Debugging Node.js in Production by coldtea on Friday December 04, 2015 @02:12PM (#10678373)
>Lambdas make code really easy to read compared to making the reader skip around the code.

You can have lambdas that are also named, e.g:

if(objects.find(function idCheck(object){ return object.id === id})

Not sure if it's possible with the new ES6 syntax [Edit: apparently it's not, although arrow functions will automatically be assigned a name in some common scenarios].

Re:Debugging Node.js in Production by Killswitch on Friday December 04, 2015 @04:18PM (#10679201)
That defeats the whole purpose of fat arrow functions.
Re:Debugging Node.js in Production by coldtea on Friday December 04, 2015 @06:32PM (#10679955)
I'm not sure what purpose you have in mind.

The main purposes of fat arrow functions is the auto-binding of this and succinctness.

A named fat-arrow function would still retain both, and in fact the feature was considered for ES6 back in 2013, but didn't made it. Syntax would have been like:

if(objects.find(functionName(object) => object.id === id)) {}

Re:Debugging Node.js in Production by vkjv on Friday December 04, 2015 @05:56PM (#10679781)
It's also important to note that these are not quite the same thing. If you name a function, it will be hoisted to the top of the scope where a possible naming conflict could occur.
Re:Debugging Node.js in Production by aus_ on Friday December 04, 2015 @11:17AM (#10677150)
http://callbackhell.com is a good starting point.
Re:Debugging Node.js in Production by riebschlager on Friday December 04, 2015 @03:11PM (#10678817)
I was on board with that website until I couldn't find a single semicolon. Then my eyeballs started twitching.
Re:Debugging Node.js in Production by Killswitch on Friday December 04, 2015 @04:19PM (#10679207)
Love me some non-semicolon JavaScript.
Re:Debugging Node.js in Production by aaronem on Friday December 04, 2015 @12:02PM (#10677452)
Best (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.

Re:Debugging Node.js in Production by ameen on Friday December 04, 2015 @12:05PM (#10677476)
I 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.

Re:Debugging Node.js in Production by aaronem on Friday December 04, 2015 @12:20PM (#10677580)
JScript isn't a shorthand for Javascript. It's a name for an ill-favored heterodox implementation of ECMAscript. Using it in the fashion you have here will therefore cause knowledgeable engineers to assume you must not know what you're talking about. Of course, you're welcome to continue doing so nonetheless.

Using lambdas in the fashion you describe is a dangerously poor practice that, over the long run, produces code that's much harder to maintain than it should be. That's why you don't see it in environments where changes have to pass peer review by experienced Javascript engineers before being accepted into the codebase. Unfortunately, such environments are far from prevalent and the JS dev world lacks rigor even by comparison with web development as a whole, so you see a lot of people abusing lambdas and encouraging others to do likewise. That doesn't make it any less a bad idea.

Re:Debugging Node.js in Production by ameen on Friday December 04, 2015 @12:33PM (#10677666)
I totally agree, and my use of JScript(er) has been the first in the last few years, the last was when I was teaching a bunch of school kids about Web technologies. I don't intend to use it again and only arose out of laziness than ignorance.

Are there any other such bad practices in the JS dev world? I've only just considered moving onto Node as I've grown disillusioned with Rails and the concept of using a single language across the entire app & mobile (React Native, etc) sounds enticing.

Re:Debugging Node.js in Production by aaronem on Saturday December 05, 2015 @06:38AM (#10681603)
Javascript is a fun language, but its laissez-faire nature and occasionally weird semantics lend themselves easily to the development of poor practices. Fortunately, there are several linting tools, such as jshint, which analyze code and complain about such things; I'd suggest taking a look at jshint's documentation, which is a veritable catalog of things it's easy to get wrong in JS and have come back later to bite you. I'd suggest even more strongly installing jshint and using it on your code; it's easy (just "npm install -g jshint" and you're off and running) and highly beneficial.

I'd also suggest that it's worth your while to take advantage of the JS dev community's excellent culture around automated testing; while that's a slightly more complicated topic, it is only slightly so, and I've found that a solid test suite, plus a coverage tool to ensure it doesn't leave anything out, is even better for improving and maintaining code quality than just a linter by itself -- jshint can catch stylistic and structural errors, but only unit tests will catch logic errors and regressions introduced by a new change. If you're (understandably) bewildered by the variety of testing tools available -- it seems like somebody comes out with a nifty new one every week or so -- then I'd suggest you can hardly go wrong with these:

* jshint, as already described

* Mocha, a flexible and reliable test framework

* Istanbul, a coverage report generator

* Grunt, a build tool whose plugin architecture simplifies integrating them

For an example of how they work together, you might take a look at https://github.com/aaron-em/same-encoder, where I use them all in support of a library providing functionality which I think is neat and probably no one else cares about. No doubt there are much better examples, but none come to mind quite so readily, and this codebase being as small as it is, it should be able to serve as a decent overview without being so big you'll get bogged down in it. In particular, /Gruntfile.js demonstrates how to tie all these disparate tools together for quick invocation with a single command like 'grunt validate', which I find makes it a lot more likely I will actually remember to run them; also take a look in /test/unit for an example of how Mocha tests are actually written.

Re:Debugging Node.js in Production by maxerickson on Friday December 04, 2015 @01:24PM (#10678042)
Perhaps they aren't clearly reasoning about first class functions and think that is the way it needs to be written.

Some complaints I see about Python's weak lambda have a similar undercurrent.

Re:Debugging Node.js in Production by mgr86 on Friday December 04, 2015 @12:19PM (#10677573)
this is my problem with Reactjs right now. I like it but debugging it is often difficult as hell. Thankfully my problems right now are small. With Hot module reloading I am constantly watching for signs of trouble. And can spot many quickly as they are introduced. But the stack trace is damn near foreign to me right now.
Re:Debugging Node.js in Production by novaleaf on Friday December 04, 2015 @12:52PM (#10677829)
Re:Debugging Node.js in Production by mgr86 on Friday December 04, 2015 @12:53PM (#10677836)
I am using those.
Re:Debugging Node.js in Production by novaleaf on Friday December 04, 2015 @01:22PM (#10678029)
i use that + sourcemaps, and it seems pretty easy to debug.

using Chrome + React Dev Extensions

setting up sourcemaps to work properly is quite a pain though, most the browserify tooling via gulp or grunt does a pretty poor job of it.

Re:Debugging Node.js in Production by cozuya on Friday December 04, 2015 @02:30PM (#10678501)
Actually gulp has many great ways to work with sourcemaps, browserify, and babel. Google it and you should find a gist. I'm using through2.
Re:Debugging Node.js in Production by novaleaf on Saturday December 05, 2015 @11:22AM (#10682300)
I'm using typescript, which may impact my success, but really I don't get proper sourcemappings if I use uglify/browserify, or even the sourcemaps plugin.

I only get proper sourcemaps if I use the tsify plugin + setting browserify debug=true. adding anything else causes sourcemaps to go out-of sync. That's OK though, as I get good sourcemaps in development and can live without it when in production.

Re:Debugging Node.js in Production by aswanson on Friday December 04, 2015 @11:50AM (#10677363)
Yeah, I've always been suspicious of anon functions in any language, but jscripters take it beyond absurdity.
Re:Debugging Node.js in Production by cookiecaper on Friday December 04, 2015 @05:08PM (#10679530)
Surely 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.

Re:Debugging Node.js in Production by Osiris on Friday December 04, 2015 @06:01PM (#10679797)
I 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.
Re:Debugging Node.js in Production by jtmarmon on Friday December 04, 2015 @10:45AM (#10676908)
even more abused now w/ es6 arrow shorthand
Re:Debugging Node.js in Production by lewisl9029 on Friday December 04, 2015 @11:17AM (#10677152)
I wish named arrow functions made it into the spec.
Re:Debugging Node.js in Production by gotchange on Friday December 04, 2015 @11:33AM (#10677269)
A workaround for this to get the best of the two worlds:

    let func = () => {};
Re:Debugging Node.js in Production by ehynds on Friday December 04, 2015 @11:40AM (#10677311)
That 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
Re:Debugging Node.js in Production by lewisl9029 on Friday December 04, 2015 @11:59AM (#10677428)
Wow. 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.

Re:Debugging Node.js in Production by ehynds on Friday December 04, 2015 @12:30PM (#10677650)
Still though, in situations where I care about the stack trace, I don't see myself writing:

  let doStuffCallback = () => {};
  doStuff(doStuffCallback);
over:

  doStuff(function doStuffCallback() {
  });
Re:Debugging Node.js in Production by spamizbad on Friday December 04, 2015 @12:26PM (#10677626)
Does 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'
Re:Debugging Node.js in Production by lewisl9029 on Friday December 04, 2015 @12:45PM (#10677778)
The .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.
Re:Debugging Node.js in Production by gotchange on Friday December 04, 2015 @01:37PM (#10678134)
Have 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.

Re:Debugging Node.js in Production by spamizbad on Friday December 04, 2015 @02:53PM (#10678673)
var 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.

Re:Debugging Node.js in Production by gotchange on Friday December 04, 2015 @03:14PM (#10678833)
I 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.
Re:Debugging Node.js in Production by spamizbad on Friday December 04, 2015 @03:46PM (#10679017)
The most direct ES5 translation of

    let func = () => {}
would be

  var func = function() {}
But even in that case, func.name is going to be "".
Re:Debugging Node.js in Production by gotchange on Friday December 04, 2015 @04:21PM (#10679221)

    var func = function func() {};
That's what the Babel REPL spits when you input the « let » statement.
Re:Debugging Node.js in Production by Scarbutt on Friday December 04, 2015 @12:35PM (#10677681)
sadly, babel is taken for granted these days.
Re:Debugging Node.js in Production by merpnderp on Friday December 04, 2015 @12:39PM (#10677720)
Which is the same as var func = function(){...}.bind(this); no es6 required.
Re:Debugging Node.js in Production by ehynds on Friday December 04, 2015 @01:03PM (#10677900)
OP 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.

Re:Debugging Node.js in Production by gotchange on Friday December 04, 2015 @01:33PM (#10678103)
and more verbose as well which was the concern of person to whom I replied.
Re:Debugging Node.js in Production by snappy173 on Friday December 04, 2015 @11:54AM (#10677402)
on the other hand, es6 classes encourage named methods ...