πŸš€ Say Goodbye to If-Else in C#: Use Design Patterns for Cleaner Code!

 

πŸ™‹‍♂️ Why Do Developers Use Too Many If-Else Conditions?

If you've ever written C# code, you've probably used if-else statements like this:


public void ProcessLead(string leadSource) { if (leadSource == "Website") { Console.WriteLine("Processing Website Lead..."); } else if (leadSource == "Referral") { Console.WriteLine("Processing Referral Lead..."); } else if (leadSource == "Event") { Console.WriteLine("Processing Event Lead..."); } else { Console.WriteLine("Unknown Lead Source"); } }

πŸ”΄ What's Wrong with This?

  • Hard to Maintain: Every time a new lead type is added, you need to modify the method.
  • Violates Open-Closed Principle (OCP): The method is not open for extension but open for modification.
  • Not Scalable: Imagine having 10+ conditions—the code becomes unmanageable!

✅ Solution 1: Strategy Pattern (Best for Business Logic)

Instead of using multiple if-else conditions, we can separate each logic into its own class using the Strategy Pattern.

πŸ”Ή Step 1: Create an Interface for Lead Processors

public interface ILeadProcessor { void Process(); }

πŸ”Ή Step 2: Implement Different Lead Types as Separate Classes

public class WebsiteLeadProcessor : ILeadProcessor { public void Process() => Console.WriteLine("Processing Website Lead..."); } public class ReferralLeadProcessor : ILeadProcessor { public void Process() => Console.WriteLine("Processing Referral Lead..."); } public class EventLeadProcessor : ILeadProcessor { public void Process() => Console.WriteLine("Processing Event Lead..."); }

πŸ”Ή Step 3: Use a Factory or Dictionary to Select the Right Class

Instead of a long if-else chain, we map lead sources to the correct processor dynamically.

public class LeadProcessorFactory { private static readonly Dictionary<string, ILeadProcessor> _processors = new Dictionary<string, ILeadProcessor> { { "Website", new WebsiteLeadProcessor() }, { "Referral", new ReferralLeadProcessor() }, { "Event", new EventLeadProcessor() } }; public static ILeadProcessor GetProcessor(string leadSource) { return _processors.ContainsKey(leadSource) ? _processors[leadSource] : new DefaultLeadProcessor(); } } public class DefaultLeadProcessor : ILeadProcessor { public void Process() => Console.WriteLine("Unknown Lead Source"); }

πŸ”Ή Step 4: Use the Factory in Your Code


public void ProcessLead(string leadSource) { var processor = LeadProcessorFactory.GetProcessor(leadSource); processor.Process(); }

🎯 Now, adding a new lead type is easy!

  • Just create a new class (e.g., SocialMediaLeadProcessor).
  • Add it to the dictionary in the factoryno need to modify existing code! πŸŽ‰

✅ Solution 2: Dictionary-Based Approach (Simpler Alternative)

If your logic is simple (e.g., just printing messages), a Dictionary can replace if-else completely.

csharp

public void ProcessLead(string leadSource) { var leadProcessors = new Dictionary<string, Action> { { "Website", () => Console.WriteLine("Processing Website Lead...") }, { "Referral", () => Console.WriteLine("Processing Referral Lead...") }, { "Event", () => Console.WriteLine("Processing Event Lead...") } }; if (leadProcessors.TryGetValue(leadSource, out var action)) { action(); } else { Console.WriteLine("Unknown Lead Source"); } }

πŸ”Ή Advantages of this Approach:
Cleaner Code – No nested if-else.
Faster Execution – Direct lookup in a Dictionary.
Easy to Extend – Just add a new entry!


🎯 Which Solution Should You Use?

SolutionWhen to Use
Strategy Pattern   When each condition has complex logic and needs separate classes
Dictionary-Based Approach   When conditions just return simple values or small actions

πŸš€ Final Thoughts

πŸŽ‰ No more messy if-else chains!

  • Use Strategy Pattern for complex, scalable solutions.
  • Use Dictionary for quick and simple condition handling.

πŸ’‘ What do you think? Have you replaced if-else in your projects? Let me know in the comments! 😊

Comments

Popular posts from this blog

πŸ€– Copilot vs Microsoft Copilot vs Copilot Studio: What’s the Difference?

Understanding Auto-Numbering in a Multi-Transaction System

Integrating Dynamics 365 CRM with MuleSoft Using a Synchronous C# Plugin