DEV Community


Discussion on: Easy refactor ternary conditions to if else

moopet profile image
Ben Sinclair

The "before" code made me raise my eyebrows, because I can't imagine anyone creating anything like that by hand. It's the sort of thing that would come out of a code minifier, not out of a human brain.

Then I looked at the "after" code and I think it's worse.

This is a great example of where splitting it up into nicer variable names and keeping each if separate would be better:

let bestCarnivalRide = null;

if (mySistersHeight > 172) {
  bestCarnivalRide = 'rollercoaster';

if (mySistersFears.indexOf('spiders') > -1) {
  bestCarnivalRide ='house-of-horrors';

// etc.
danielpdev profile image
danielpdev Author

The code provided was just an example and not real code in production.

The real problem I had was with a nested ternary written in a string and then passed to eval. It had a real long nested conditions and babel plugin helped me refactored that code.

I agree with you, no code like that should be in a production application.