1
\$\begingroup\$

I'm just wondering if there is a better way of executing multiple methods depending on a value?

I have a Property that gets the NumberOflegs. NumberOfLegs can have a value between 1-6. If the value is 1, it needs to execute InsertLeg1Details. If the value is 2, it needs to execute InsertLeg1Details and InsertLeg2Details, etc.

  private void SaveLegs()
    {
        switch (NumberOfLegs)
        {
            case 1:
                SaveDataALC.SaveFlight(InsertLeg1Details());
                break;
            case 2:
                SaveDataALC.SaveFlight(InsertLeg1Details());
                SaveDataALC.SaveFlight(InsertLeg2Details());
                break;
            case 3:
                SaveDataALC.SaveFlight(InsertLeg1Details());
                SaveDataALC.SaveFlight(InsertLeg2Details());
                SaveDataALC.SaveFlight(InsertLeg3Details());
                break;
            case 4:
                SaveDataALC.SaveFlight(InsertLeg1Details());
                SaveDataALC.SaveFlight(InsertLeg2Details());
                SaveDataALC.SaveFlight(InsertLeg3Details());
                SaveDataALC.SaveFlight(InsertLeg4Details());
                break;
            case 5:
                SaveDataALC.SaveFlight(InsertLeg1Details());
                SaveDataALC.SaveFlight(InsertLeg2Details());
                SaveDataALC.SaveFlight(InsertLeg3Details());
                SaveDataALC.SaveFlight(InsertLeg4Details());
                SaveDataALC.SaveFlight(InsertLeg5Details());
                break;
            case 6:
                SaveDataALC.SaveFlight(InsertLeg1Details());
                SaveDataALC.SaveFlight(InsertLeg2Details());
                SaveDataALC.SaveFlight(InsertLeg3Details());
                SaveDataALC.SaveFlight(InsertLeg4Details());
                SaveDataALC.SaveFlight(InsertLeg5Details());
                SaveDataALC.SaveFlight(InsertLeg6Details());
                break;
        }
    }
\$\endgroup\$
2
  • 1
    \$\begingroup\$ The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How do I ask a good question?. \$\endgroup\$
    – BCdotWEB
    Commented Apr 30, 2022 at 18:25
  • \$\begingroup\$ Sounds like a perfect opportunity to combine a lazy generator with Linq's Take() method \$\endgroup\$
    – D. Jurcau
    Commented May 3, 2022 at 9:19

3 Answers 3

3
\$\begingroup\$

With reflection you can do something like this

Type thisType = this.GetType();
for(int i = 1; i <= NumberOfLegs; i++)
{
  string toBeCalledMethodName = $"InsertLeg{i}Details";
  MethodInfo toBeCalledMethod = thisType.GetMethod(toBeCalledMethodName);

  var details = (FlightDetails)toBeCalledMethod.Invoke(this);
  SaveDataALC.SaveFlight(details);
}

I assumed your InsertLegXYZ methods are returning FlightDetails instances based on your previous question.

\$\endgroup\$
2
\$\begingroup\$

You should strive to avoid repeating operations like that.

My suggestion would be to test at each stage whether there's more work to do. If we've finished, then return.

private void SaveLegs()
{
    if (NumberOfLegs < 1 || NumberOfLegs > 6) { return; }

    SaveDataALC.SaveFlight(InsertLeg1Details());
    if (NumberOfLegs == 1) { return; }

    SaveDataALC.SaveFlight(InsertLeg2Details());
    if (NumberOfLegs == 2) { return; }

    SaveDataALC.SaveFlight(InsertLeg3Details());
    if (NumberOfLegs == 3) { return; }

    SaveDataALC.SaveFlight(InsertLeg4Details());
    if (NumberOfLegs == 4) { return; }

    SaveDataALC.SaveFlight(InsertLeg5Details());
    if (NumberOfLegs == 5) { return; }

    SaveDataALC.SaveFlight(InsertLeg6Details());
}

Assuming you have control over the functions, consider replacing (or augmenting) them with a single function that takes a leg number as argument - the code then becomes a simple loop:

private void SaveLegs()
{
    for (int i = 1;  i <= NumberOfLegs;  ++i) {
        SaveDataALC.SaveFlight(InsertLegDetails(i));
    }
}

Here, I've assumed that InsertLegDetails() will respond appropriately (e.g. throw an exception) if its argument is too high - the original action of just ignoring all the data seems a poor choice to me.

\$\endgroup\$
1
\$\begingroup\$

Why are there InsertLeg<N>Details()s in the first place?

Alternative assuming order of execution insignificant, not quite Duff's device:

switch (NumberOfLegs)
{
    case 6:
        SaveDataALC.SaveFlight(InsertLeg6Details());
        goto case 5;
    case 5:
        SaveDataALC.SaveFlight(InsertLeg5Details());
        goto case 4;
    case 4:
        SaveDataALC.SaveFlight(InsertLeg4Details());
        goto case 3;
    case 3:
        SaveDataALC.SaveFlight(InsertLeg3Details());
        goto case 2;
    case 2:
        SaveDataALC.SaveFlight(InsertLeg2Details());
        goto case 1;
    case 1:
        SaveDataALC.SaveFlight(InsertLeg1Details());
        break;
}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Fallthrough in C# need to be written differently compared to C/C++ stackoverflow.com/questions/174155/… \$\endgroup\$
    – D. Jurcau
    Commented May 3, 2022 at 9:37
  • \$\begingroup\$ (The main idea of making this one-idea "review" community wiki was to enable every contributor to edit it. (You will have to decide whether to touch the reopen&close queues RSN.)) \$\endgroup\$
    – greybeard
    Commented May 3, 2022 at 10:52
  • \$\begingroup\$ (continue might have read more fluently in place of "the goto <switch label>;"s - and would have exacted a casuistic definition.) \$\endgroup\$
    – greybeard
    Commented May 3, 2022 at 11:11
  • \$\begingroup\$ I'm guessing that the order of execution is likely to be significant here - but who knows, when we've been shown just a single function out of context? \$\endgroup\$ Commented May 7, 2022 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.