Was just a quick question to see if there are different ways you code something similar when it comes to evaluating conditional statements/control flow.
For example:
- If Statements
- Switch Statements
Is there any tidier way to do these as I have basically the option of If (value == X) { // do X } and Switch(value) { case X: ...
When doing this with over 100 values is there any data driven approach that could be taken or any different evaluation methods that would tidy up the code?
If your values are integers and are not sparse sometimes it can be convenient to use a lookup table (both for data and for code – in this last case you’d use function pointers and is often called a jump table, which is incidentally what the compiler often does with
switchblocks); if the alternative is checking the possible values one by one, the performance jumps from O(N) to O(1).Also, for non-integer data, hash tables can be used. How convenient they are depends from case to case.