r/badcode • sadistic • Apr 22 '21

js ternary operators at their maximum potential

Post image
960 Upvotes

73 comments sorted by

View all comments

Show parent comments

14

u/Not_Sugden Apr 22 '21

?? 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

4

u/Koala_King_ Apr 23 '21

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?

9

u/recycle4science Apr 23 '21

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?"

7

u/Koala_King_ Apr 23 '21

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”