7 Reasons to build and use Small Composable Rules
Essential knowledgeCore knowledge to understand Fluent Commerce
Author:
Lesley Dean
Changed on:
7 Aug 2024
Overview
Small composable rules enable composition of business logic. This is important to maintain flexibility and minimise the cost of change over time. These are 2 of the most common reasons our customers choose Fluent Order Management!So why is this so important? In this first article in the “Composable Rules Series”, I present “the why”. We’ll explore 7 reasons for building small composable rules.By the end of this article, you will understand the benefits for both developers and our customers.Key points
- Small Composable Rules:
- Deliver an improved developer experience, by keeping things simple, easy to test and debug
- Are more self-explanatory in their job to be done
- And allow you to focus first and foremost on the harder more valuable problems to solve
- They also deliver more value to our customers:
- Deliver more flexibility, agility, and adaptability
- Minimise extension cost
- Reduce total cost of ownership (TCO)
- Increase return on investment (ROI)
Introduction
Small composable rules enable composition of business logic. This is important to maintain flexibility and minimise the cost of change over time. These are 2 of the most common reasons our customers choose Fluent Order Management!So why is this so important? In this first article in the “Composable Rules Series”, I present “the why”. We’ll explore 7 reasons for building small composable rules.By the end of this article, you will understand the benefits for both developers and our customers.In short, the 7 reasons (and I am sure there are more) to build and use small composable rules are:- They are quicker and easier to write and test
- They are simpler to name, and more self-explanatory
- They provide richer audit logging, resulting in improved traceability and debugging
- They increase configuration over code, enabling greater flexibility
- They are more reusable, reducing TCO, and increasing ROI
- They decrease extension costs
- They make you more productive
#1 They are quicker and easier to write and test
This might seem obvious, but the smaller the unit of code, the quicker it is to write it and test it. Now don’t get me wrong, in certain areas of software development, even a few lines of code can represent, what some might consider to be, “rocket science”. These may be the result of significant research, testing, and time. I’m talking about advanced algorithms and lower level computer science problems.While there are times when a more advanced rule may be required, this is not the majority of use cases in the order management domain. In general, every requirement can be broken down in to small steps. The power of the workflow engine is its ability to execute each step of the process efficiently to solve for the requirement.The more code paths there are in a single rule, the more complex it is to test and debug. More tests are required to cover all code paths.So, smaller pieces of code are quicker and easier to write and test. And each rule then has one very clear and concise job to be done. It has fewer code paths to assert for, and therefore fewer opportunities for fragility.Tip:If you want to know if your Rules might be doing too much, read “7 signs your Rules are doing too much” [TODO: Link].#2 They are simpler to name, and more self-explanatory
Smaller Rules are also easier to name - they do one very small specific thing. Rule names should be as intuitive as possible. Ideally, you should not need to read the Rule description (or worse, the code) to understand what it does. Consider the following 2 examples:- ForwardIfDeliveryAddressPresent - Forwards the source event if the order's delivery address is present
- VerifyingFulfilmentItems - Verifies the quantity (not a number, quantity here refers to ALL, PARTIAL or NONE) of items fulfilled in the incoming fulfilment entity and sends an event with corresponding event name when it matches the quantity from the input parameter.
#3 They provide richer audit logging, resulting in improved traceability and debugging
The Fluent OMX Workflow Engine logs an Orchestration Audit Event for each Rule, Action, and Ruleset. This means that when your Rules are more granular, so will your audit event logs. This makes a significant difference when identifying issues with certain orders or processing.With large monolithic rules, debugging becomes complex as these code paths are not reflected in the standard Audit Log Events. It essentially becomes a black box, where the inner workings are hidden.When an exception might occur in the middle of one of the many code paths in the rule, the Audit Event will only present is the name of the rule with the exception.If there were only 1 or 2 code paths in the rule, it would be much quicker and easier to identify why it failed, and resolve the issue.Since smaller rules have more meaningful names, it also makes it easier for non-developers to understand. It may even help them identify and resolve the issue without the help of a technical person.#4 They increase configuration over code, enabling greater flexibility at lower cost
Rules are the building blocks for business logic. Not the business logic themselves. Rules compose together into Rulesets to form units of logic, such as a task. Rulesets compose together to fulfil a function or process.Configuration of business logic happens at multiple levels:- Configuration of Rule parameters
- Composition and sorting of Rules within a Ruleset
- Composition of Rulesets within a Workflow
A Fraud Check configured by composition of Core Reference Module RulesLater, based on analysis of their data, they realise most Orders over $100 are ok from existing customers, so they ask for a change to the fraud checking logic. They ask that only Orders over $100 for customers with less than 2 successfully completed Orders in the past 6 months, get flagged for a fraud check.To implement this logic, a new Rule would be required to query Orders for the current Customer reference where the Order status is COMPLETE.There is no need to change the existing Core Rules code. Rather one or more new Rules could be written, and configured within the workflow like so:
Fraud Check Logic composed of Core Rules and New RulesPerhaps later, the client realises that while the customer may have more than 2 completed orders, they also return a lot. In this case they would like to add another condition to the fraud check. This time they ask that any Orders over $100, where the customer has more than 1 return, and less than 5 completed orders gets flagged for fraud check. Again, only the new Rules are written and then configured into the workflow:
Fraud Check Logic composed of Core Rules and New RulesSmaller composable rules mean there are more unique solutions possible with the same set of building blocks.Large monolithic rules are much more difficult to adapt and change, and therefore more costly.Fluent Commerce customers need the ability to continuously adapt to their market. The Fluent Platform is designed to empower customers to do this without significant development costs each time they want to change or extend their business logic.#5 They are more reusable, reducing TCO, and increasing ROI
Smaller composable rules tend to be more versatile. They can be combined with a number of other rules to produce unique functions or tasks within a process. The Fraud Check example in the previous point demonstrates this well. The same Rules are used multiple time with different parameters.This reusability prevents the need to write specific rules each time the function or task is required. This reduces the total cost of ownership, and continuously increases the return on investment.For example, the Core Reference Module provides a number of Rules which are flexible to be used on any orchestration entity, and perform various actions, such as setting state, or sending events.Another example could be a Rule like “SetAttributeIfPropertyEquals”. This Rule could set the value of an Attribute on the current entity if a property (configured by JSON Path) equals a provided conditional value.This rule could be used to set attributes on any entity, making it reusable across domains. Like many of the Core Rules, the JSON Path could be configured to compare against an entity field or an event field. This helps to prevents duplication of code, reducing maintenance costs, which further improves TCO and ROI.#6 They decrease extension costs
There are 2 aspects to how small composable rules decrease extension cost:- With smaller composable rules, less new rules should be required. This is because of all the benefits of composability described in the previous points. This means it costs less to extend the implementation, through the reuse of the existing rules.
- When new rules are needed, the missing piece should, theoretically, be quite small. Of course this depends on the richness of the existing library, but in theory, should be true. If the workflow is built from small composable rules, the missing pieces should be smaller.
#7 They make you more productive!
As a developer, you are paid to deliver solutions to problems. Solutions that hold intrinsic value to someone or something. The more value you can deliver, the more valuable you are.The opportunities for delivering more value as a developer are simple:- Spend less time on the basic fundamentals
- Leverage tools and processes that optimise and reduce the repetitive low value work
- Spend time where your skillset is really needed - in the hard problems
- The ability to fulfil orders from any location
- Prefer the closest to the customer
- Deliver on time
- Product value
- Cross-boarder constraints
- Shallow stock levels
- Protecting the in store customer experience
- and so on…
Conclusion
The benefits of building and using small composable rules are clear (I hope!). But sometimes it is harder to break something down into smaller reusable parts.It can feel quicker to just write the solution in code. And this gives a greater sense of reward and satisfaction when “it works”. Yet in the long term, there are costs or pain that may result from this approach.Later when you’re trying to debug an intermittent problem, you realise you can’t easily trace the code path taken. Perhaps you didn’t think to write a test case for this scenario at the time, and it takes you days to find!Thats the pain and the cost.What’s more is once you fix this problem, you may find you have broken something else in the process, and it starts to snowball.So, in conclusion, smaller composable rules:- Deliver an improved developer experience, by keeping things simple, easy to test and debug
- Are more self-explanatory in their job to be done
- And allow you to focus first and foremost on the harder more valuable problems to solve
- Deliver more flexibility, agility, and adaptability
- Minimise extension cost
- Reduce total cost of ownership (TCO)
- Increase return on investment (ROI)
Except as otherwise stated in the Extend Knowledge Content site policy, the content on this page is licensed under the Creative Commons Attribution 4.0 Licence, and any code samples that appear on this page are licensed under the Apache 2.0 Licence, unless any code sample forms part of the Fluent Order Management Platform code. Neither of these licences apply to any content on any other page that can be reached via a link on this page unless otherwise specified on that other page. If you wish to use any of the Extend Knowledge Content, you must do so in compliance with the licenses referred to above and the Extend Knowledge Content site policy, including attribution in the manner set out on this page.
