简体   繁体   中英

Better way than using multiple if

I have a simple problem: I need to roll two sets of dice. For each number possible in the first roll, there are a set of conditionals that I then check against the second roll. The conditionals for each roll are different.

The problem, to me, is that something like this repeated roughly thirty times seems like a gross thing to stick in my code:

    if (roll == 3) {
        if (secondRoll < 5) {
           do this
        }
        else if (secondRoll > 5 && secondRoll < 10) {
           do this
        }
        else {
           do this
        }
    }

    if ...... and so on

So what do I do? Is there a more elegant way to do this? I've thought about something like this:

    class Result {
        constructor(firstRoll, secondRollMin, secondRollMax, output) {
            this.firstRoll;
            this.secondRollMin;
            this.secondRollMax;
            this.output;
        }
    }

and then checking the rolls against matching properties in a set of objects, but I'm not sure that's honestly any better. Any thoughts would be appreciated.

Just a small improvement, but you only need to check the upper bound of the range in each subsequence else if :

if (roll == 3) {
    if (secondRoll < 5) {
       do this
    }
    else if (secondRoll < 10) {  // secondRoll must be >= 5 already
       do this
    }
    else {
       do this
    }
}

Use Switch to simplify

switch (roll) {
    case 1-4: 
        // Do something.
        break;
    case 5-8: 
        // Do something.
        break;
    case 9-11: 
        // Do something.
        break;
    default:
        break;
}

How about creating a key for the combination and a bit simpler if/else ? You could combine any combination that has the same action.

const combo = `${roll}-${secondRoll}`;

if (['1-1', '1-2', '1-3', '3-4', '3-5', '3-6'].includes(combo) {
  // Do this
} else if (['1-4', '1-5', '1-6', '3-1', '3-2', '3-3'].includes(combo) {
  // Do this
// ...
} else if (['6-4', '6-5', '6-6'].includes(combo) {
  // Do this
}

Or create a switch/case :

const combo = `${roll}-${secondRoll}`;

switch (combo) {
  case '1-1':
  case '1-2':
  case '1-3':
  case '3-4':
  case '4-5':
  case '5-6':
    // Do this
    break;
  case '1-4':
  case '1-5':
  case '1-6':
  case '3-1':
  case '4-2':
  case '5-3':
    // Do this
    break;
  // ...
  case '6-4':
  case '6-5':
  case '6-6':
    // Do this
    break;
}

You can try a mixture of if, else and switch blocks. You can use different methods to call based on the first roll. A switch statement is usually more efficient than a set of nested ifs.

class Result {
        constructor(firstRoll, secondRollMin, secondRollMax, output) {
            this.firstRoll;
            this.secondRollMin;
            this.secondRollMax;
            this.output;


            switch(firstRoll){
                case 1:
                    this.output = oneToXTime(secondRollMin,secondRollMin)
                    break;
                case 2:
                    this.output = twoToXTime(secondRollMin,secondRollMax)
                    break;
                case 3:
                    this.output = threeToXTime(secondRollMin,secondRollMax)
                    break;
                case 4:
                    this.output = fourToXTime(secondRollMin,secondRollMax)
                    break;
                case 5:
                    this.output = fiveToXTime(secondRollMin,secondRollMax)
                    break;
                case 6:
                    this.output = sixToXTime(secondRollMin,secondRollMax)
                    break;
            }
        }

          static String oneToXTime(secondRollMin,secondRollMin) {
            String result = ""

            if (secondRollMin < 5) {
               result = "My result"
            }
            else if (secondRollMin < 10) {
                result = "My result 2"
            }

            return result
        } 

         static String twoToXTime(secondRollMin,secondRollMin) {
            String result = ""
            if (secondRollMin < 5) {
               result = "My result"
            }
            else if (secondRollMin < 10) {
                result = "My result 2"
            }
            return result
        } 

         static String threeToXTime(secondRollMin,secondRollMin) {
          // Same as above...
        } 

        static Stfing fourToXTime(secondRollMin,secondRollMin) {
          // Same as above...
        } 

        static String fiveToXTime(secondRollMin,secondRollMin) {
          // Same as above...
        } 

        static String sixToXTime(secondRollMin,secondRollMin) {
          // Same as above...
        } 

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM