I don’t often talk about programming (but when I do, I get attacked by random nerds online [insert Dos Equis Man meme here]). Since my job as a software engineer does involve a fair bit of it, though, I have, over the years, developed some likes and dislikes. Oh, not about anything that smart engineers would consider important, of course. It’s mostly about… prettiness.
One thing that bothers me is when people put “true” or “false” in a ternary expression. If you don’t know what that is, it’s one of those things that looks like
p ? x : y
that just means “if p, then x; otherwise y.”
OK, so it really bugs me when I see things like
x ? y : false
I tear my hair out. I have to read this and think to myself that they’re trying to say “if x, then y; otherwise false.” But then that’s equivalent to saying that the expression is true if x and y are both true, otherwise it’s false, right?
It always seems to me that putting a “true” or “false” in a ternary expression creates extra cognitive burden that I just don’t want. A ternary expression is sort of like a conditional, but a disjunction or conjunction is just so much more natural to understand, in my opinion.
So here are some easy conversions!
x ? y : false => x && y x ? y : true => !x || y x ? false : y => !x && y x ? true : y => x || y
Shorter and more legible. At least in my opinion.