Home > front end >  How do I avoid repetitive code in the case of switch statements which are the same but for some subs
How do I avoid repetitive code in the case of switch statements which are the same but for some subs

Time:09-04

The following snippet is from an inventory system I'm working on. I keep on running into scenarios where I fell I should be able to simple run a for loop, but am stymied by the fact that in different cases I'm using different vectors/variables/etc. I run into this problem just about any time I need to work with a variable or object who's name won't be known at run-time. In this particular situation, case 1: is exactly the same as case 2: except that the vector tankInventory[] would be dpsInventory[] in case 2:

I feel I'm doing something fundamentally backwards, but I'm not clear on how to reorient my thinking about this. Any advice?

case 1:
    //loop through the inventory...
    for (int i = 0; i < 6; i  )
    {
        //looking for an empty spot
        if (tankInventory[i] == -1)
        {
            //add the item...
            tankInventory[i] = { item };
            //decrement the number of items being added
            number--;
            //and stop the loop if you're out of items to add
            if (!number)
                break;
        }
    }
    //if there are no more items to add, break;
    if (!number)
        break;
    //but if there are more...
    else
    {
        //switch to main inventory...
        character = 0;
        //and return to the top
        goto returnPoint;
    }

CodePudding user response:

Use a function.

Just extract the common logic out into a function, and take as parameters whatever can change.

Also, it seems like you're using goto and breaking out from the switch instead of doing a loop. I'd do something like do {} while (number) or while (number) {}, depending on what you need. This way it's much easier to use a function.

CodePudding user response:

You're very likely on the right track, this is how we build up the abstractions. A simple way is to define a lambda:

// you might refine the captures
auto processInventory = [&](auto& inventoryToProcess) {
    //loop through the inventory...
    for (int i = 0; i < 6; i  )
    {
        //looking for an empty spot
        if (inventoryToProcess[i] == -1)
        {
            //add the item...
            inventoryToProcess[i] = { item };
            //decrement the number of items being added
            number--;
            //and stop the loop if you're out of items to add
            if (!number)
                break;
        }
    }
    //if there are no more items to add, break;
    if (!number)
        break;
    //but if there are more...
    else
    {
        //switch to main inventory...
        character = 0;
        //and return to the top
        goto returnPoint;
    }}
};

switch(condition) {
case 1:
    processInventory(tankInventory);
    break;
case 2:
    processInventory(dpsInventory);
}
  •  Tags:  
  • c
  • Related