DEV Community

Stop using if...else ✋🚫

Dipankar Shaw on December 31, 2022

Stop using if...else 🚫 for every thing. There is a better option than if...else which is simpler to write and less line of code. This alternative i...
Collapse
 
cicirello profile image
Vincent A. Cicirello

Nesting/chaining the conditional operator (or "ternary") is considered a code smell: rules.sonarsource.com/cpp/RSPEC-3358

Actually some consider the ternary operator itself to be a code smell (rules.sonarsource.com/cpp/RSPEC-1774). But I think that is too strong to say that generally as there are lots of places where it can help with readability such as your min example. If you're ignoring its return or using it to call void functions that don't return anything, then that would certainly be detrimental to readability.

Collapse
 
pedromellogomes profile image
Pedro Mello

I personally like ternary operator syntax, but I rather use if statements because I feel its more readable. It becomes even harder when you chain ternary operators.