Say I have code like this:
if (var == OPTION_A) {
// Statements
}
else if (var == OPTION_B) {
// Statements
}
else if (var == OPTION_C) {
// Statements
}
else if (var == OPTION_D) {
// Statements
}
else if (var == OPTION_E) {
// Statements
}
else if (var == OPTION_F) {
// Statements
}
else if (var == OPTION_G) {
// Statements
}
else {
// Statements
}
Is this the best way to write such as expression? I only worry because my list is growing rather long (currently twelve such branches). In each case, the entire warp will dive into one branch, but every OPTION_ I’ve got is an integer code, so I wonder if it’s not better to write
if (var < OPTION_E) {
if (var == OPTION_A) {
// Statements
}
else if (var == OPTION_B) {
// Statements
}
else if (var == OPTION_C) {
// Statements
}
else if (var == OPTION_D) {
// Statements
}
}
else {
if (var == OPTION_E) {
// Statements
}
else if (var == OPTION_F) {
// Statements
}
else if (var == OPTION_G) {
// Statements
}
else {
// Statements
}
}
And perhaps bifurcate even more. My code won’t look any more difficult to read for such nesting; I’ve got it set up pretty well. The code within each branch is a lot more meaty than a handful of if-thens, so the case switch to get us into the section that is needed is still a relatively minor expense, but I want to know if that cost is growing with each new option I add, and if so what I can do about it.
Thanks!