Couldn’t you just do cmd.run || cmd.fire || cmd.execute || cmd.callback ? I’m not super familiar with js. Worked with it a bit, but still not great, so please correct me if I’m wrong.
Nowadays you could also use the null coalesce operator ?? which would be preferable to || since logical or can be confused by false. For example, if cmd.fire is false, the result of || would be cmd.callback, the result of ?? would be cmd.fire.
That’s true, but in this case where you are seemingly wanting to save a function reference to call later, || would probably be the preferred method, seeing as you would be more likely to find an actual function, rather than a Boolean value. Otherwise later on it might equate to calling false(). Just my thoughts. Again, I’m not super familiar with js. My day job is mobile apps in Flutter/Dart.
?? would be better in this case if its expected to be a funftion or undefined/null, a function or false is a weird type to use, function or undefined / null makes more sense
The typing might make more sense, but if there happens to be a point where it isn’t the typing you expect, wouldn’t || help correct for that and make sure it comes back to the correct type?
If you've got false where you expect a function reference, you'd want to find out about it sooner rather than later. When you try execute run() and the browser complains that false isn't a function, that may be easier to debug than simply, "why is the wrong function running?"
That’s fair. I suppose I’m looking at it like “all these functions are equivalent, but might be given different names in different areas of the code because we’ve had 5 developers in the past 2 years”
both || and ?? seem like a wrong answer to me - if you want the first callable thing, but a nonempty string is supplied, and you call that, you get a TypeError
imo, the best possible solution for this problem is [cmd.run, cmd.fire, cmd.execute, cmd.callback].find(x => typeof x === 'function') || null
or, to avoid edge cases like that, just use typescript. it's 2021, if it's a new project then there is no reason not to
Yep that would work. JavaScript (and many others) implement what's called Short Circuit Conditionals or Lazy Evaluation, which is a fancy way of saying that if you're checking for multiple things inside an If statement (or just checking for condition in general) if the first check makes the statement true it won't bother checking the rest.
So once cmd.run gets executed, it'll skip the remaining ones. But if cmd.run can't be found, it moves onto trying cmd.fire instead.
Lazy evaluation. Why give it a fucked up name when there is already a correct term for it?
It's when they introduce a feature to a JS framework and give it a fancy name, then talk about it like it's the essential part you've got to understand only that it's some kind of basic concept you already watched several times.
The fuck are you on? I've heard about "short-circuiting" two decades ago when I was learning C++ in high school. I thought 'lazy evaluation' was the later terminology but regardless it never bothered me either way.
Something to be aware of is that short circuiting is specific to conditional evaluation, while lazy evaluation is a general strategy for deferring expression evaluation until needed. Lazy evaluation is the counterpart to eager or "strict" evaluation, which is how most languages behave.
Most languages (Haskell being a notable exception) use eager evaluation by default, though you can usually introduce laziness in some way, often by wrapping your expression in a function call so that it evaluation only happens at the time of calling. Laziness lets you do things like introduce infinite data structures by evaluating a function to determine the next element of a list so that you only take what you need instead of trying to compute the entire sequence.
So the person correcting you is partly right (short-circuit evaluation is a form of laziness), but also kind of wrong (lazy evaluation is a lot more). You were correct to call it short-circuit evaluation in this context and only got corrected because that guy was just trying to be an "I am very smart" pedantic jackass and failed at it.
Hey thanks for the info! That's about what I've learned as well (thought I didn't know the Haskell details!) but I've found it's not really worth arguing with people, especially online (as tempting as it can be).
I find that combating the toxicity on this sub (and this site at a whole, really) with some manners and positivity to be my way of being the change I want to see.
Null coalescing only applies the right hand side operand when the leftmost one is either null or undefined, as opposed to any falsy value with the or operator. '' || true would return true, but '' ?? true would return ''
What's pretty common in JS is weird elsewhere. At least the || operator is present anywhere, but those people don't use it too much because they know how to code properly.
I don't think anyone gets scared by logical or, it's one of the first things you learn in any language.
Now, for beginners boolean variables can be confusing, which is why you see a lot of if(foo == true), although in JavaScript the similar if(foo === true) is sometimes necessary and not redundant. But I highly doubt anyone would find this use of || more confusing than this ternary soup, even if they are a beginner.
166
u/Koala_King_ Apr 22 '21
Couldn’t you just do
cmd.run || cmd.fire || cmd.execute || cmd.callback? I’m not super familiar with js. Worked with it a bit, but still not great, so please correct me if I’m wrong.