DEV Community

Discussion on: Why I'm phasing out ternary statements

Collapse
riccardomessineo profile image
Riccardo Messineo

I actually think they are very useful to make your code concise and easier to read.
The problems arise if you abuse of them - as always.

Ternaries (imho of course) should be used only to assign values

a = (b == c) ? 'ok' : 'ko'

Avoid:

  • ternaries in ternaries
  • logic in ternaries
a ? b : (c ? d : f)
a ? doThis() : doThat()
Collapse
mcgurkadam profile image
Adam McGurk Author

I definitely agree that we need to avoid ternaries in ternaries (I've seen that in wild....just why????) and also logic in ternaries...

And I agree that the time to use them is when we're assigning value...and only then. And there is definitely a time and a place for them!

Thanks for reading!!!

Collapse
riccardomessineo profile image
Riccardo Messineo

Thank you for sharing!

I really appreciate that readability is becoming more important than having a fewer lines of code, as you suggested.

We need to understand that we are mainly readers of code, and only then writers.

Thread Thread
mcgurkadam profile image
Adam McGurk Author

Totally agree!!! We are definitely readers first, creators second, writers third.