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.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more