DEV Community

Discussion on: Top 20 JavaScript tips and tricks to increase your Speed and Efficiency

Collapse
 
pazka profile image
Alexandre Weisser

I would advise to not use nested ternary operators as it is considered a code smell

Collapse
 
techygeeky profile image
Kapil Raghuwanshi🖥

Can you share the disadvantages with code snippets ?

Collapse
 
pazka profile image
Alexandre Weisser

Sorry I don't have the time nor the patience at the moment but I can at least detail my answer :

Simply, if you think about the Signal-to-Noise ratio, nesting ternary operator will more often lead to error when sigh treading and ask more energy to be understood for the sake of a very few line of code.

In this particular case, writing a more explicit comparison will be better for the whole understanding of the code flow. with "ifs" or juste split it in more lines of ternary attribution.

Also, if you should write half a dozen comparison for the attribution of a variable, you should put it in a specific function that will be named accordingly and this will greatly improve this part of the code.

For more information, you can look in the ESLint documentation for this specific code smell : eslint.org/docs/rules/no-nested-te...

Also thank you for your post !