...

/

Implementing Complex Conditional Logic

Implementing Complex Conditional Logic

Learn which design patterns to use when implementing complex conditional logic.

Problem statement

Let’s suppose that we need to implement either a switch statement with several cases or an if-else logic with several conditions.

A traditional way of dealing with this is just to implement specific code under each condition. However, if we’re dealing with complex conditional logic, our code will become harder to read and maintain.

If we have this conditional logic inside a single method, it’ll probably become really difficult to write unit tests for this method. Our code will probably violate SOLID principles because the method will have more than one responsibility. It’ll be responsible for both making conditional decisions and executing different types of logic based on those decisions. We might end up having as many responsibilities inside this method as there are conditions in our statement!

...