DEV Community

Cover image for 2 Tips to Clean Ugly if Statements
Petros Koulianos
Petros Koulianos

Posted on

2 Tips to Clean Ugly if Statements

Conditional statements are the backbone of programming but lot of times business requirements can lead to create long nested and ugly if statements.
This post demonstrates some tips to clean those ugly situations.

#1 Complex condition expressions

Long and complex condition expressions are obvious an ugly situation

// weird 😣😣
if((temp === 0) || (temp > 0 && temp < 5 && gusts > 10) || (snowing)){
//code block
}
Enter fullscreen mode Exit fullscreen mode

Solution create a separate function to return a boolean that represents the long condition

// better 😁😁
if(isColdOutside(temp, windGusts, snowing)){
//code block
}

function isColdOutside(temp, windGusts, snowing){
  if(temp === 0){
   return true
  }
  if(snowing){
   return true
  }
  if(temp > 0 && temp < 5 && windGusts > 10){
   return true
  }
  return false
}
Enter fullscreen mode Exit fullscreen mode

#2 Ternary into ternary

This is another situation that is ugly and the human brain struggle to parse

// weird 😣😣
let temp = 6
let windGusts = 20
let isFreezingOutside = temp < 5 ? (windGusts > 15 ? true : false) : (snowing ? true : false)
Enter fullscreen mode Exit fullscreen mode

Solution again here we can create smaller functions to make it cleaner

// better 😁😁
let temp = 6
let windGusts = 20

let isFreezingOutside = temp < 1 ? isSnowing(snowing) : isWindStrong(windGusts)

function isWindStrong(windGusts){
  if(windGusts > 15){
   return true
  }
return false
}

function isSnowing(snowing){
  if(snowing){
   return true
  }
return false
}
Enter fullscreen mode Exit fullscreen mode

This was two quick tips to clean ugly if statements.

Thanks for reading 😎😎😎

twitter @petroskoulianos

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (2)

Collapse
 
amiceli profile image
amiceli β€’

Another tips I use :

if (a === "test" || a === "foo") {}
Enter fullscreen mode Exit fullscreen mode

Better I think :

if (["test","foo"].includes(a)){}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
tirthaguha profile image
Tirtha Guha β€’

For #1 Complex condition expressions
I would prefer writing the complex condition, as it is simpler than 3 if statements.

function isColdOutside(temp, gusts, snowing){
  return ((temp === 0) || (temp > 0 && temp < 5 && gusts > 10) || (snowing))
}
Enter fullscreen mode Exit fullscreen mode
πŸ‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Communityβ€”every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple β€œthank you” goes a long wayβ€”express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay