DEV Community

Cover image for Refactoring 014 - Remove IF
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

Refactoring 014 - Remove IF

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

Related Code Smells

Steps

  1. Find or Create a Polymorphic Hierarchy

  2. Move the Body of Each IF to the Corresponding Abstraction

  3. Name the Abstractions

  4. Name the Method

  5. 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. */
Enter fullscreen mode Exit fullscreen mode

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();
    }
Enter fullscreen mode Exit fullscreen mode

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

Tags

  • IFs

Related Refactorings

See also

Credits

Image by Renuagra on Pixabay


This article is part of the Refactoring Series.

Top comments (1)