|
|
|
| Non-WTF Job: C++ Developer at Good Grievance (Ronkonkoma, NY) |
| « We Need a Body | 1.14: Turnover » |
Pop quiz, hot shot. There are seven different true/false flags. You have only a single integer to represent them. What do you do? What do you do?
When Stephan E's predecessor was faced with this problem, he knew exactly how to handle things. He used the integer to store a bit pattern. Of sorts. Well... kinda...
public class BestMatchedEntity {
public static final int MATCH_EMAIL_ONLY = 0001000;
public static final int MATCH_LNAME_ONLY = 1000000;
public static final int MATCH_LNAME_DOB = 1100000;
public static final int MATCH_LNAME_DOB_EMAIL = 1101000;
public static final int MATCH_LNAME_DOB_FNAME = 1110000;
public static final int MATCH_LNAME_DOB_FNAME_ADDR = 1110001;
public static final int MATCH_LNAME_DOB_FNAME_TELNO = 1110010;
public static final int MATCH_LNAME_DOB_FNAME_TELNO_ADDR = 1110011;
public static final int MATCH_LNAME_DOB_FNAME_REFNO = 1110100;
public static final int MATCH_LNAME_DOB_FNAME_REFNO_ADDR = 1110101;
public static final int MATCH_LNAME_DOB_FNAME_REFNO_TELNO = 1110110;
public static final int MATCH_LNAME_DOB_FNAME_REFNO_TELNO_ADDR = 1110111;
public static final int MATCH_LNAME_DOB_FNAME_EMAIL = 1111000;
public static final int MATCH_LNAME_DOB_FNAME_EMAIL_ADDR = 1111001;
public static final int MATCH_LNAME_DOB_FNAME_EMAIL_TELNO = 1111010;
public static final int MATCH_LNAME_DOB_FNAME_EMAIL_TELNO_ADDR = 1111011;
public static final int MATCH_LNAME_DOB_FNAME_EMAIL_REFNO = 1111100;
public static final int MATCH_LNAME_DOB_FNAME_EMAIL_REFNO_ADDR = 1111101;
public static final int MATCH_LNAME_DOB_FNAME_EMAIL_REFNO_TELNO = 1111110;
public static final int MATCH_LNAME_DOB_FNAME_EMAIL_REFNO_TELNO_ADDR = 1111111;
public static final int NUM_OF_MATCH_FIELDS = 7;
...
}
And how exactly did this bit pattern get used? Well...
if
(hasLastName && hasDob && hasFirstName && hasEmail
&& hasRefNo && hasTelNo && hasAddr) {
matchPattern = MATCH_LNAME_DOB_FNAME_EMAIL_REFNO_TELNO_ADDR;
} else if
(hasLastName && hasDob && hasFirstName && hasEmail
&& hasRefNo && hasTelNo && !hasAddr) {
matchPattern = MATCH_LNAME_DOB_FNAME_EMAIL_REFNO_TELNO;
} else if
(hasLastName && hasDob && hasFirstName && hasEmail
&& hasRefNo && !hasTelNo && hasAddr) {
matchPattern = MATCH_LNAME_DOB_FNAME_EMAIL_REFNO_ADDR;
(hasLastName && hasDob && hasFirstName && hasEmail
&& hasRefNo && !hasTelNo && !hasAddr) {
matchPattern = MATCH_LNAME_DOB_FNAME_EMAIL_REFNO;
...
|
There's nothing essentially wrong with doing it like this, in fact in some situations it is to be prefered.
You get finer grained control over the business logic if you can handle specific combinations seperately. You can, for example, scroll down to the elseif block that handles the DOB-email-name combination and make it a Special Case with special behavior. It makes it a lot easier to refactor. That's not so easy if you just obfuscate it all as a load of bits and masks which by definition have no meta-data attached. |
Re: Boolean Integers
2008-03-07 08:39
•
by
this webcomic is a wtf
(unregistered)
|
This is actually very nice, we can then change the value to a string, then test for the condition with a simple mid(str(integerflagholder),positiontotest,1). You can't do neat stuff like that with some crazy ass binary bit flipper. |
| « We Need a Body | 1.14: Turnover » |