The first instruction you learned should be the least you use.
TL;DR: Remove all your Accidental IF-sentences
Problems Addressed
- Code Duplication
- Possible Typos and defects
Code Smell 07 – Boolean Variables
Code Smell 36 – Switch/case/elseif/else/if statements
Code Smell 133 – Hardcoded IF Conditions
Code Smell 156 – Implicit Else
Code Smell 145 – Short Circuit Hack
Code Smell 101 – Comparison Against Booleans
Code Smell 45 – Not Polymorphic
Steps
-
Find or Create a Polymorphic Hierarchy.
-
Move the Body of Each IF to the Corresponding Abstraction.
-
Name the Abstractions.
-
Name the Method.
-
Replace if Statements with Polymorphic Message Sends.
Sample Code
Before
public String handleMicrophoneState(String state) {
if (state.equals("off")) {
return "Microphone is off";
} else {
return "Microphone is on";
}
}
/* The constant representing the 'off' state is
duplicated throughout the code,
increasing the risk of typos and spelling mistakes.
The "else" condition doesn't explicitly check for the 'on' state;
it implicitly handles any state that is 'not off'.
This approach leads to repetition of the IF condition
wherever the state needs handling,
exposing internal representation and violating encapsulation.
The algorithm is not open for extension and closed for modification,
meaning that adding a new state
will require changes in multiple places in the code. */
After
// Step 1: Find or Create a Polymorphic Hierarchy
abstract class MicrophoneState { }
final class On extends MicrophoneState { }
final class Off extends MicrophoneState { }
// Step 2: Move the Body of Each IF to the Corresponding Abstraction
abstract class MicrophoneState {
public abstract String polymorphicMethodFromIf();
}
final class On extends MicrophoneState {
@Override
public String polymorphicMethodFromIf() {
return "Microphone is on";
}
}
final class Off extends MicrophoneState {
@Override
public String polymorphicMethodFromIf() {
return "Microphone is off";
}
}
// Step 3: Name the Abstractions
abstract class MicrophoneState {}
final class MicrophoneStateOn extends MicrophoneState {}
final class MicrophoneStateOff extends MicrophoneState {}
// Step 4: Name the Method
abstract class MicrophoneState {
public abstract String handle();
}
final class MicrophoneStateOn extends MicrophoneState {
@Override
String handle() {
return "Microphone is on";
}
}
final class MicrophoneStateOff extends MicrophoneState {
@Override
String handle() {
return "Microphone is off";
}
}
// Step 5: Replace if Statements with Polymorphic Message Sends
public String handleMicrophoneState(String state) {
Map<String, MicrophoneState> states = new HashMap<>();
states.put("muted", new Muted());
states.put("recording", new Recording());
states.put("idle", new Idle());
MicrophoneState microphoneState =
states.getOrDefault(state, new NullMicrophoneState());
return microphoneState.handle();
}
Type
- [x]Semi-Automatic
Safety
Most steps are mechanic. This is a pretty safe refactoring.
Why is the code better?
The refactored code follows the open/closed principle and favors polymorphism instead of using IFs.
Limitations
You should only apply it to Accidental IFs.
Leave the business rules as “domain ifs,” and don’t apply this refactoring.
See Also
This article is part of the Refactoring Series.