- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
priceless...[B]
Admin
What a dork. He's supposed to use bitfields. What happens tomorrow when they need to cover the case of applying the adjustment to only aggregates?
Doesn't he know what BOOL means? Bitfield Object Operating as Long.
--Rank
Admin
I guess his binary looks like this 11011012.
I can understand having more states than true and false like if I was working on software used for circuit design I could have a function IsConnected returning {true, false, HI_Z} but I would create a enum for this not put BOOL ret = 2.
Admin
And there was me, innocently thinking it was going to be another SQL WTF to do with tri-state logic.
Nice.
Very, very, nice. I bow to obviously superior code-fu.
Simon
Admin
Admin
Pesky booleans. Just a special case of an enum.
Could've been worse... he could've quantized the values between 0 and
1 on a float.
#define FALSE 0.0f
#define ALMOST_FALSE 0.1f
#define NOT_QUITE_FALSE 0.2f
#define COIN_FLIP 0.5f
#define MAYBE_TRUE 0.6f
#define PROBABLY_TRUE 0.7f
#define ALMOST_CERTAINLY_TRUE 0.8f
#define TRUE 1.0f
Admin
All the above posts are all TWO_TRUE.
Admin
A friend put together an application that, at one point, asked the user:
Delete the User: J Smith
[Yes] [No] [Maybe]
.... gotta love programmers with a sense of humour
Admin
Delete the User: J Smith
[Yes] [No] [Who?]
Admin
What did the [Maybe] button do? A true Maybe button would use a random number generator and work 50% of the time.
Admin
Pops up another dialog:
Are you sure?
[ Yes ] [ No ]
Admin
A similar but different problem I always hate in a lot of languages is just deciding whether something will be true or false.
For example, in Perl, you so often see just "if ($foo)" to check if the variable is defined and not null. The same thing happens in a lot of languages, but these vague statements can have bad effects, like when 0 or the emptry string becomes a valid value for $foo. When there is a built-in defined() function (same thing in PHP...isset() built-in) or when you could just check != null), I amaze even myself when I do the same thing of just putting "if ($foo)". I only change it when it comes back to bite me.
FWIW, please let the language war begin just because I mentioned the two P languages...we all already know that it wouldn't be a problem with a stricter-typed language and therefore all scripting languages suck...and we all already know that stricter-typed languages are cumbersome with too much typing and so all scripting languages rock.
WTFever...and BTW someone tell this guy to get an enum.
Admin
You know, this really isn't that bad. He just extended the functionality of the function without correctly refactoring like he should have. It's more of a comment on how C works than how the coder works. Just make "BOOL IsAdministrative" into "int AdministrativeCode", and use 0 and 1 instead of TRUE and FALSE (or better yet, make new named constants). A select instead of ifs would be more efficient, but that's nitpicking. Could be much worse.
Admin
In quantum computing you need a "Yes And No" button.
Admin
Admin
I love the smell of a Saturday, 0344 hack in the morning.
It smells like...victorEEEEEeeeeeeeeeeeeeee!
Admin
Use boost::tribool. The biggest WTF i've ever seen. I mean, why not just use enumerations.
Admin
In its simplest form this is a WTF, but if we dig a little deeper we will find that it was a simple copy/paste error where the programmer simply copied the wrong parameter from the function.
observe the function declaration
Admin
ooh -- great point! I think you are correct -- this is not a WTF, but rather a typo or a honest mistake in writing the code. And, to be honest, it is well-commented and that helps us determine what the true intent was.
So, I think we definitely cannot consider this a WTF but rather a simple coding error, and we should give some props to the coder for good comments which helps someone who is trying to debug the code quickly identify the mistake.
Admin
Sorry guys, that was my oversight. The original submission had nothing to do with accounts, adustments, etc. Everything to do, however, with a TRUE/FALSE/2 boolean tho ...
But good find none the less.
Admin
Even if it is a cut'n'paste the wrong variable error, they're still using TRUE, FALSE and 2. maybe not a major WTF, but still very odd...
Admin
You're right! Not only was it an honest mistake, it was some of the most clever code I have seen in a long time.
Admin
There's nothing WTFery about tribool. You just need to know when to use it. The indeterminate value could, for example, mean "impossible to determine". Situation would be a generic stream, and you ask it if the next read for 100 bytes will block. Problem is, the stream is in reality a decompression wrapper, so it has no clue how many bytes are really needed, and therefore whether the read will block.
Or it could mean "partially true", because not everything is black and white. Just look at the checkboxes in Windows - checked, not checked, or grey-checked (e.g. because only a part of the subfeatures will get installed.
Sure, you can misuse it - but you can misuse normal booleans as well.
Admin
Ah, the wonderful world of tri-state booleans.
In Java, you can have a java.lang.Boolean whose values are Boolean.TRUE, boolean.FALSE, and of course good old null.
In SQL, you can have a bit field of size one with values: 0, 1, and good old null.
In C# I even saw a third-party class called defaultable boolean. It was an enum with three values: True, False, and Default. I think it was for gui controls that were supposed to inherit their state from their container.
Kind of reminds me of those trilogies with four books in them. Or of Indiana Jones: "I said NO camels! Can't you count?"
Admin
I prefer my book trilogies in 5 parts, thank you very much.
Admin
And you probably know where your towel is.
Admin
I have indeed been referred to as a 'Hoopy Frood'.
Admin
Ok. I'll bite.
This is in fact a great example of the downsides to loosely typed scripting languages, because of the weird side-effects you mention. I've had the same problems in VB, Javascript, VBScript and Coldfusion. There are definite down-sides to strictly typed languages as well, but its usally easier to convey your exact intentions to the runtime in a strongly typed language.
So when using a loosely typed lanuage, I discipline myself and DON'T do things like the above. I treat the language as if its strongly typed unless i really need something flexible. Heck, I hate how most of the strongly typed languages let you pass in null instead of a real object, rather than dictate if that's allowed in the method defiintion.
Ding! Ding! Round 2...
Admin
Its amazing that this was the post today, and your response. I JUST implemented an enumeration in C# that allowed True, False or "Not Applicable." When saved to the sql server database, this comes out to a nullabel bit field (1, 0 or null.)
Is that bad? The business requirements had to allow some bit fields to remain null, or even revert back to null if they edited the record later.
Admin
Come to think of it, I don't think I've ever read a trilogy that had other than 5 book. I've read several series with 3 novels, but they were not called trilogies by anyone I can determine. I've read several trilogies that contain 5 novels. (not just the obvious one)
Unfortunatly I'm the type of guy who can never find his towel, hence I've been stuck on this backwater planet for years.
Admin
And a bonus WTF for today:
<FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#0000ff>bool</FONT> HasDst = (tzi.DaylightDate.wMonth == 0) ? <FONT color=#0000ff>false</FONT> : <FONT color=#0000ff>true</FONT>;</FONT>
Taken from http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21497706.html.
Admin
That is simply redundant, but not wrong.
Admin
"Ones and zeroes everywhere... and I thought I saw a two"
Admin
I think there are valid usages of tristates. nullable bit field and a triple enum make a lot of sense for this. It just seems odd that the world tries so hard and so often to push you into that direction. I guess it is just hard in practice to constrain a field to two values.
Admin
Don't worry, Bender. There's no such thing as two.
Admin
Actually I've seen at least five checkbox states.
checked
unchecked
checked and disabled (that is, grey with a check)
unchecked and disabled (that is, grey without a check)
greyed out (that is, enabled but in a filled-with-grey state)
The last is usually the "third state" of a tri-state checkbox.
Admin
Would you truly suggest that this line of code should not have been written as,
<FONT color=#0000ff> bool</FONT> HasDst = tzi.DaylightDate.wMonth!=0;
?
That aside, since when do WTFs have to do with right and wrong? If the code compiles, does that make it right? If redundancies are no longer WTFs, maybe we should have Alex remove the post at http://www.thedailywtf.com/forums/32119/ShowPost.aspx, and a few others.
Admin
I would write it your way, but if someone told me it is more readable to use the "? true : false" method, I would not call the kode kops to have them taken away.
Admin
I know exactly which third party controls you are talking about...Infra...whatever you get the GIST. Regardless of whether tri-states are a WTF, the way they are implemented by this ISV is a WTF. In most circumstances, the enum for DefaultableBoolean is redeclared in every namespace, instead of being in some common namespace.
For example, a control's property such as Namespace.Object1Namespace.Object1.Visible isn't just of type Namespace.DefaultableBoolean, it's of type Namespace.Object1Namespace.DefaultableBoolean. That way if you have Namespace.Object2Namespace.Object2.Visible, you need to use Namespace.Object2Namespace.DefaultableBoolean.
Extreme WTFery with every control I have ever used w/ from vendor [but when you figure it all out (because there is no documentation) the controls look really pretty :P ].
Admin
Check/uncheck and enabled/disabled are two separate states, enabled/disabled belonging to the base "window" object and check/uncheck belonging to the checkbox object that is a child of window.
Also the checkbox state is stored as an INT rather than a bool, thus allowing the tri-state check/uncheck/grey.
Admin
And don't forget
#define A_DEFINITE_MAYBE 1.0f
Admin
At least with a tristate bool you can work around those pesky 'Assertion Failed' errors [;)]
if (true)
alert("Yes");
else
alert("No");
otherwise
alert("Dunno");
Now that looks silly even to myself [:$]
Drak
Admin
Oh, don't get me started on Boolean objects in Java. How about this construct, then:
Doesn't that bring a tear to the eye? Fortunately I was able to stop the programmer in time.
Admin
I consider overuse of
defined
and co to be a WTF. If you really mean to check whether a string contains anything, then yes,( defined $str and length $str )
is the right conditional. But if$foo
is a metasyntactic placeholder for$somebool
in your example, then absolutelyif( $somebool )
is the exact right thing to do.Perl has a slightly peculiar, but very clear definition of truth: in scalar context, anything is true other than
undef
, the empty string, a numeric zero, or a string consisting of single0
character. Checking for only one of these things specifically is bound to eventually get you in trouble with other people’s code. Particularly because in list context, anything other than the empty list is true.A big WTF in Perl is saying
return 0;
to return a generic false. In context, that’s a list with a single element and actually means “true”. The right thing to do is to simply sayreturn;
, which will give the caller anundef
in scalar context and an empty list in list context – both correctly false. You should onlyreturn 0;
if you actually want to return a numeric zero.The funny thing about Perl is that generally, the less work you do, the more correct and general your code is. Unfortunately, many people insist on using it like an awkward, strongly typed language instead of an expressive, weakly typed language, thereby playing to its weakness instead of its strength.
Admin
That was meant to say “In context, that’s a list with a single element” – I wish we could actually edit our posts.
Admin
Err, “In list context, that’s a list with a single element,” goddammit.
Admin
I've seen this a lot actually, that's because a BOOL is an integer, not a real boolean. Even the bool in MFC isn't a real bool. I once searched a whole day why
if(somebool == true){
// code here
}
else{
// code here
}
kept going in the else even when somebool in the quickwatch gave true. Seemed that even the bool type could have a tri-state. [^o)]
When I did
if(somebool){
// code here
}
else{
// code here
}
Admin
No, no, no, no, no!
This is a simple case of not understanding your own data relationships. He does not have one datum here, he has two data which he has wrongly amalgamated into one, thus losing orthogonality. Just because only threads for administrators will ever need to consider adding aggregates, he has treated them as a special type of administrator.
The correct answer is a pair of booleans. By all means stick them in a bitfield, but then write a proper set of functions by checking each bit value rather than by checking for equality against values. In other words...
If bit0 is the administrator flag, then bitwise-and with 1 and check for non-zero in whatever language you choose.
If bit1 is the administrator flag, then bitwise-and with 2 and check for non-zero in whatever language you choose.
Justin.
Oracle and Java Guru who really should get a login here.
Admin
Every hack fantasy series is either a trilogy or a double trilogy. (Trilogy of trilogies are reserved for the esteemed L. Ron.) It's book publishers' weapon against good writing: drown a little quality in a lot of quantity and hope people keep paying for it.
I was really disappointed that this code did not end with isAdministrative == YELLOW. I'd have paid good money to see a code snippet like that. (If it walks like a wtf and quacks like a wtf... maybe it's a TRUE2.)
I guess you could define true as GREEN and false as RED if you wanted to put it in a framework that made a goofy sort of sense... but wouldn't it be so much more surreal to spot a YELLOW out of nowhere in a couple of source files with no comment and no obvious definition? HELL YES.
Alex, your title has finally inspired me to create code that will have my corporate descendants upping their medication levels. Score!
Admin
What?
You do know he's supposed to use XML, throw in some Javascript and then, using an XSLT needs to validate it with some Instr, Left$, Right$ and Mid$ don't you? Tsssssk, developers these days... always looking for an easy way out.