Live data from Hacker News

Why I sometimes hate JavaScript

geekregator.com

21–30 of 66 posts

Re: Why I sometimes hate JavaScript

#21
post #16

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.…

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.

Re: Why I sometimes hate JavaScript

#22
post #16

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.…

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.

Re: Why I sometimes hate JavaScript

#23
post #22
post #16

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.…

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.

Re: Why I sometimes hate JavaScript

#24
post #20

I really don't see why this isn't a syntax error What is actually being done with the calls after the chain of +'s? Edit: Something to do with semicolons being optional in js?

This is valid JS:

    1;
    2;
    alert("returns undefined");
    confirm("return true or false?")
    (function() {return 3})();
    (function() {})();
What's being done with the values of 1, 2, undefined, true/false, 3 and undefined? Nothing if they're not assigned. But as every function returns a value and invocations are therefore an expression, it would be impossible to disallow expressions to be evaluated but not assigned.

Re: Why I sometimes hate JavaScript

#26
post #21
post #16

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.…

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.

Re: Why I sometimes hate JavaScript

#27
post #20

I really don't see why this isn't a syntax error What is actually being done with the calls after the chain of +'s? Edit: Something to do with semicolons being optional in js?

Javascript has "semicolon insertion".

It tries to parse a newline with no semicolon as continuing the current expression, if it can't, it inserts a semicolon.

It's a 'feature' that's caused too many bugs and is a warning in most linters.

Re: Why I sometimes hate JavaScript

#29
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.

Personally I've never understood with Javascript strict mode doesn't make semicolons mandatory.

Post reply on HN