Do You Want to Remove "If" Statements From Your Java Code?
Discover the techniques and strategies I employed to reduce the reliance on "if" statements in a legacy codebase.
👋 Hi, this is Dishit with this week’s newsletter. I write about software engineering, clean code and developer productivity. Thank you for your readership 🙏 🎉

Have you ever struggled with code that's hard to understand and modify?
Did you find yourself adding breakpoints and debugging the code just to understand how the logic worked?
Did you feel if someone had written it more simply, it would have made your life easier?
I felt the same way when I was asked to work on a legacy project a few years back.
Today, I will tell you how I removed all the if statements.
Not all. But I removed most of them.
The Legacy Project
The project was enormous.
The methods were super long and the classes longer.
The business logic was embedded deep into the methods.
There was no clarity on how much business logic was present in those methods.
Moreover, the cyclomatic complexity for some methods ranged from 20 to 35.
After a while of delivering features, I thought it was enough.
This cannot go on.
Challenge
I created a challenge for myself.
I will remove all the “if” statements from the code.
There was also a selfish reason for this.
I wanted to learn all about Optional API which I thought was a game-changer.
The Refactoring Process.
I started refactoring the simpler ones and then I found another challenge.
There were no unit tests.
It was production code and I could not afford to break any functionality.
So instead of marching forward, I went back creating foundations.
This was the process I followed.
I wrote unit tests.
But, I did not write all the unit tests in one go.
I would identify 1 method to refactor.
Write unit tests for that method.
I would ensure the test coverage of that method was 100%.
Refactor code.
Rerun the tests
Go to step 1
What I learnt
I had focussed on using Optional API as null checks were the biggest source of if-conditions.
But soon I found Optional API is not the silver bullet to solving all the problems in the code.
To truly conquer the "if" statement problem, I went deeper.
Here's what I discovered:
Guard Statements:
These statements act as gatekeepers, ensuring invalid data never enters crucial sections of the code.
By placing guard statements at the beginning of a method or block, we can quickly exit if certain conditions aren't met, preventing unnecessary processing and improving code flow.
It was easier to understand as developers who want to know the logic can ignore the initial couple of lines and read the core logic.
Ternary Operators: These compact conditional expressions provide a concise way to write "if-else" statements in a single line.
While overuse can lead to less readable code, they can be effective for simple checks within a method.
You can also create a simple 1-line method using the ternary operator. You can name the method as a business rule.The above code transforms to:
Policy Rules
Create classes or methods that encapsulate business rules. What that did was, I could write tests only for that business rule.
It became easier for a new developer to learn the business rules.
Separating logic into reusable policy rules promotes code organization and maintainability.Streams API
Introduced alongside lambdas in Java 8, the Streams API provides a powerful and concise way to process data collections.
By chaining together operations like filtering, mapping, and reducing, we can manipulate data efficiently, often eliminating the need for explicit "if" statements within loops.Mockito Library
To write effective tests, the Mockito library helped in mocking the dependent functionality. It also meant the Single Responsibility Principle was adhered to.
Did I succeed?
I could not eliminate all the “if” statements in the code. But the experience was worthwhile. I also achieved:
Reduced cyclomatic complexity of all the methods to under 10.
All the refactored methods had 100% coverage
Smaller methods
Reusable and testable code
This experience ignited my passion for promoting cleaner code practices.
That's why I'm excited to announce my upcoming course, "Code Like a Pro".
In this comprehensive program, I'll delve deeper into the techniques I used, equipping you with the skills to conquer "if"-heavy code and transform it into clear, maintainable masterpieces.
Stay tuned for more information about the course launch!