10 Proven Techniques to Eliminate Excessive if…else in Your Code
This article explains why overusing if…else harms code readability and maintainability, outlines ten practical methods—including table‑driven, chain of responsibility, annotation‑driven, event‑driven, state machines, Optional, Assert, and polymorphism—to refactor or replace complex conditional logic, and provides Java code examples for each approach.
Introduction
if...else is essential but overuse harms readability and maintainability; this article explores how to eliminate excessive if...else.
Problem 1: Too many if...else
Code with many branches violates Single Responsibility and Open/Closed principles, reducing extensibility.
if (condition1) { ... } else if (condition2) { ... } else if (condition3) { ... } else if (condition4) { ... } else { ... }How to Solve
Table‑driven
Chain of Responsibility
Annotation‑driven
Event‑driven
Finite State Machine
Optional
Assert
Polymorphism
Method 1: Table‑driven
Map fixed logical expressions to a table and look up the corresponding handler.
if (param.equals(value1)) { doAction1(someParams); }
else if (param.equals(value2)) { doAction2(someParams); }
else if (param.equals(value3)) { doAction3(someParams); }Refactored version using a map:
Map<?, Function<?>> actionMappings = new HashMap<>();
actionMappings.put(value1, (p) -> doAction1(p));
actionMappings.put(value2, (p) -> doAction2(p));
actionMappings.put(value3, (p) -> doAction3(p));
actionMappings.get(param).apply(someParams);Method 2: Chain of Responsibility
When conditions are flexible, delegate the decision to a chain of handlers.
public void handle(Request request) {
if (handlerA.canHandle(request)) {
handlerA.handleRequest(request);
} else if (handlerB.canHandle(request)) {
handlerB.handleRequest(request);
} else if (handlerC.canHandle(request)) {
handlerC.handleRequest(request);
}
}After refactoring:
public void handle(Request request) {
handlerA.handleRequest(request);
}
abstract class Handler {
protected Handler next;
public abstract void handleRequest(Request request);
public void setNext(Handler next) { this.next = next; }
}Method 3: Annotation‑driven
Use Java annotations (or similar mechanisms) to declare when a method should be executed, often implemented with table‑driven or chain‑of‑responsibility techniques.
Method 4: Event‑driven
Link events to handlers, achieving loose coupling; suitable for scenarios like order payment triggering inventory, logistics, and points.
Method 5: Finite State Machine
A state machine models a finite set of states and transitions, useful for protocols, order processing, etc.
Finite‑state machine (FSM) is a mathematical model of a limited number of states and transitions.
Method 6: Optional
Java 8 Optional removes null‑check if…else.
String str = "Hello World!";
if (str != null) { System.out.println(str); } else { System.out.println("Null"); }With Optional:
Optional<String> strOptional = Optional.of("Hello World!");
strOptional.ifPresentOrElse(System.out::println, () -> System.out.println("Null"));Method 7: Assert
Use validation utilities (Apache Commons Validate, Spring Assert) to replace repetitive checks.
Method 8: Polymorphism
Replace conditional logic with subclass implementations, as described in Martin Fowler’s refactoring catalog.
Additional Techniques
Extract Method and Guard Clauses further improve readability of nested conditionals.
Conclusion
Eliminating excessive if…else demonstrates mastery of refactoring, design patterns, and software architecture; the right technique depends on the problem’s nature.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
