Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truth table with actions

In my code I'm evaluating 3 booleans.
Each combination of those booleans truth value requires different action (for example, different method should be executed).
Currently I'm using if-else block (8 options, not that nice). I'm wondering if there's another option to write this code which will make it 'prettier'.
Maybe design pattern?
Someone has an idea?

like image 247
danieln Avatar asked Mar 28 '13 09:03

danieln


1 Answers

use a switch block

switch ((A?4:0) + (B?2:0) + (C?1:0)){
case  0: //A,B,C false 
break;

case  3: //A False, B,C true
break;

case  4: //A True, B,C false 
break;
}
like image 123
Lefteris E Avatar answered Oct 25 '22 19:10

Lefteris E