Ata Sasmaz

Software Engineer
atIBMin Dublin, Ireland

Minimising if/else statements in code

« Home

Using lots of if statements, especially nested if statements are a bad smell in code. They make the code difficult to understand to other developers and decrease team work. A nested if statement code like below difficult to understand and prone to errors.

By constructing a good software architecture we can eliminate IF/ELSE statements for handling the workflow. It is safe to have validation checks for like null or undefined variables (i.e. if (x == null) throw new exception()), however dividing the logic with IF/ELSE statements points out a bad technical design.


As long as developers use polymorphism, inheritance, interfaces, aspect oriented programming (or hooks), dependency injection with many other suitable and creative design patterns there is no need for IF/ELSE statements for handling the business workflow. 

Adapting the best software engineering practices will make the software more maintainable, also more easy to develop after some point. Software Development is not about writing code, it is structuring the logic with the best suitable architecture for the well-understood problem. Coding is the easy part. Instead of writing this code piece:

if (person.job == "singer")
else if (person.job == "developer")

I can write:


by using polymorphism. Or instead of:

if (session["user"] == "guest")
   user = getGuestById(session["Id"])
else if (session["user"] == "member")
   user = getMemberById(session["Id"]);

I can write:

ISessionHandler sessionHandler = sessionHandlerFor(session["user"]);
user = sessionHandler.getById(session["Id"])

Be aware that  we have two implementations for ISessionHandler, one is for getting the member and the other is for getting the guest. Do you think you can write code without if statements in the business logic layer? Please share below.

Disclaimer: Opinions expressed herein are my personal and do not represent my employer’s view.
This website uses cookies for security purposes and anonymous statistics tracking.