C
C#2y ago
Maskoe

❔ 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
Becquerel
Becquerel2y ago
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:
public class Adjustment
{
Func<bool> Applies;

Func<int, int> Calculate;

string Description;
}

var originalValue = 10;

var adjustments = new List<Adjustment>()
{
new()
{
Calculate = x => x = x + 25,
Applies = () => SomeCondition(),
Description = "example"
}
};

foreach (var adjustment in adjustments)
{
if (adjustment.Applies())
{
originalValue = adjustment.Calculate(originalValue);
Console.WriteLine(adjustment.Description);
}
}
public class Adjustment
{
Func<bool> Applies;

Func<int, int> Calculate;

string Description;
}

var originalValue = 10;

var adjustments = new List<Adjustment>()
{
new()
{
Calculate = x => x = x + 25,
Applies = () => SomeCondition(),
Description = "example"
}
};

foreach (var adjustment in adjustments)
{
if (adjustment.Applies())
{
originalValue = adjustment.Calculate(originalValue);
Console.WriteLine(adjustment.Description);
}
}
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
Maskoe
Maskoe2y ago
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
ffmpeg -i me -f null -
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
Maskoe
Maskoe2y ago
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") everywhere
ffmpeg -i me -f null -
what 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
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server
More Posts