Best Practices for writing If-else.

Bal sikandar
3 min readMay 13, 2021

--

In this blog, we’ll talk about avoiding or writing better If conditions on a case-to-case basis.

More If else means more branching and more paths to follow in a program. So, If we can avoid that, we can reduce the complexity of our program.

Preview

This blog has few examples that discuss how to reduce if-else blocks from code or make them less complex.

It mostly contains code so read It when you have your code brain-on.

Example 1: If-else chain with one object type in conditional

Variant1: With string checks in a conditional block when there are too many conditions.

Note: Don’t use Switch either for this otherwise when more cases are added switch becomes bloated.

Snippet 1: showing bad practice involving If-else

Solution: Map can be used for the above if-else chain and bloated switch cases.

Snippet 2: Using a map to avoid the If-else chain for the snippet 1

Variant 2: When we use if else in place of Polymorphism or type of checks

‘If’ shouldn’t be used in places of polymorphism like below.

if (vehicle.isBus()) return speed()
else if (vehicle.isCar()) return speed()
else if (vehicle.isLimo()) return speed()
//instead of using below statement above if-else being usedvehicle.getSpeed()

A better solution for the above is logically separate code by using polymorphism. Check out the below snippet.

Snippet 3: Usage of Polymorphism to avoid If else

Example 2: Replace If with && and || operators

If you have If-else blocks that return booleans, you can combine them with ‘&&’ and ‘||’ operators.

//one, two, three, four are logical expressions equating to //Booleans.if (one) {
if (two) {
return true
}
}

if (three) {
return four
}
//Solution
return (one && two) || (three && four)

Example 3: Complex If else chain, It gets more complex when more conditions are added.

Please take your time to understand the given conditions then we’ll discuss ways to make it more readable.

Here we have two tabs Pickup & Drop with location lists in each to select. We can go to the next page when both options are selected.

  • pickupSelected — means pickup location from pickup list is selected
  • dropSelected — means drop location from drop list is selected
  • selectedTab — represents the current tab
Snippet 4: Obscure If else chain, right now it looks clean but with more conditions, it’ll become a mess.

Solution 1: Early return, the first condition stands on its own and can be returned early on.

Snippet 5: Early return example

Solution 2: Break the chain if there are only two or three blocks after break up.

Snippet 6: Broken If-else chain example

Code Smell (Added Just for fun): It’s actually a code smell due to over-engineering. It looks clean but hard to understand especially what the heck msg is doing and to check if you’re paying attention.

Snippet 7: Over-engineered solution

Solution 3: Break nested If blocks in smaller functions so it’s easier to read. Personally, I prefer both options 2 and 3.

Snippet 8: Nested blocks moved in separate functions example

Always try to avoid nested If blocks.

Tips:

  • long conditions should be put into separate functions
  • Cleaner If-else makes reading code more enjoyable.

Summarize:

  • Use mapper for string checks, type of checks, or polymorphism.
  • Use early return to reduce the complexity of If else chain.
  • Break the chain and write separate(up to only 2 or 3) blocks, as per your taste.
  • Move nested blocks to separate methods.

We should try to write better Ifs since we may be the ones reading it later. DIFY(Do it for yourself).

Happy Coding!

Learn more: Checkout command and strategy pattern to avoid If-else for specific cases.

Thanks for reading this article. Be sure to 👏 recommend this article if you found it helpful. It means a lot.

Also let’s connect on twitter, github and linkedin.

Clap, share if you like it and follow me for my next move.

--

--

Bal sikandar

Android developer @shuttl Ex @_okcredit. Blogger | Open source contributor https://about.me/balsikandar.