One of the more powerful ways to make a program readable is to break the calculations up into intermediate values that are held in variables with meaningful names. - Robert C. Martin, Clean Code, A Handbook of Agile Craftsmanship
Having worked on many line of business and some finance applications authors have observed a common difficulty of understating and communicating non-trivial business logic or calculations.
Validating the correctness of financial calculations, like tax calculations for example, involves a lot of effort on analyst or tester side. For a developer pinpointing problems from just the final result is challenging, thus often we resort to manual debugging.
TDD falls short of communicating more complex test cases to business people as it relies on quite granular decomposition. Splitting up some algorithms is not always a good option too, due to loss of the original conciseness. Troubleshooting calculations happening in a production environment is impossible without deploying some manual logging that makes code noisy and less readable.
This project attempts to provide a clean way to get an "X-ray" of calculations or business logic.
Spend minutes of programming to save hours of debugging and testing.
- Number (Decimal) and Condition (Boolean) datatype are supported,
- Seamlessly use C# math and logic operators,
- Build isolated calculation components,
- Close to no boilerplate code,
- Serialize calculation tree to JSON,
- Generate DOT graph output of your calculations,
- Render graph as an image.
Fluent.Calculations can be installed using the Nuget package manager or the dotnet
CLI.
dotnet add package Fluent.Calculations.Primitives
Basic demo calculation:
public class Demo : EvaluationScope<Number>
{
Number
A = Number.Of(1),
B = Number.Of(2),
C = Number.Of(3);
Number D => Evaluate(() => A + B);
Number E => Evaluate(() => D * C);
public override Number Return() => E;
}
Number result = new Demo().ToResult();
The result
value can be :
- Serialized to JSON (example: fluent-calculation-demo.json),
- Converted to DOT Language (example: fluent-calculations-demo.dot),
- Rendered using DOT rendering programs and utilities.
- Explore ways to introduce concept of units (Meters, Kilograms, etc.).
- Expand a list of supported operations and math functions.
- Explore ways to optimize lambda expression compilation.
- Explore a ways to reuse existing calculations and attempt to benefit from :
- Cache compiled expressions,
- Cache evaluation results,
- Partial execution depending on changed parameters.
- Explore thread-safety aspects.
GNU General Public License v3.0
We are bunch of .NET practitioners always looking for ways to make code great.
Loving it? Show your support by giving this project a star!