if (a || (b and (c or d)) and (not e or (f and g and h))) then ???
September 25, 2007 7:24 PM
Subscribe
Comp Sci: What's the best Design Pattern or structure for dealing with a complex relationship between many booleans?
I'm working on an app that has a zillion configuration options. I need to deal with situations in which the user choses Option A and (Option B or Option C) but not (Option D and (Option E or F)). That sort of thing.
Using tons of if/then/else's is clearly wrong. It's totally confusing. The State Pattern seems like the right direction, but unless I misunderstand it, it's too simple. It assumes that an app is in State A, State B or State C. What about apps that are in States A and C? In other words, there are 50 options and any combination of them can be on/off.
What's the best way to manage this?
I'm an Actionscript Programmer, but I'm pretty comfortable translating concepts from C-family languages.
posted by grumblebee to computers & internet (34 comments total)
1 user marked this as a favorite
Consider, say, three bools. Since a bool can have only two states, I can treat it as a bit. A binary number is just a vector of bits too.
So:
enum mybittype { option1 = 1, option2 = 2, option3 = 4 };
enum mybittyype mybits = option1 | option2 ; // bitwise |, not ||
if( mybits & option1) // bitwise &
Alternately, make a class type that wraps an enum (or bitmap or bitvector) that encapsulates frequent and complex operations as named methods.
posted by orthogonality at 7:40 PM on September 25, 2007