r/badcode • sadistic • Apr 22 '21

js ternary operators at their maximum potential

Post image
960 Upvotes

73 comments sorted by

View all comments

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.

10

u/Major_Fudgemuffin Apr 23 '21 edited Apr 23 '21

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.

Edit: added simpler name

-20

u/TigreDeLosLlanos Apr 23 '21

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.

14

u/[deleted] Apr 23 '21

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.

7

u/Major_Fudgemuffin Apr 23 '21

Same. And as long as you and your team are on the same terms, who cares what you call it?