Refactor my code: Conditions based on different variables

Given:

internal void Configure(ButtonEventArgs args, IBroker broker, FunctionEntry entry)
        {
            int phase = broker.TradingPhase;

            if (args.Button == ItemType.SendAutoButton)
            {                              
                if (phase == 1)
                {
                    entry.SetParameter("ANDealerPrice", -1);
                    entry.SetParameter("ANAutoUpdate", 4);
                }
                else if (phase == 2)
                {
                    entry.SetParameter("ANDealerPrice", -1);
                    entry.SetParameter("ANAutoUpdate", 2);
                }
            }

            if (phase == 1)
            {
                if (broker.IsCashBMK)
                {
                    entry.SetParameter("Value", 100);

                }
                else if (broker.IsCross)
                {

                    entry.SetParameter("Value", 200);

                }
            }
}

I am looking for suggestions for refactoring the code above. As Fowler suggested: “Replace condition with strategy / polymorphism,” I cannot implement efficient code on this line. Since there are several conditions, they are based on variables.

Please suggest if there could be a template that could eliminate these error-prone and ugly conditions (code smell).

Thank you for your interest.

Edit: 1) My intention is to apply the Open-Closed principle here, so if tomorrow, if there is a change in the logic, I can expand these conditions by introducing a new class. 2) Please do not pay attention to magic numbers, in a real scenario I have a valid constant / source for them.

+3
5

, , , , :

void SetAnDealerPrice(ButtonEventArgs args, IBroker broker,
        FunctionEntry entry) {
    if (args.Button != ItemType.SendAutoButton)
        return;
    int phase = broker.TradingPhase;
    if (phase == 1 || phase == 2)
        entry.SetParameter("ANDealerPrice", -1);
}

void SetAnAutoUpdate(ButtonEventArgs args, IBroker broker,
        FunctionEntry entry) {
    if (args.Button != ItemType.SendAutoButton)
        return;
    switch (broker.TradingPhase) {
    case 1:
        entry.SetParameter("ANAutoUpdate", 4);
        break;
    case 2:
        entry.SetParameter("ANAutoUpdate", 2);
        break;
    }
}

void SetValue(IBroker broker, FunctionEntry entry) {
    if (broker.TradingPhase != 1)
        return;
    entry.SetParameter("Value", broker.IsCashBMK ? 100 : 200);
}

( ) ( , , , , ). , , ( , , ), , . , , , .

+2

, , , . , .

, .

Demeter.

. , , , .

+2

, , , , , , , , .

, : GetBrokerPrice SetPriceOptions

+2

:

  • else: ifs .
  • if, if (phase == ...) .

, if-, if (phase == 1), , , if (phase == 1) , .

.

internal void Configure(ButtonEventArgs args, IBroker broker, FunctionEntry entry) 
{ 
        int phase = broker.TradingPhase; 

        if (phase == 1) 
        {                               
            if (args.Button == ItemType.SendAutoButton)
            { 
                entry.SetParameter("ANDealerPrice", -1); 
                entry.SetParameter("ANAutoUpdate", 4); 
            }
        }
        if (phase == 2) 
        {                               
            if (args.Button == ItemType.SendAutoButton) 
            { 
                entry.SetParameter("ANDealerPrice", -1); 
                entry.SetParameter("ANAutoUpdate", 2); 
            } 
        } 
        if (phase == 1) 
        { 
            if (broker.IsCashBMK) 
            { 
                entry.SetParameter("Value", 100); 

            }
        }
        if (phase == 1)
        { 
            if (broker.IsCross) 
            { 

                entry.SetParameter("Value", 200); 

            } 
        } 

}

if-. List<MyAction>. - , :

internal void Configure(ButtonEventArgs args, IBroker broker, FunctionEntry entry)  
{
      foreach(var action in MyActions)
      {
           action(args, broker, entry);
      }  
 }
internal void PopulateMyActions()
{
      // Hopefully I have not made a syntax error in this code...
      MyActions.Add( (ButtonEventArgs args, IBroker broker, FunctionEntry entry) =>
         {
            if (broker.TradingPhase == 1) 
            {                               
                if (args.Button == ItemType.SendAutoButton)
                { 
                    entry.SetParameter("ANDealerPrice", -1); 
                    entry.SetParameter("ANAutoUpdate", 4); 
                }
            }
        } );
      // And so on
}

== 1 == 2 action:

internal void Configure(ButtonEventArgs args, IBroker broker, FunctionEntry entry)  
{
      int phase = broker.TradingPhase;
      foreach(var action in MyActions[phase])
      {
           action(args, entry);
      }  
 }

internal void PopulateMyActions()
{
      // Hopefully I have not made a syntax error in this code...
      MyActions[1].Add( (ButtonEventArgs args, FunctionEntry entry) =>
         {
            if (args.Button == ItemType.SendAutoButton)
            { 
                entry.SetParameter("ANDealerPrice", -1); 
                entry.SetParameter("ANAutoUpdate", 4); 
            }
        } );
      // And so on
}

, , phase .

action(args, entry) action(args.Button, entry), , .

, . . . Presto: !

PS: , . , , MyActions ..

+2

, FunctionEntry.
, FunctionEntry .
Configure().
, ItemType ButtonEventArgs IBrkoer FunctionEntry if-else.

if-else, .
- , , .
, : , , . , .

+1

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


All Articles