Hacker News new | ask | show | jobs
by hobozilla 3994 days ago
"The OR adds mental overhead for whoever works on the function in the future"

What world is this guy living in? It's like it's the first time he's seen this and just decided "I don't like it, let me write an article about that".

Anyone working in JS for more than a month will have seen this as common practice. It much neater (and less mental headspace IMHO) than an extra if block. Particularly if you have more than one optional variable.

The only practical consideration raised is the falsy types can trip you up but that is easily negated. I have strong negative feelings for people who impose their own personal believes based on nothing but their feelings.

4 comments

Agreed. His statement is based on opinion, nothing objective. I could argue that in languages like C# the coalesce operator '??' has a similar meaning, so it is intuitive for people who know C# concepts.
C#'s coalese only works on null. The problem is that JS will apparently see an empty string or 0, and consider that null. So if your function passes those values deliberately, the || trick breaks.

So long you're OK with that, it seems like a clean solution. It's JavaScript. There's tons of dumb stuff a JS coder must learn. If he's confused by || then he's likely to be unable to work on any real JS.

If || is too ugly use the ternary operator, it's inline and only slightly more verbose. That being said, I think using || is a nice pattern for readability.
And what about the article's argument about passing in 0, which is falsey?
Just be careful. It's worth it at the end of the day.

Honestly, 0 sounds like a default value in most cases anyway.

> The only practical consideration raised is the falsy types can trip you up but that is easily negated.

You mean adding an if block to check for the zero and blank string?

    fruit = fruit === "" ? "" : fruit || "strawberry";
Woohoo! Javascript rocks! Note that

    fruit = fruit == "" ? "" : fruit || "strawberry";
May or may not be bugged depending on your understanding of a bug and whether you're a marmite fan.

    fruit = fruit === "" && "" || fruit || "strawberrry";
It's flexible too.. though imo your ternary operation is more obvious. ;-)

Personally, I find that once you understand JavaScript, the beauty in what you can do, especially for input validation (which is an ugly, ugly thing) works so well in this language.

And what about the check for 0? Or does the empty string handle that case as well?
It might be ugly but there are more uglier things in JS :P..callbacks and so on.

The way he wants to solve it however uglier imho

Why are Callbacks ugly? I mean that's one of the best things.
They are not ugly, nested callbacks are ugly, and they are not ideal to deal with asynchronous code.
Well both things are subjectively "ugly". Functionally they are awesome.
I think most people still like callbacks, cause of their functional behavior. Currently more and more people are loving some functional language designs. Especially things like map() which always has some kind of inner function in every functional or 'kind of' functional language.