❔ Smart way to code an insurance pricing calculation with explanation?
I'm refactoring a ~60% correct pricing method. To give you an idea its currently like 1000 lines of c# code, 1000 lines of sql queries and maybe about 7 involved tables.
The code and math is actually not heavily complicated. Its just a LOT of
"if there is a contract for this item in that table, its prio1. Otherwise look for an old contract in the other table with column = true, thats prio2, column = flase is prio3..."
"If there is a discount, apply the discount from that table, but only up to this total"
The heaviest mathemathical operation is actually just percentages.
Its important for users to see how the app ended up at the final number.
All I can think of is basically doing it manually. For every line of math, i just add an explanation after like this:
if(contract.AllowDiscount && discountTotal < allowedDiscount)
price = price * (1-contract.Discount)
Console.WriteLine("Discount is below {allowedDiscount}. Reducing price by {contract.Discount}%")
6 Replies
first things first is get it under test!
unit tests, integration tests, whatever
make sure you actually know what the correct logic should be before changing stuff
re: actual architecture, how about something like this:
encapsulate the idea of discounts or other operations on the value, whether or not you should do the operation, and a description of it in one class
hmm i think that only works in theory
if it was literally 800 if statements yea
but they start to be nested
and then you need an else if and an else etc
and you end up just writing a programming language
and you need to pass variables etc
well you have to decide how to model these operations one way or another, if you want to simplify this stuff
if you already know a how this can be done, good
if not you start with a generic model and then simplify it, especially if they have to remain data -- and not have flow operations in them
in a way these are just logs or traces right?
no one writes their tracing like that. people just bite the bullet and throw in
logger.LogTrace("did xy")
everywherewhat i would do is using a list operation classes, and then having a separate service with string and translations for putting objects to text
but again, there's to understand the linearity and nestedness of the operations involved
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.