DEV Community

Discussion on: What do you think about the ternary operator?

deceze profile image
David Zentgraf

Everything can be written in multiple different ways. The Best Way™ to write something depends on what it is you're trying to write. Sometimes it's much more readable to write a ternary expression, and sometimes it's much more readable to write that same code in an if, and sometimes in a switch, and sometimes using a dictionary or whatever other idiom your language of choice offers. The important thing is to (re-)write code in the most readable way whenever you touch it. There are no hard and fast rules. A piece of code may start out as a ternary expression, but when added to must be rewritten into something else to remain readable. Things become unreadable when you leave something as is (e.g. as a ternary expression) while adding to it because you don't feel like rewriting it.

shreyasminocha profile image
Shreyas Minocha Author

Very true.