Hacker News new | ask | show | jobs
by tinco 4136 days ago
If you only hate Javascript sometimes, you are definitely new to the language. That said even in a proper language like Ruby this particular bug can happen, that's why communities around these languages strongly encourage proper discipline, enforced by tools.

This bug most definitely should've made your tests go red, your linter give a warning and your code metrics suite should have a thing or two to about the huge expressions and 10+ line function they are in.

2 comments

Is a function longer than 10 lines considered a problem by JavaScript analysis tools (or their users)? Surely you jest?
No, by Ruby analysis tools like Rubocop. For my Javascript I use CoffeeScript, and I rarely go over 10 lines for a function. Javascript obviously is a bit weird in this because many Javascript programmers wrap whole prototypes in closures.

If your function is over 10 lines, it's probably bad, just look at your functions. Every 10+ line function I've ever written was only that long because of laziness and reducing them has always resulted in better legibility.

I remember Code Complete giving a different recommendation, googled and found this: http://stackoverflow.com/a/670039

>I recently wrote some code for Class::Sniff which would detect "long methods" and report them as a code smell. I even wrote a blog post about how I did this (quelle surprise, eh?). That's when Ben Tilly asked an embarrassingly obvious question: how do I know that long methods are a code smell?

>I threw out the usual justifications, but he wouldn't let up. He wanted information and he cited the excellent book Code Complete as a counter-argument. I got down my copy of this book and started reading "How Long Should A Routine Be" (page 175, second edition). The author, Steve McConnell, argues that routines should not be longer than 200 lines. Holy crud! That's waaaaaay to long. If a routine is longer than about 20 or 30 lines, I reckon it's time to break it up.

>Regrettably, McConnell has the cheek to cite six separate studies, all of which found that longer routines were not only not correlated with a greater defect rate, but were also often cheaper to develop and easier to comprehend. As a result, the latest version of Class::Sniff on github now documents that longer routines may not be a code smell after all. Ben was right. I was wrong.

Great. So I could have just left that 100-line function as it was and been spared that 30-minute bug-hunt for the missing "+".

Of course I wouldn't have written that blog-article then, and wouldn't have gotten onto the HN homepage... Oh, well... :)

That's not a function longer than ten lines. That's a single expression longer than ten lines.
Yes of course, but my parent post mentioned tools detecting and flagging a "10+ line function".
Thankfully the bug DID make my tests go red.