Live data from Hacker News

Why I sometimes hate JavaScript

geekregator.com

41–50 of 66 posts

Re: Why I sometimes hate JavaScript

#41
post #39

It's way better to use [values].join('') here... It's probably faster and it coerces all values to string. If the first couple of results were numbers or booleans, they would be summed: > true + 10 + " result" 11 result > [true,10," result"].join('') true10 result

All the functions in that code return numbers.

Re: Why I sometimes hate JavaScript

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

Re: Why I sometimes hate JavaScript

#43
post #39

It's way better to use [values].join('') here... It's probably faster and it coerces all values to string. If the first couple of results were numbers or booleans, they would be summed: > true + 10 + " result" 11 result > [true,10," result"].join('') true10 result

[deleted]

Re: Why I sometimes hate JavaScript

#44
post #39

It's way better to use [values].join('') here... It's probably faster and it coerces all values to string. If the first couple of results were numbers or booleans, they would be summed: > true + 10 + " result" 11 result > [true,10," result"].join('') true10 result

All the functions in that code return numbers.

Ah ok. At first glance it looked like he was putting together something like an url.

Re: Why I sometimes hate JavaScript

#45
post #39

It's way better to use [values].join('') here... It's probably faster and it coerces all values to string. If the first couple of results were numbers or booleans, they would be summed: > true + 10 + " result" 11 result > [true,10," result"].join('') true10 result

All the functions in that code return numbers.

Making [`.reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...) a significantly better choice.

Re: Why I sometimes hate JavaScript

#46

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. 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... There was an article here a while back abo…

> Or use strict mode would not allow that. It was in strict mode. It allows that.

True, bad example. I was talking in general: the "yes, X has this problem, but if you tweak it to be X', then it doesn't." argument. You're not talking about X but X'.

But you're totally right, use strict is not syntactical. (I think?)

Re: Why I sometimes hate JavaScript

#47

Lots of comments here, and on the blog, essentially stating "It's not Javascript, it's that you forgot to [lint|test|format|.+]" There are tools to make up for these language deficiencies, but our attitude to those bitten by the language deficiencies it's truly unfortunate. Even Python, everyone's second favorite dead horse for dynamically typed language problems, would throw an error here.

Agreed. "It's not the language's fault; it's your fault for not using all the tools and best practices that make the language tolerable!" That, to me, sounds like the language has some deficiencies.

These two things are not exclusive. The language can be deficient, and you can be irresponsible not for using well-known, long-established tools for addressing those deficiencies.

Normally I'd toss in a jab about deficiencies in choosing your language, too, but Javascript gets a bit of a free pass here on the browser.

Re: Why I sometimes hate JavaScript

#48
My approach, in any language, would be to collect it in an array and sum (or concatenate) it at once.

    var values = [];
    values.push(phraseValue( result, queryData ));
    ...
    var value = values.reduce(function(prev, next) { return pre + next; });
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);

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.

Re: Why I sometimes hate JavaScript

#49
post #42

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…

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.

Post reply on HN