Can we replace the if statement with an object in C #

The following is the method:

private RiskFactor calculateMotoristRiskFactor() { if (motorist.PointsOnLicense > 3 || motorist.Age < 25) return RiskFactor.HighRisk; if (motorist.PointsOnLicense > 0) return RiskFactor.ModerateRisk; return RiskFactor.LowRisk; } 

I do not need these if statements.

Can a strategy template be used to solve this problem? If so, then I also do not want each method in different polymorphic classes to have an If statement.

RiskFactor is Enum

Any better way to make this more object oriented instead of procedural?

+6
source share
5 answers

Well, you could have List<Tuple<Func<Motorist, bool>, RiskFactor> :

 var filters = new List<Tuple<Func<Motorist, bool>, RiskFactor> { Tuple.Create(m => m.PointsOnLicense > 3, RiskFactor.HIGH_RISK), Tuple.Create(m => m.Age < 25, RiskFactor.HIGH_RISK), Tuple.Create(m => m.PointsOnLicense > 0, RiskFactor.MODERATE_RISK), }; 

Then:

 var risk = filters.Where(filter => filter.Item1(motorist)) .Select(filter => filter.Item2) .DefaultIfEmpty(RiskFactor.LOW_RISK) .First(); 

This at least makes it easy to add extra checks, and it just knocks them down in order. This is a bit strange - I could create my own Filter type, not Tuple , for example, but it should work ...

+5
source

I do not think that the strategy template should be the answer here - as a rule, you would use a strategy if the particular type of motorist with whom you deal affects the behavior.

In your case, you have a standard domain logic based on the properties of a motorist. I think conditional processing is very appropriate.

+2
source

RiskFactor function RiskFactor . Instead of a consumption code for this function, encapsulate the logic that requires its return value in separate classes that perform the task in different ways.

For example, you can define an abstract Policy class that inherits from LowRiskPolicy , ModerateRiskPolicy and HighRiskPolicy . They simply return values ​​calculated in accordance with the relevant policy, and the consumption code does not know or does not care about what kind of policy they are. All political logic ends with politics.

+1
source

I don’t think you need to be object oriented just for more object oriented orientation. If this is only the place in the code where you calculate the risk, and I assume that there is a quantity of other data calculated on the basis of age and license points, I would leave your code as it is.

Except, perhaps, passing Motorist as a parameter to a method of the RiscCalculator class.

Just in case, a similar example is here Replace conditional with polymorphism

0
source

You will have much more code, but this will allow you to take it into account. You can create a policy class for each type of risk factor and put them together. Something like that:

  public abstract class RiskPolicy { public abstract RiskFactor Risk { get; } } public class PointsPolicy : RiskPolicy { private readonly int _points; public PointsPolicy(int points) { _points = points; } public override RiskFactor Risk { get { return _points > 3 ? RiskFactor.HIGH_RISK : _points > 0 ? RiskFactor.MODERATE_RISK : RiskFactor.LOW_RISK; } } } public class AgePolicy : RiskPolicy { private readonly int _age; public AgePolicy(int age) { _age = age; } public override RiskFactor Risk { get { return _age < 25 ? RiskFactor.HIGH_RISK : RiskFactor.LOW_RISK; } } } 
0
source

Source: https://habr.com/ru/post/893815/


All Articles