Clean Code with Java: Replace the logical condition using ENUM(if / else statements)

Roan Monteiro
4 min readJul 11, 2020

Hello everybody, we are going start the first part of Clean Code with Java session and today I am going to present one thing that it is really important when you are evaluate a Pull Request or try to understand a code. Sometimes when you arrive in an ongoing project is really hard to read and understand some codes, mainly when those codes have many if / else statements. I bring here a good to make your clean code with Java.

Java logo

Thinking about SOLID, the S — Single of Responsibility, I will use the idea from my friend @Henri_Tremblay in his Github with TransactionType class from Montreal Java User Group.

Since we can assume we have 4 types of operations in the TransactionType such as BUY, SELL, DEPOSIT, WITHDRAWAL. For each operation I have a rule to calculate a tax value. How would your first idea to implement this? Normally people tend to do if / else statement on this. Normally everybody tends to do like the example below. The code is hard to understand and is not clean.

What can we do to improve the code? Would you approve this PULL REQUEST? For sure, I wouldn't approve this because we…

--

--