Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplify Complicated If statements

Tags:

c#

Well, I have a Form that receives the Employee's level and enable some options depending on his level using a bunch of Checkboxes. However the problem i am facing is that for my application's logic there is specific level range for every option to be enabled so i created an Ugly IF range checking statements that i am sure there is a better way to achieve.

CODE:

if (level >= 1 && level < 3) {
    _items[0].Enabled = true;
    _items[1].Enabled = false;
    _items[2].Enabled = false;
    _items[3].Enabled = false;
    _items[4].Enabled = false;
    _items[5].Enabled = false;
    _items[6].Enabled = false;
    _items[7].Enabled = false;
}
else if (level >= 3 && level < 5) {
    _items[0].Enabled = true;
    _items[1].Enabled = true;
    _items[2].Enabled = false;
    _items[3].Enabled = false;
    _items[4].Enabled = false;
    _items[5].Enabled = false;
    _items[6].Enabled = false;
    _items[7].Enabled = false;
}
else if (level >= 5 && level < 7) {
    _items[0].Enabled = true;
    _items[1].Enabled = true;
    _items[2].Enabled = true;
    _items[3].Enabled = false;
    _items[4].Enabled = false;
    _items[5].Enabled = false;
    _items[6].Enabled = false;
    _items[7].Enabled = false;
}
else if (level >= 7 && level < 9) {
    _items[0].Enabled = true;
    _items[1].Enabled = true;
    _items[2].Enabled = true;
    _items[3].Enabled = true;
    _items[4].Enabled = false;
    _items[5].Enabled = false;
    _items[6].Enabled = false;
    _items[7].Enabled = false;
}
else if (level >= 9 && level < 11) {
    _items[0].Enabled = true;
    _items[1].Enabled = true;
    _items[2].Enabled = true;
    _items[3].Enabled = true;
    _items[4].Enabled = true;
    _items[5].Enabled = false;
    _items[6].Enabled = false;
    _items[7].Enabled = false;
}
else if (level >= 11 && level < 13) {
    _items[0].Enabled = true;
    _items[1].Enabled = true;
    _items[2].Enabled = true;
    _items[3].Enabled = true;
    _items[4].Enabled = true;
    _items[5].Enabled = true;
    _items[6].Enabled = false;
    _items[7].Enabled = false;
}
else if (level >= 13 && level < 15) {
    _items[0].Enabled = true;
    _items[1].Enabled = true;
    _items[2].Enabled = true;
    _items[3].Enabled = true;
    _items[4].Enabled = true;
    _items[5].Enabled = true;
    _items[6].Enabled = true;
    _items[7].Enabled = false;
}
else if (level >= 15 && level < 17) {
    _items[0].Enabled = true;
    _items[1].Enabled = true;
    _items[2].Enabled = true;
    _items[3].Enabled = true;
    _items[4].Enabled = true;
    _items[5].Enabled = true;
    _items[6].Enabled = true;
    _items[7].Enabled = true;
}
like image 815
Roman Ratskey Avatar asked Nov 28 '22 11:11

Roman Ratskey


2 Answers

You can simplify this with a little math:

int on = (level+1)/2;
for (int i = 0 ; i != 8 ; i++) {
    _items[i].Enabled = (i < on);
}

The first line converts a number in the range from 1 to 16, inclusive, to a number in the range from 1 to 8, inclusive. Then the loop goes through all items, and enables as many of them as is indicated by the value of the on variable computed earlier.

like image 120
Sergey Kalinichenko Avatar answered Dec 19 '22 02:12

Sergey Kalinichenko


You (might) lose performance, but its arguably more readable. Choose your poison! The performance difference depends on how often this is called.

if(level >= 1) {
    _items[0].Enabled = level >= 1;
    _items[1].Enabled = level >= 3;
    _items[2].Enabled = level >= 5;
    _items[3].Enabled = level >= 8;
    _items[4].Enabled = level >= 9;
    _items[5].Enabled = level >= 11;
    _items[6].Enabled = level >= 13;
    _items[7].Enabled = level >= 15;
}
like image 43
dee-see Avatar answered Dec 19 '22 02:12

dee-see