Sometimes there is need to do another action at first call of a method. Suppose client shouldn’t know about that. It can be done in a standard way, using if/else statement and boolean variable indicating first call of a method.
However, another solution exists. Make a class inheriting a standard command class. In inherits class a chosen method is overwritten and has a code that should be executed at first time. At the end of this method a command object is replaced by standard one.
Here are next ideas how to replace “if” statement by other patterns.
Improvement of loop
Each loop checks condition if the next iteration should be performed. You should use this construction instead of using if(..) break; inside the loop if it’s possible.
if statement is often used in almost every code. Unfortunately it reduces readability of source code and may influences of performance (but rather in low level programming languages). Below I show examples of
if and propositions of improvements.
Often there’s need to change variable or return value depending on some condition. Instead of if/else it’s better to use conditional operator: