DEV Community

.Net Labs
.Net Labs

Posted on

Fixing Multiple If-Else problem with Chain of Responsibility Pattern

Situation will become unmanageable if we have lot of if else condition in our code, Lets understand problem and then we will see it’s Solution.

Problem with Multiple If Conditions
Using multiple if-else conditions to handle different cases can lead to code that is hard to read, maintain, and extend. Here’s a simple example using multiple if conditions in a leave management system.

We will implement below Business Logic for Leave management system

a. IF Leave < = 3, then Team Lead will approve

b. IF Leave < =5, then Project Manager will approve

c. IF Leave > 5, then HR Manager will approve

Image description

Request first go to team lead to check if Request can be handled by Team lead else it will go to Project Manager else request will go to HR manager

Example with Multiple If Conditions

public class LeaveRequest
{
    public string EmployeeName { get; set; }
    public int NumberOfDays { get; set; }
    public string Reason { get; set; }
}

public class LeaveApprovalService
{
    public void ApproveLeave(LeaveRequest request)
    {
        if (request.NumberOfDays <= 3)
        {
            Console.WriteLine($"Leave request by {request.EmployeeName} for {request.NumberOfDays} days approved by Team Lead.");
        }
        else if (request.NumberOfDays <= 5)
        {
            Console.WriteLine($"Leave request by {request.EmployeeName} for {request.NumberOfDays} days approved by Project Manager.");
        }
        else if (request.NumberOfDays > 5)
        {
            Console.WriteLine($"Leave request by {request.EmployeeName} for {request.NumberOfDays} days approved by HR Manager.");
        }
        else
        {
            Console.WriteLine("Leave request could not be processed.");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Problems in above code

Maintainability: The method becomes harder to maintain as the logic grows.

Single Responsibility Principle (SRP): The method does too much by handling all approval levels, violating the single responsibility principle.

*Testing * : Each time we add new condition we need to test end to end to make sure nothing is breaking because we are making changes into existing class because we are breaking SRP.

Solution with Chain of Responsibility Pattern

Please click here for fixing this problem

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay