View Single Post
Nextra
Veteran Member
Join Date: Apr 2008
Location: Germany
Old 05-30-2015 , 11:23   Re: How to transfer a lot of options in "for" operator
Reply With Quote #10

Quote:
Originally Posted by groofshark View Post
That's for all menu. I want instead adding 20 if( g_options[id][iItem] ) just to make a loop and that loop to give on every "IF" - option and his number(iItem)
You don't need to loop unless you have a more complex situation.

Code:
/**  * Given this enum the following things are true:  *    opt1 == 0 && opt2 == 1 && opt3 == 2  */ enum _:options {     opt1,     opt2,     opt3, } /**  * That means that this if can easily be reduced.  * Observe that when |iItem == 0| then |opt1 == iItem| is also true.  */ if(g_options[id][opt1] && iItem == 0 ) {     return ITEM_DISABLED; } /**  * So instead we can write it as following, note that |opt1| has  * been replaced, and instead |iItem| is retrieved from the array.  * This does not change code behavior.  */ if(g_options[id][iItem] && iItem == 0 ) {     return ITEM_DISABLED; } /**  * Since the code inside of your if statements does not  * differ (it always returns the same value), the |iItem == 0|  * check is actually redundant. This means that we can remove it  * and get rid of all the other if statements at the same time:  */ if(g_options[id][iItem]) {     return ITEM_DISABLED; }

A loop would unnecessarily complicate things. You need to do something based on a single value, and you have the means to directly derive the necessary information from that single value. You don't need to search for anything, you don't need to iterate. A lookup-table is perfect for this, and you already have it in place. You just need to use it properly.
__________________
In Flames we trust!
Nextra is offline