Breaking the if-else logic trap with the Rule-based design pattern
Navjot Bansal
Building Computer Vision Systems @Oracle | Software Architecture | System Design | ICPC Regionalist
Overview
There are situations where you are presented to deal with legacy code or work upon modules that require you to return responses by matching the input on certain conditions this is a common situation represented by each software engineer in their day-to-day life.
We often choose to use if-else statements or switch statements to solve such problems. This approach is fine if you have like 1-2 conditions to deal with. But what if we have multiple conditions?
Bob likes to call this situation an if-else trap. Because if you have a lot of conditions to deal with there is a high chance these conditions will further increase as functionality increases and will make our code a piece of art "The Moaning Lisa".
Bob's use case
Bob's manager asked him to create a functionality that returns the status message for an asynchronous job triggered on some trivial conditions. The workers handling the job has complete information. The flow of requests is depicted as
For our convenience, we will keep the request in 4 states represented by ENUM
In order to provide the status messages to customers the request in each state reads the progress from different files which is elaborated in the pseudocode below
Advantages:
Disadvantages:
Rule-based pattern to rescue
Formal Definition
Rules design pattern helps the developer to encapsulate each business rule in a separate object and decouple the definition of business rules from their processing. New rules can be added without the need to modify the rest of the application logic. ?????
领英推荐
Rule-based in Nutshell
For me personally rule based design pattern is an extension of the Factory pattern. The observation I did is that we compare the logic on a similar object (Request in our case) and create a response on the same basis. Instead of returning the response as a string what if we return an object and we can read the response from them?
The end goal we want to achieve should look something like this
The flow would look something like this
The snippets attached below will first implement the RequestFactory with its subclasses and would be utilized by the RequestAdapter in the further snippet
We are done with condition mapping now we need a class that acts as an adapted and helps us map the right object according to the conditions.
Advantages:
Disadvantages:
Conclusion
People usually feel the implementation is code-heavy and takes a lot of time. The benefits of this pattern are usually seen in the long run when the backend code goes through iterative changes and the behavior changes as per the new policies.
Staff Engineer at TRACT | Ex - Tide| Morgan Stanley | Bank Of America
2 年We at Tide use expression language library, while rules sit in a json/yaml file(s) There are times when you have to do attribute preprocessing, but by and large looks way cleaner.
Software Engineer III | Java 8 | Microservices | Spring boot | REST APIs |
2 年Helpful
Senior Data Engineer || Spark, Azure, Python, Databricks, Snowflake, SQL | Building robust, scalable data solutions to fuel business insights
2 年Ken and Ryu ! My favourite characters.
Founder, InterviewReady
2 年Interesting story with Bob. This looks similar to the design pattern commonly known as "state pattern".