DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Dust: Open-Source Actors for Java
  • Redefining Java Object Equality
  • Workarounds for Oracle Restrictions on the Size of Expression Lists

Trending

  • Docker Base Images Demystified: A Practical Guide
  • AI Meets Vector Databases: Redefining Data Retrieval in the Age of Intelligence
  • The Modern Data Stack Is Overrated — Here’s What Works
  • Doris: Unifying SQL Dialects for a Seamless Data Query Ecosystem
  1. DZone
  2. Coding
  3. Java
  4. Avoiding If-Else: Advanced Approaches and Alternatives

Avoiding If-Else: Advanced Approaches and Alternatives

Skip if-else statements by using design patterns, polymorphism, and functional strategies for cleaner, more adaptable, and significantly more testable code architectures.

By 
Taras Ivashchuk user avatar
Taras Ivashchuk
·
Jan. 02, 25 · Tutorial
Likes (14)
Comment
Save
Tweet
Share
9.4K Views

Join the DZone community and get the full member experience.

Join For Free

Mostly, developers make use of if-else statements to cater to differing circumstances. Even so, this could prove itself quite troublesome especially when more conditions arise. Putting additional business needs into these chains might cause errors while making the code more complicated than necessary. It is advisable that we anticipate eventually creating solutions that could change or grow without being difficultly updated in order not only to ensure the robustness of one’s system but also to enable its adaptation within unforeseen circumstances. Our codes will then remain potent and readily adaptable to the needs ahead us in this case.

In this article, we’ll delve into methods for managing functions in a calculator using Java in all examples. The aim is to enhance the processing of operations (such as addition, subtraction, multiplication, and division) in our coding. We’ll incorporate techniques by using a sample calculator that receives a request with an operation type and two values, including if-else statements, switch cases, and the strategy design pattern. The main focus will be on describing the concepts and benefits of each method.

Request Structure

First, let’s define the request structure, which will be used to demonstrate different approaches:

Java
public class CalculationRequest {
 private final Operation operation;
 private final int first;
 private final int second;

 // Constructor, getters, and setters
}

enum Operation{
  ADD,
  SUBTRACTION,
  DIVISION,
  MULTIPLICATION;  
}


If-Else Statement

If-else statements are one of the simplest and most commonly used constructs for handling conditions in programming. They allow the execution of a specific block of code depending on whether a condition is met. In the context of a calculator, if-else statements can be used to handle various operations like addition, subtraction, multiplication, and division. Consider the following example demonstrating the use of if-else statements for performing these operations:

Java
public static Integer calculate(CalculationRequest request) {
 var first = request.getFirst();
 var second = request.getSecond();
 var operation = request.getOperation();

if (Operation.ADD.equals(operation)) {
   return first + second;
 } else if (Operation.SUBTRACTION.equals(operation)) {
   return first - second;
 } else if (Operation.DIVISION.equals(operation)) {
   if (second == 0) {
     throw new IllegalArgumentException("Can't be zero");
   }
   return first / second;
 } else if (Operation.MULTIPLICATION.equals(operation)) {
   return first * second;
 } else {
   throw new IllegalStateException("Operation not found");
 }
}


Pros

  • Simplicity: Easy to understand and implement.
  • Clarity: The code clearly shows what happens in each condition.

Cons

  • Maintenance: Changing logic requires modifications in multiple places, increasing the risk of errors. For example, if a new operation is added, another condition must be added to the if-else chain, increasing the risk of missing a condition. Numerous changes in different places also complicate debugging and testing the code.
  • Scalability: Adding new operations requires changing existing code, violating the open/closed principle (OCP) from SOLID. Each new condition requires modifying existing code, making it less flexible and resilient to changes. This can lead to increased technical debt and reduced code quality in the long term.

Switch Statement

Switch statements can be more readable and convenient in some cases compared to if-else chains. They allow better structuring of the code and avoid long chains of conditions. Let’s consider using switch statements.

Java
public static Integer calculate(CalculationRequest request) {
 var first = request.getFirst();
 var second = request.getSecond();
 var operation = request.getOperation();

return switch (operation) {
   case ADD -> first + second;
   case SUBTRACTION -> first - second;
   case DIVISION -> {
       if (second == 0) {
       throw new IllegalArgumentException("Can't be zero");
     }
     yield first / second;
   }
   case MULTIPLICATION -> first * second;
 };
}


Pros

  • Readability: More structured compared to a long if-else chain. The code becomes more compact and easy to read.
  • Simplification: Clear separation of different cases, making the code neater.

Cons

  • Scalability: Like if-else, adding new operations requires changing existing code, violating the open/closed principle (OCP) from SOLID.
  • Flexibility: Switch statements can be less flexible than some other approaches. For example, they do not easily integrate complex logic or states that may be necessary in some operations. This makes them less suitable for advanced use cases where more complex processing is required.

Strategy Pattern

The strategy pattern allows defining a family of algorithms, encapsulating each one, and making them interchangeable. This allows clients to use different algorithms without changing their code. In the context of a calculator, each operation (addition, subtraction, multiplication, division) can be represented by a separate strategy. This improves the extensibility and maintainability of the code, as new operations can be added without changing existing code.

Pros

  • Scalability: Easy to add new strategies without changing existing code. This is especially useful in situations where new functions need to be supported or added in the future.
  • SOLID Support: The pattern supports the single responsibility principle (SRP), as each strategy is responsible for its specific operation. It also supports the open/closed principle (OCP), as new strategies can be added without changing existing classes.
  • Flexibility: Algorithms can be easily changed at runtime by substituting the appropriate strategy. This makes the system more flexible and adaptable to changing requirements.

Cons

  • Complexity: Can add additional complexity to the code, especially when implementing multiple strategies. The number of classes increases, which can make project management difficult.

Let’s look at different implementation options:

Enum

In this example, we create an enum Operation with an abstract apply method. Each enum element corresponds to an implementation of this method. This encapsulates the logic of each operation in separate enumeration elements, making the code more organized and maintainable.

Java
public enum Operation {
    ADD {
        @Override
        Integer apply(int first, int second) {
            return first + second;
        }
    },
    SUBTRACTION {
        @Override
        Integer apply(int first, int second) {
            return first - second;
        }
    },
    DIVISION {
        @Override
        Integer apply(int first, int second) {
            if (second == 0) {
                throw new IllegalArgumentException("Can't be zero");
            }
            return first / second;
        }
    },
    MULTIPLICATION {
        @Override
        Integer apply(int first, int second) {
            return first * second;
        }
    };

  abstract Integer apply(int first, int second);
}


Usage:

Java
public static Integer calculate(CalculationRequest request) {
    var first = request.getFirst();
    var second = request.getSecond();
    var operation = request.getOperation();
    
    return operation.apply(first, second);
}


Map of Objects

The OperationStrategy interface defines the apply method to be implemented for each operation, creating a single contract for all operations, simplifying the addition of new strategies.

Java
public interface OperationStrategy { 
 Integer apply(int first, int second); 
}


Each operation is implemented as a separate class that implements the OperationStrategy interface. Each class implements the apply method to perform the corresponding operation.

Java
class AddOperationStrategy implements OperationStrategy {
    @Override
    public Integer apply(int first, int second) {
        return first + second;
    }
}

class SubtractionOperationStrategy implements OperationStrategy {
    @Override
    public Integer apply(int first, int second) {
        return first - second;
    }
}

class DivisionOperationStrategy implements OperationStrategy {
    @Override
    public Integer apply(int first, int second) {
        if (second == 0) {
            throw new IllegalArgumentException("Can't be zero");
        }
        return first / second;
    }
}

class MultiplicationOperationStrategy implements OperationStrategy {
    @Override
    public Integer apply(int first, int second) {
        return first * second;
    }
}


We create a map STRATEGY_OBJECT_MAP where the keys are values of the Operation enum, and the values are the corresponding OperationStrategy implementations. This allows for the quick finding and use of the necessary strategy for each operation.

Java
public static final Map<Operation, OperationStrategy> STRATEGY_OBJECT_MAP =
            Map.ofEntries(
                    Map.entry(Operation.ADD, new AddOperationStrategy()),
                    Map.entry(Operation.SUBTRACTION, new SubtractionOperationStrategy()),
                    Map.entry(Operation.DIVISION, new DivisionOperationStrategy()),
                    Map.entry(Operation.MULTIPLICATION, new MultiplicationOperationStrategy())
            );


The method retrieves the necessary strategy from the map and performs the operation by calling the apply method.

Java
public static Integer calculate(CalculationRequest request) {
        var first = request.first();
        var second = request.second();
        var operation = request.operation();

        return STRATEGY_OBJECT_MAP.get(operation).apply(first, second);
    }


Map of Functions

This approach uses functional interfaces for each operation and creates a map where keys are operations, and values are functions. This avoids creating separate classes for each strategy, simplifying the code and making it more compact.

Java
public static final Map<Operation, BiFunction<Integer, Integer, Integer>> STRATEGY_FUNCTION_MAP;

static {
    STRATEGY_FUNCTION_MAP = Map.ofEntries(
        Map.entry(Operation.ADD, (first, second) -> first + second),
        Map.entry(Operation.SUBTRACTION, (first, second) -> first - second),
        Map.entry(Operation.DIVISION, (first, second) -> {
            if (second == 0) {
                throw new IllegalArgumentException("Can't be zero");
            }
            return first / second;
        }),
        Map.entry(Operation.MULTIPLICATION, (first, second) -> first * second)
    );
}
public static Integer calculate(CalculationRequest request) {
    var first = request.getFirst();
    var second = request.getSecond();
    var operation = request.getOperation();
    
    return STRATEGY_FUNCTION_MAP.get(operation).apply(first, second);
}


Using a map of functions is quite suitable in cases when you need to implement a set of operations as quickly and easily as possible without creating separate classes for each. However, object strategies fit better in more complex scenarios.

Conclusion

Every method has its advantages and disadvantages that need to be considered in software development decisions. If-else switch statements are straightforward and user-friendly at first but become challenging to organize as the number of conditions grows. They don’t adapt well to changes and make it difficult to incorporate functions without altering the codebase.

The strategy pattern provides an adaptable approach to managing operations while adhering to SOLID principles. This makes it simple to incorporate operations without disrupting code, promoting code maintenance and scalability. Moreover, it enables adaptation to evolving business requirements without strain. Although initially complex, the benefits of its extendable code base prove worthwhile.

A meme about if-else

Data Types Software design pattern Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Recurrent Workflows With Cloud Native Dapr Jobs
  • Dust: Open-Source Actors for Java
  • Redefining Java Object Equality
  • Workarounds for Oracle Restrictions on the Size of Expression Lists

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!