If have a big ( about 100 plus) if else statement like below and the if else condition might be irregular(for example some depends on 3 variables, some on 4), is there any way of making it simpler?
Basically I have a table of around 100 plus rows, with the a,b,c and d as the column. Based on a,b,c and d, I need to perform 3 different type of function.
The table describes a set a business rules.
uint8 a;
uint8 b;
uint16 c;
uint8 d;
if ( a == 1 && b == 1 && c == 0) { functionA();}
else if ( a == 5 && b == 5 && c == 2 && d == 2) { functionB();}
else if ( a == 1 && (b ==36 || b == 7) && c == 0) { functionC();}
else if ( a == 3 && b == 3 && d == 50) {functionA();}
:
:
Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false. if (testScore > 60) cout << "You pass" << endl; if (testScore > 90) cout << "You did great" << endl; For example, before noon (AM) and after noon (PM) are mutually exclusive.
Yes, it's almost always unbounded recursion. a large amount of else if won't overflow the stack or cause a crash, but if you have pages and pages of it then it's a sign that the code should be structured better.
if / else if / else is more flexible (hence better), but switch is slightly faster because it just computes the condition once and then checks for the output, while if has to do this every time.
A switch statement is significantly faster than an if-else ladder if there are many nested if-else's involved. This is due to the creation of a jump table for switch during compilation. As a result, instead of checking which case is satisfied throughout execution, it just decides which case must be completed.
There are lots of ways to make it simpler, for example:
a
, b
, c
, and d
values to check for to the function to call (the code to populate the map may still be a mess, but it'll be faster and cleaner executing; can add two keys for cases ala b == x || b == y
)if (a == 1) if (b == 1 && c == 0) functionA(); else if ((b == 36 || b == 7) && c == 0) functionC();
. switch
statements may make this cleaner. In such a factoring, you can also use <
, <=
, >
, and/or >=
to divide larger search spaces, improving performance to from O(N) to O(log2N).a
, b
, c
, and d
once, use a macro ala #define ELIF_ABCD(A, B, C, D, F) else if (a == (A) && b == (B) && c == (C) && d == (D)) F();
. Add macros as necessary for other combinations of tests e.g. ABD
, ABC
, AD
.int64_t
) then e.g. binary search an array of function pointersSomething to look out for though is that the if/else chain may contain things like:
if (a == 1 && c == 3 && d == 2) functionY();
else if (a == 1 && b == 2 && c == 3) function X();
Here, the order is significant as an input can match both. This aspect can easily get lost or altered if the searches are factored differently or some manner of indexing to function pointer is used, which is one argument in favour of the macro approach above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With