Hacker News new | ask | show | jobs
by nkozyra 4123 days ago
The real issue here is it's not an error.

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.

Of course, jslint will complain about it (and a dozen other style things), so the more people utilize tools like that, the better.

More than anything, it's yet another reason to employ unit testing. Valid code that fails is very hard to debug without unit testing.

3 comments

This is like the

    if (x = true) {}
kind of error. Yeah it sucks, but it's not really the language's fault.

Not to mention that that large a block of addition is bad code smell to me.

Yes, it is the language's fault. The language could have been better designed, here are some solutions:

1. Assignments in conditions could be required to be surrounded with an additional pair of parentheses, like this: `if ((x = true)) {}`. GCC with warnings already requires this for C.

2. The assignment operator could be something other than the equal signs, for example, it could be `:=`. Assignment is so different from mathematical equality and beginners to programming trip up on this all the time, it's a shame programming languages copy each other for familiarity and keep this bad design.

3. Assignments in conditions could be banned out right, like Python does.

Sure, but detecting assignments inside conditionals is very easy and every compiler worth its salt gives warnings for them.

IMO, the really annoying language faults are the sort of thing that can't be fixed by a simple linter: object keys being converted to strings, the wonky pseudo-classy prototypal inheritance, dealing with libraries that abuse Function.toString or eval, etc.

>Yeah it sucks, but it's not really the language's fault.

Imagine a language were assignments were only done via := and comparisons with = or ==. A programmer would never encounter this error.

Ada does precisely this. (It also doesn't support assignment inside expressions at all.)

Modern Ada's a thoroughly nice language; I did a writeup: https://cowlark.com/2014-04-27-ada

First thing I did was paste this into an an editor, hit ctrl J, and see if jshint sprang up anything:

      12,40: Expected an assignment or function call and instead saw an expression.
Adding the missing + (and hence removing the expression which doesn't do anything) fixes it.
If everything is defined beforehand, jshint will show two warnings. The code is still valid.

The OP is talking about catastrophic errors as a means for surfacing problems in code. That's a pretty old-school (and I don't mean to pick on the OP, he mentions his background in the post) methodology for quality control in code. It worked a lot of the time in compiled languages because they're far more syntactically strict than languages like Javascript.

And that's why we need tools like jslint/jshint. Whether that's a good thing or not depends on your comfort with the language - having the flexibility to structure code differently can be seen as a positive, but you do open yourself up for these types of errors.

Having JSLint and a test framework in my toolchain when developing with JS is so essential...it's like trying to write Ruby without using unit tests.