Comment On Boolean Integers

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? [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Boolean Integers

2008-03-07 08:04 • by Anonymous (unregistered)
I'm always amazed at the number of people who don't understand bitwise operators in C. Any type of embedded GPIO practically requires them.

Re: Boolean Integers

2008-03-07 08:06 • by ParkinT
Bitwise math. I love it!

Re: Boolean Integers

2008-03-07 08:09 • by Christian (unregistered)
Wow...just wow...

Re: Boolean Integers

2008-03-07 08:15 • by dabean
I bet he had nightmares about having to add another 'bit'.

Re: Boolean Integers

2008-03-07 08:22 • by Michael (unregistered)
If the code REALLY looks like that, there's another WTF hidden: It literally is 1111111 (decimal), not binary 1111111 aka 127.

Re: Boolean Integers

2008-03-07 08:29 • by Welbog
181979 in reply to 181978
Michael:
If the code REALLY looks like that, there's another WTF hidden: It literally is 1111111 (decimal), not binary 1111111 aka 127.
Your powers of observation are incredible.

Re: Boolean Integers

2008-03-07 08:30 • by gabba
That first one is actually octal, right? It is in C/C++, anyway.

Re: Boolean Integers

2008-03-07 08:32 • by rdrunner
181982 in reply to 181978
Michael:
If the code REALLY looks like that, there's another WTF hidden: It literally is 1111111 (decimal), not binary 1111111 aka 127.


That is actually a feature. It allows you to REALLY make sure you are searching for an lastnameby passing in a 9000000

Re: Boolean Integers

2008-03-07 08:33 • by gekko
181983 in reply to 181978
Michael:
If the code REALLY looks like that, there's another WTF hidden: It literally is 1111111 (decimal), not binary 1111111 aka 127


If only it were that simple. All the numbers with a leading one are actually in decimal--but leading zeros? That's octal, baby!

Re: Boolean Integers

2008-03-07 08:37 • by Burninator (unregistered)
This is the first code in a month or two that's made me actually want to gouge my eyes out in disbelief.

The fact that he attempted to use a bitmask but had to make a constant variable for each possible combination of bits that he would use, OUGHT to tell him something....

Re: Boolean Integers

2008-03-07 08:37 • by Jens (unregistered)
He he, Speak of the devil! I'm making use of a bit array myself for something im working on right now.

Though i'm opting to use the bitwise operators for accessing it, and i think i'm going to the skip storing every possible combination as a constant (who's decimal represetation resembles the binary representation of arra).

Great that he actually has booleans for everything allready ... but insists on setting the contants anyway.

Re: Boolean Integers

2008-03-07 08:37 • by cthulhu (unregistered)
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:38 • by Anon (unregistered)
Because of the mix between octal and decimal, I'm guessing that's why he did this. Something wasn't working by using bitwise operators correctly (because his numbers were wrong), so he came up with this cockamamy scheme to get it to work.

Re: Boolean Integers

2008-03-07 08:38 • by Berislav (unregistered)
Wait a second -- why is the result variable of all those if's called matchPattern? It couldn't be that they perform a regexp comparison somewhere down the road, could it? If this is so, the WTF should be titled "Boolean Integer Strings".

Re: Boolean Integers

2008-03-07 08:39 • by this webcomic is a wtf (unregistered)
181991 in reply to 181978
Michael:
If the code REALLY looks like that, there's another WTF hidden: It literally is 1111111 (decimal), not binary 1111111 aka 127.


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.

Re: Boolean Integers

2008-03-07 08:41 • by yer mom (unregistered)
After defining 20 of these "bit patterns" he probably thought "Screw this", and left out the other 107.

Re: Boolean Integers

2008-03-07 08:42 • by earl (unregistered)
181993 in reply to 181979
Well, at least that explains why the programmer could not use bitwise operators:

MATCH_LNAME_DOB | MATCH_EMAIL_ONLY != MATCH_LNAME_DOB_EMAIL
0x10C8E0|0x3E8 != 0x10CCC8
0x10CBE8 != 0x10CCC8

1100000|0001000 = 1100776

So it makes perfect sense why the binary operators where not used: they just would not work.

(and by the looks of it, that is Java)

Re: Boolean Integers

2008-03-07 08:42 • by DaveAronson
TORWTF is that crazy code indentation....

Re: Boolean Integers

2008-03-07 08:44 • by Pyro (unregistered)
There are exactly 10 types of people in the world: those who understand binary and those who don't :)

Re: Boolean Integers

2008-03-07 08:47 • by BrianJ (unregistered)
181997 in reply to 181983
gekko:
Michael:
If the code REALLY looks like that, there's another WTF hidden: It literally is 1111111 (decimal), not binary 1111111 aka 127


If only it were that simple. All the numbers with a leading one are actually in decimal--but leading zeros? That's octal, baby!


This looks like C#. C# does not have octal literals. So all these numbers are decimals.

Re: Boolean Integers

2008-03-07 08:49 • by Harry (unregistered)
181998 in reply to 181995
Pyro:
There are exactly 10 types of people in the world: those who understand binary and those who don't :)


There are exactly three types of people in the world. Those that understand true and false and those that don't.

Re: Boolean Integers

2008-03-07 08:49 • by sweavo (unregistered)
181999 in reply to 181995
Pyro:
There are exactly 10 types of people in the world: those who understand binary and those who don't :)


And those who know about ternary

Re: Boolean Integers

2008-03-07 08:50 • by yer mom (unregistered)
182000 in reply to 181997
There's no final in C Pou... Sharp

Re: Boolean Integers

2008-03-07 08:50 • by John Doe (unregistered)
In this case using XML would really be a better solution... (No matter what way!)

Re: Boolean Integers

2008-03-07 08:55 • by krupa (unregistered)
Let me count the ways that code is bad. Oh wait, I only have an integer to store the total and I see eight things. Shit.

Re: Boolean Integers

2008-03-07 08:57 • by tgies (unregistered)
182003 in reply to 181988
cthulhu:
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.


look how dumb you are

Re: Boolean Integers

2008-03-07 08:58 • by BlueCollarAstronaut (unregistered)
Actually, this implementation looks "Pretty Simple".

Re: Boolean Integers

2008-03-07 09:02 • by Claxon
Looks like Java to me.

Which is your favorite Bit?

Re: Boolean Integers

2008-03-07 09:04 • by keanpedersen (unregistered)
182008 in reply to 181997
The code is in Java, which does have octal literals.

Re: Boolean Integers

2008-03-07 09:05 • by Anon E. Muss (unregistered)
182009 in reply to 182000
I've always preferred to call it D Flat.

Re: Boolean Integers

2008-03-07 09:13 • by DeLos
clbuttic c-pound code!

Cargo Cult Programming

2008-03-07 09:18 • by Anonymouse (unregistered)
This is clearly a case of Cargo Cult design. He saw the bitfield method used somewhere, heard it was the right thing to do, and tried to implement it. Without the appropriate background in boolean mathematics or software theory. And then made a complete botch of it.

Since C doesn't usually (outside of certain embedded compilers) support raw binary numbers, most people use octal or hex for constants. Then you get a nice string of 0x0001, 0x0002, 0x0004, 0x0008, 0x0010 etc.

Re: Boolean Integers

2008-03-07 09:19 • by Emphyrio (unregistered)
TRWTF is that the equals signs are not lined up in the constant definitions.

Re: Boolean Integers

2008-03-07 09:21 • by jimlangrunner
Sweet Lord.

It doesn't matter what language it is. Given that you need to track that much information, the combinations give 2^7=128 possible combinations. Setting a constant for each possible combination is just silly.

Structure the code s.t. you deal with the fields as little as possible.

if
(hasLastName && hasDob && hasFirstName && hasEmail
&& hasRefNo && hasTelNo && hasAddr) {
DO SOMETHING;
} else if
....

Setting a bunch of constants that you have to remember later is a foolish waste of resources. (The programmmer's time, mostly)

Later on, you'll have to have something like

if
(MATCH_LNAME_DOB_FNAME_EMAIL_REFNO_TELNO_ADDR) {

Do Something;
} else if
....

How is that better than the setting of flags in the first place?

Finally, what if I have email & address, but not the last name? Shiat.

Re: Boolean Integers

2008-03-07 09:21 • by Anonymous Coward (unregistered)
182014 in reply to 182010
DeLos:
clbuttic c-pound code!


It's Java. C# has no final keyword.

Re: Boolean Integers

2008-03-07 09:25 • by operagost
182015 in reply to 182003
tgies:
cthulhu:
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.


look how dumb you are

you should all video in your bitwise operations

Re: Boolean Integers

2008-03-07 09:37 • by zip
182023 in reply to 181995
Pyro:
There are exactly 10 types of people in the world: those who understand binary and those who don't :)


There are 2 types of commenters, those that are able to create original humor and those that can only parrot things that were funny five years ago.

Re: Boolean Integers

2008-03-07 09:41 • by Alan (unregistered)
182024 in reply to 181988
cthulhu:
There's nothing essentially wrong with doing it like this, in fact in some situations it is to be prefered.

There is so much wrong with that, i don't know where to start. There is absolutely no reason to define all those extra matches, only 7 are required.

What should be used here is a bitfield, no question about it, but not in the way he implemented it. Let me psuedo code a much better one in C#:

public enum MatchPattern
{
None = 0;
FirstName = 1 << 0;
LastName = 1 << 1
Email = 1 << 2;
...
...
}

public MatchPattern Match ()
{
MatchPattern p = MatchPattern.None;
if (hasLastName)
p = p | MatchPattern.LastName;
if (hasFirstName)
p = p | MatchPattern.FirstName;
if (hasEmail)
p = p | MatchPattern.Email;
return p;
}

Re: Boolean Integers

2008-03-07 09:49 • by Schnolle (unregistered)
182030 in reply to 181988
Oh, sure. All you have to do is to find the special case among the 128 different cases in this motherhuge if statement reaching over about three dozen pages, which is MUCH easier than just have a separate statement covering all the actual special cases after the assignment or whatever...

Re: Boolean Integers

2008-03-07 09:52 • by KludgeQueen
182032 in reply to 182030
Aside from the fact that he's using decimal numbers instead of bitstrings, isn't THIS TRWTF:

" You have only a single integer to represent them. "

Is he writing for the VIC-20 or something?

Re: Boolean Integers

2008-03-07 09:59 • by mack (unregistered)
182034 in reply to 181988
anyone who doesn't think that this code is a piece of crap should be slapped across the face with a keyboard. I suppose the author of this code, as well as this comment, have never heard of something called an enumeration.

If you are setting your program up to have multiple special cases then you are setting your program up wrong.

Re: Boolean Integers

2008-03-07 10:02 • by savar
182035 in reply to 181999
sweavo:
Pyro:
There are exactly 10 types of people in the world: those who understand binary and those who don't :)


And those who know about ternary


And those who have social anxiety disorder.

So really there are 4 (decimal) or 100 (binary) kinds of people in the world.

Re: Boolean Integers

2008-03-07 10:09 • by snoofle
182037 in reply to 181995
In 30 years of programming, I can honestly say I've never seen anyone use mixed decimal bitflags (octal yes, hex yes but never decimal).

Maybe this guy just didn't understand the difference?

Re: Boolean Integers

2008-03-07 10:17 • by akatherder
This is the worst kind of code. Using a bitmask isn't super-advanced, but it does take a certain degree of cleverness to apply it appropriately. So when I see code like that, I usually think that the programmer knew what he/she was doing and I spend time reverse engineering and trying to rationalize it when I should just throw it out the window.

Horrible code masquerading as clever code is your enemy.

Re: Boolean Integers

2008-03-07 10:23 • by lagzor (unregistered)
the real wtf is that the featured comments are even bigger wtfs than the article.

Re: Boolean Integers

2008-03-07 10:27 • by Kyle (unregistered)
182058 in reply to 181991
this webcomic is a wtf:
Michael:
If the code REALLY looks like that, there's another WTF hidden: It literally is 1111111 (decimal), not binary 1111111 aka 127.


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.



No you can't, because (((1 << positiontotest) & integerflagholder) != 0) is too long.

Re: Boolean Integers

2008-03-07 10:27 • by diebitfieldsdie (unregistered)
182059 in reply to 182024
Alan:

What should be used here is a bitfield, no question about it, but not in the way he implemented it.


I question that. Unless you are working with limited hardware or bandwidth, there is *no* reason to try to cram 8 boolean conditions into a single byte. It will only lead to obfuscation and wtfery. There are several alternative solutions involving data structures all the way from enums to classes, all of which involve no more overhead than the original wtf here, and all of which are far more maintainable and less bug-prone.

I would likely just set independent bools and check them in the if statements directly, which has the benefits of adding metadata (variable names) to the conditions, requires coding only the number of cases you actually need, and the logic being immediately readable by the poor sod who has to debug it later.

Re: Boolean Integers

2008-03-07 10:29 • by Kyle (unregistered)
182061 in reply to 181988
cthulhu:
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.


Why yes in fact, it is.

if (specialcase) {
} else {
//use bitmaps here.
}

Re: Boolean Integers

2008-03-07 10:30 • by WhiskeyJack
182062 in reply to 181988
cthulhu:
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.


Fail.

Re: Boolean Integers

2008-03-07 10:30 • by Nitehawk
182063 in reply to 182055
Well if they are going to be highlighting the troll posts, I think its time to quit reading this site.
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment