D
D
ddd3292019-12-11 05:26:06
Software design
ddd329, 2019-12-11 05:26:06

How to get rid of multiple if/else?

Good day!
There is something like this pseudo-code for some calculations:

IF Условие 1 THEN
    Делаем расчеты первым методом
ELSE IF Условие 2 THEN
    Делаем расчеты вторым методом
ELSE IF Условие 3 THEN
    Делаем расчеты третьим методом
ELSE
    Здесь обращаемся к базе данных и находим последнее валидное значение
END

The bottom line is that you need to do the calculations. Let's choose the first method for this, if it didn't work out first, then you need to try the second, otherwise try the third, but if all the methods did not bring successful results, then you need to contact the Database and find the last successful value there.
There can be about 20 such if / else in the code, and here there is a mixture of different logic: business logic and the logic of accessing the database.
Do you know how you can "beautifully" organize such code? Are there any design patterns that can be applied?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Adamos, 2019-12-11
@ddd329

class CalculationData;

abstract class Calculation 
{
  public function calculate(CalculationData data);
}

class CalculationQueue 
{
  public function addCalculation(Calculation calculationVariant, int priority);

  public function calculate(CalculationData data) {     
     for (c in calculations) {
       if(res = c.calculate(data)) return res;
     }
  }
}

A
antonwx, 2019-12-11
@antonwx

The obvious switch . Available in most languages.

S
Sergey Gornostaev, 2019-12-11
@sergey-gornostaev

Usually, a blanket of conditional expressions (especially multi-level ones) is a signal that you can use the state pattern . It is also useful to decompose the code that makes the decision and the code that performs the calculation, that is, return not the result of the calculations, but the behavior that performs them . It's nice to wrap it in a maybe-type so that the calling code can delegate getting the default value to other code.

R
Rsa97, 2019-12-11
@Rsa97

Let's choose the first method for this, if it didn't work out first, then you need to try the second, otherwise try the third, but if all the methods did not bring successful results, then you need to contact the Database and find the last successful value there.
Your code does not match this description. In the above if-else chain, the execution of a method depends not on the result of the previous method, but on a set of certain external conditions. For a chain, you can use
result = tryMethod1() || tryMethod2() || ... || getFromDB();

L
lJser, 2019-12-11
@SteelJames

it all depends on how the conditions are arranged and the actions performed.
Without this specificity, there can be no answer, except for the obvious switch

A
Antonio Solo, 2019-12-14
@solotony

here it depends on what kind of conditions
if the condition is a comparison with a constant, and there are a lot of such conditions, I would use a dictionary (associative array) in the values ​​\u200b\u200bof which there would be pointers to a function
, if the conditions themselves are some kind of calculations, then of course you can’t do without enumeration .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question