DEV Community

Discussion on: Why I'm phasing out ternary statements

Collapse
 
sargalias profile image
Spyros Argalias

Personally I wouldn't use if / else statements for assignment. At least not very often:

  • If we are assigning multiple things inside the if / else statement, then we may lose track of one of them. It's less clear what's going on at first glance.
  • The declaration can be moved much further away, making things less clear.
  • We cannot use const, which gives us a guarantee of immutability (relatively speaking).

Having said that, I don't really remember having to conditionally assign multiple things...

However if I felt it was best, I would definitely replace ternary statements with if / else statements in other cases. Code readability and maintainability are extremely important. If you and your team believe that using if / else statements is best for that, then that's a very important consideration.

Collapse
 
mcgurkadam profile image
Adam McGurk

I definitely agree with you, I love using const, and if/elses take it away!

Thanks for reading!!

Collapse
 
anssip profile image
Anssi Piirainen

Completely agree with this. I always use const and resort to a mutable variable only if I cannot find a way around it. Variables that can be mutated add mental load and make the program harder to follow.

Collapse
 
mcgurkadam profile image
Adam McGurk

That’s definitely fair!!

Thanks for reading!