Hacker News new | ask | show | jobs
by thebouv 4123 days ago
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.

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.

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.

3 comments

Specifically the mistake Javascript is making here is one designed into the language itself:

When there are no semicolons between statements, it will automatically and silently insert them. Without this behavior his mistake would've been a compile-time error and easily caught. (Of course, this leads to the bigger bug that sometimes Javascript will insert them in the wrong place.)

He doesn't know exactly what the bug in Javascript is and how to point it out accurately, but he is absolutely correct that there is a bug in Javascript's design.

As someone pointed out, the implied semicolon isn't the problem.

The problem is that the code is written in a way that makes it vulnerable to an implied semicolon.

The core syntax of JavaScript won't be changing any time soon, so it's important to teach beginners how to write code that minimises mistakes like these.

I haven't seen many books and tutorials attempt this. So it's unfair to blame beginners when there's so little encouragement to learn a strong defensive coding style.

jshint is nice for basic sanity checks, but it can't deal with bigger structural or stylistic mistakes. You can only fix those by learning from strong examples of good, clean code.

I think this applies to all languages. Being able to solve a problem and write a solution in code symbols isn't enough. I wonder if it would help everyone if there was a store of established best practices with examples for every language.

You know that kind of bullying where someone grabs someone else's arm and uses it to hit them in the head with their own hand, while yelling "Stop hitting yourself! Why are you hitting yourself?"?

Javascript's automagic semicolon is that, and you're the kind of person who condones it.

Yes, at the end of the day one has to learn how to deal with the automagic semicolon, if one doesn't just give up on Javascript wholesale and finds a way to write better languages. However the very presence and behavior of that code in every javascript interpreter is still a cruel misbehavior whose implementation was a mistake. It is just as cruel and as much of a mistake to imply even in the slightest way that it isn't.

The correct response is to not blame the coder who stumbles over that, but to gently let them know that yes, Javascript is a bad language, and to gracefully show them how to not fall into that trap.

Actually that's what I said.

I wasn't blaming the coder or the language.

I was saying that it would help everyone if there was top quality online help explaining how to code around language deficiencies - the kind of deficiencies that everyone has to deal with it, because no language is perfect, and most languages are quite a lot less than perfect in at least some respects.

Be realistic. js isn't suddenly going to be rewritten with a proper semicolon patch. It's too established, and it would break too much code.

So complaining about it is a waste of everyone's time. It's like complaining about the fact that sometimes it gets cold in the winter.

It's more productive to do something practical about producing quality code that minimise possible problems, surely?

I want to make sure that you understand why i wrote that post. I saw you mentioning that the docs need to be better. I actually KNOW the problem, since we have it in Perl too, and i made a site for it. ( perl-tutorial.org )

However, your first two lines were still:

> the implied semicolon isn't the problem.

> The problem is [...] the code [...]

Which is simply not an acceptable thing to say. The first thing to do is admitting that the language is the problem, not the coders or their code. The second thing is, now they're listening instead of being disgusted at you, to tell them solutions.

>JS is a mediocre language. It is popular because it is ubiquitous, not because it is good.

JS is a popular because runs on browser by default.

Yes, which is why it is so ubiquitous.