I am fairly new to node but find that it is hard as heck to debug. node debug index.js seems really weird and not at all like python's pdb. Is there something I am missing?
Trying to figure out some websocket stuff, I somehow managed to get nodejs, autobahn, and whatnot running locally (client and server) using Jetbrains' WebStorm on a Mac.
This tool stack makes me want to knock my teeth out.
That WebStorm's debugger (with break points!) works at all is a marvel. Props to the JetBrains team.
Yeah, there are better debuggers out there for node. Hopefully others will come along and make a list, but there's a number of graphical ones you can find with a little searching. Even visual studio has step through debugging at this point.
"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.
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.
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?
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.
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)) {}
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].
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:
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
It's strange, nothing in the hosts file, any ping says it can't find the host http://techblog.netflix.com/, or even anyone of those check ip sites says the same. Asked some work colleagues here and in Ireland and it seems our company is blocking it, I guess through the firewall which unfortunately I can't disable.
My early impressions of New Relic were that they were a really fast and easy way to correlate all the default logging of the web application stack. I imagine Netflix would want something a little deeper and more customized then this, also deploying New Relic at scale would be costly and at the very least require an audit of potential performance implications.
On the contrary: there are many bugs you will not find until software is in production. (That's not a reason not to test or roll out to staging environments first and the like, because you can continue to find new bugs that way. But despite all of our efforts, there remain bugs that only manifest for the first time in production.)