5 ms·
"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 an
by hobozilla 11y 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.
- honest_joe 11y agoIt 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
- merb 11y agoWhy are Callbacks ugly? I mean that's one of the best things.
- honest_joe 11y agoWell both things are subjectively "ugly". Functionally they are awesome.
- merb 11y agoI 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.
- jbeja 11y agoThey are not ugly, nested callbacks are ugly, and they are not ideal to deal with asynchronous code.
- pweissbrod 11y agoAgreed. 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.
- MichaelGG 11y agoC#'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.
- ericcholis 11y agoIf || 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.
- zaf 11y ago> 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?
- mattmanser 11y agofruit = 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.
- tracker1 11y agofruit = 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.
- zaf 11y agoAnd what about the check for 0? Or does the empty string handle that case as well?
- deleted 11y ago[deleted]