The "JSlint catches this" argument is valid, but not to defend JS. When you say: "with a linter this doesn't happen," you're defending (Javascript + linter), not Javascript.<p>Just as when someone says: Typescript would not allow this, or that. Or use strict mode would not allow that. Yes! True. They would not. But they are not JS: they are Typescript, or JS strict mode, or...<p>There was an article here a while back about "tabooing your words"[0], and I think it applies very well here. Instead of talking about just "Javascript", let's try to not mention it. Say "Ecmascript 5 without any tooling," or "ES 6 with 6to5", or "Ecmascript 5 with a linter", or "Ecmascript 5 in strict mode with a linter."<p>Suddenly, you will find that everyone in this thread agrees: "Ecmascript 5 in non-strict mode, without special tooling" is a shitty language for humans to write because mistakes so easily go unnoticed. And humans make mistakes.<p>And if you do use linting, or whatever, no need to feel offended: we're not talking about that language!<p>In essence, saying "the linter would have caught this" is like saying "Typescript doesn't allow that". It's completely true, but it's a different language.<p>The author was not talking about "Javscript", but about "Ecmascript 5 in strict mode without tooling." And I think we can all agree: that language is problematic.<p>[0] <a href="https://news.ycombinator.com/item?id=6855568" rel="nofollow">https://news.ycombinator.com/item?id=6855568</a><p>EDIT: Author was talking about strict mode. Still problematic.
The real issue here is it's not an error.<p>In another language the compiler/parser may have thrown a warning, but the issue here is actually that this is valid Javascript. Poorly composed Javascript, but completely valid.<p>Of course, jslint will complain about it (and a dozen other style things), so the more people utilize tools like that, the better.<p>More than anything, it's yet another reason to employ unit testing. Valid code that fails is very hard to debug without unit testing.
Jshint catches this with 2 warnings: missing semicolon and expected assingment or function call.<p>In Sublime text 3, you can have inline warnings and as-you-type linting with the sublimelinter jshint plugin:<p><a href="https://github.com/SublimeLinter/SublimeLinter-jshint" rel="nofollow">https://github.com/SublimeLinter/SublimeLinter-jshint</a>
Good programmer, bad programmer. Good code, bad code. Let's not all pretend we don't have stupid bugs in our code regardless of editor, linting or language.<p>JS is a mediocre language. It is popular because it is ubiquitous, not because it is good. It really does have bad error handling, weird optional rules, problems determining type when adding/concatenating with +, and the list goes on.<p>His was a syntax error that could have been caught with linting, sure. But there is plenty of well-formed JS that passes linting but still behaves badly due to the language itself.
Lots of comments here, and on the blog, essentially stating "It's not Javascript, it's that you forgot to [lint|test|format|.+]"<p>There are tools to make up for these language deficiencies, but our attitude to those bitten by the language deficiencies it's truly unfortunate.<p>Even Python, everyone's second favorite dead horse for dynamically typed language problems, would throw an error here.
Optional semicolons is my biggest hate with Javascript --- combined with expression statements silently discarding their result, it makes this kind of mistake far too easy to make.<p>Personally I've never understood with Javascript strict mode doesn't make semicolons mandatory.
I think this is a general problem for people used to the comfort of compiled languages. I also struggle with languages like JavaScript. I always have the feeling that I'm actually doing something wrong or non-standard but the webbrowser is smart enough to understand it anyway.<p>This feels to me like I'm constantly operating in some kind of gray area where everything is "correct enough". But as others have already pointed out, this can be ameliorated by using the right tools. So the problem is actually purely about taste, I guess.
Honestly, there should have been a loop there. The author introduced un-necessary complexity and a bug appeared. Not that Javascript is a great language.
Sure, this is annoying, but the reality is that this type of stuff is very easily avoided through the use of linters. IMO, you should never work on a dynamic scripted language like JavaScript without constantly running a linter in the background to pick up stuff like this.
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.<p>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.
I really don't see why this isn't a syntax error<p>What is actually being done with the calls after the chain of +'s?<p>Edit: Something to do with semicolons being optional in js?
It's way better to use [values].join('') here... It's probably faster and it coerces all values to string.<p>If the first couple of results were numbers or booleans, they would be summed:<p><pre><code> > true + 10 + " result"
11 result
> [true,10," result"].join('')
true10 result</code></pre>
My approach, in any language, would be to collect it in an array and sum (or concatenate) it at once.<p><pre><code> var values = [];
values.push(phraseValue( result, queryData ));
...
var value = values.reduce(function(prev, next) { return pre + next; });
</code></pre>
I understand that COLLECTION.reduce may not be in all javascript implementations (or even in all languages), but that would be my first thought on how to handle that mess. Id probably even make it a function addAllOfTheseValues(result, queryData);<p>I also dislike direct string concatenation, I always try to use print type functions because it is easier to separate variables from text and spot potential errors. I'm happy that JS is getting this.
I had a similar-ish problem just last night. Spent over an hour crawling through some code trying to figure out why it wasn't working like it was supposed to. I ended up doing the divide-and-conquer approach with "console debugging". I was cursing wildly by the end of it.<p>All to find in the end that I had used ( ) instead of [ ] (or maybe it was the other way 'round?) in a thick spot of code, and Javascript was happy to try to do that without throwing any kind of error.<p>That sort of thing happens in all languages to some extent, but debugging in Javascript is unmitigated hell.
Doesn't JS have a join function?<p>I realize that problems like this are the reason that my coding is going in a more functional direction (I would still say I write imperative code), but I avoid "doing things manually" like this, as its easy to overlook such errors.
You'd spot that right away if you had a visual jshint/jslint plugin in your IDE. It's a real time saver to have one: there are dozens of cases like your example that are valid JS, although it's not what you intended to write.
What Node.js taught me is that JavaScript is language like any other. It has good parts, it has quirks and you have to know what are its strenghts and what are its weaknesses to use it properly. And you can say that about any other language.