DEV Community

Mathan raj
Mathan raj

Posted on • Edited on

3 2

Basics of refactoring a bad code | part 1 - If condition

We have all been in a situation that when we are growing as a developer often we find ourselves writing code that doesn't look right or we have a doubt that whether it follows the best practices. This article is just part 1 of the total n series. (I really don't know how many would come 🤷‍♂️)

If statement

If statements can be a root cause of writing bad code. Always think twice before writing nested if conditions whether there is a way to simplify it. A badly written if condition can reduce the readability of the program.
Let's make the following assumptions here. Any clean code will have the following properties.

  • Readable
  • Concise
  • Scalable - Should be able to allow more conditions or business logic to be added without affecting the above two factors.

Example 1 of Bad code

// Not clear code
// Readable - 0
// Concise - 0
// Scalable - 0
fun isUserValid(user: User): Boolean {
if (user.token != null) {
if (user.token != "") {
if (user.userId != null) {
if (user.userId != "") {
return true
}
}
}
}
return false
}

Improvement 1 - Combining relevant conditions
// Slightly improved
// Readable - 1
// Concise - 1
// Scalable - 0
fun isUserValid(user: User): Boolean {
if(user.token != null && user.token != "") {
if(user.userId != null && user.userId != "") {
return true
}
}
return false
}

Improvement 2 - Applying inversion of condition
// Much better
// Readable - 1
// Concise - 1
// Scalable - 1
fun isUserValid(user: User): Boolean {
if(user.token == null || user.token == "") {
return false
}
if(user.userId == null || user.userId == "") {
return false
}
// More complex conditions
return true
}

Combining relevant conditions

Combining multiple conditions that are relevant by using the logical operator can reduce down the lines of code and implies a better understanding of the condition.

Inversion of condition

Just by inverting the thought process that goes for the condition in if statement can vastly improve the readability.

Example 2 of Bad code

// Bad code
// Readable - 1
// Concise - 0
// Scalable - 0
fun unlockPremiumFeature(user: User): Boolean {
if(isUserValid(user)) {
if(isPremiumUser(user)) {
// Unlock premium membership
} else {
// Activate Premium membership
// Unlock premium feature
}
return true
} else {
return false
}
}

Improved Code - Applied short-circuiting + inversion of condition
//Improved If statement
// Readable - 1
// Concise - 1
// Scalable - 1
fun unlockPremiumFeature(user: User): Boolean {
// Inversion of condition + short-circuiting
if(!isUserValid(user)) {
return false
}
if(!isPremiumUser()) {
// Unlock premium membership
}
// Unlock premium feature
return true
}

Short-circuting

Failing-fast will help you deal with fewer problems going further. (short-circuiting is the trump card remember)

We have discussed some of the practices I follow when I write

Tips: switch statements can be of a good replacement for if statement in case of checking for enum or strings.

Top comments (0)

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

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay