- 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
A nullable bool isn't a bool. If a null value has special meaning, then you have an enumerator with three values, not a boolean. If you have a boolean with a null value you should be able to safely pick one value as the default. If you can not, then you have a third value, go back to my second statement.
So no, nullable bools have no real place if you know what a bool actually is. A boolean should be seen as simply a TWO state enumerator with very specific meanings, nothing more. Anything with more than two meaningful states is not a boolean.
Admin
You might as well write this:
Admin
Admin
Dates are not "complex" structures. They're purely reals. Or strings.
Just sayin'
Admin
If someone masquerading as a programmer thinks "if (x)" is confusing then they shouldn't have a job that requires them to be near a computer.
Admin
Admin
I recently came across:
Which left me scratching my head. The programmer was sophisticated enough to use the relatively obscure "?" operator, but he didn't realize that in this case, it did ... absolutely nothing.
Admin
No, you need the test because if x is already true, you don't want to set it to true. Or something.
Yes, I've seen this a bazillion times too. I can't help but wonder if the programmers who write this don't think, "Oh, in the case where it's already true, I'll save time it wold take to do the assignment!" So in the case where x is false, they do both the text and the assignment, which obviously takes longer. In the case where x is true, they do a test instead of an assignment. Which on most CPUs, takes longer than doing an assignment.
This is very similar to another favorite of mine:
Because, if the amount is zero, we wouldn't want to add it in to the total.
Admin
Real booleans have five possible values. :)
http://msdn.microsoft.com/en-us/library/office/aa432714(v=office.12).aspx
Admin
There are three types of programmers:
Those that understand nulls, and those that don't
Admin
Admin
That's what's called cargo-cult programming. They've probably seen lots of "if (foo == bar)" and a few "if (somefunc (...))" in code they read and gathered that that's the way to write an if-statement and so they imitate it, without ever thinking why.
Admin
Present them with a moderately complex boolean problem. If they produce a spaghetti if-elseif-else solution, ask them if they can write it more concisely. Repeat as appropriate.
If they can't, show them a compact boolean-logic solution with a small mistake. Now there are exactly two possibilities:
They understand the solution and spot the mistake: Passed.
They nod in awe and praise your solution: You've got a yea-sayer. Move them to management.
Their head aspolde: Problem solved.
Admin
See: the ES5 spec
Notice that the strict equality algorithm is the same as case 1 of the abstract equality algorithm, and in place of all other cases (where types do not match), false is returned. If == returns false, === will also return false, but sometimes == will return true when === would return false (such as '3' == 3). So the === is redundant here.
TRWTF in the first example is that JS can branch based on the truthiness of an expression (even if its result is not a boolean), so comparison with true/false is unnecessary.
Admin
Admin
if this code:
confuses you, then you shouldn't be a programmer.
Admin
Admin
Fixed that for you, this way you can pass you, him, her or them as the subject to check regardless.
Admin
Every decent language needs:
a = b // assignment a == b // test whether 0 and 0.0 evaluate the same a === b // test whether '0' and 0.0 are the same data type, even though they both evaluate to zero a ==== b // test whether they are both references to the very same object a ===== b // are they both constants, and thus can never differ?
Admin
That is why some authors are recommending to avoid boolean in their entirely and use enums instead (http://www.codinghorror.com/blog/2005/10/avoiding-booleans.html)
Admin
You didn't see it in my snippet, but
is a global variable initialized in a block of shared code that gets executed an unknown number of times.Admin
if (isTrue(thisCodeConfusesYou)) { dontBeAProgrammer(you); }
Admin
I really enjoyed how a presumptive bogus input to isTrue() and isFalse() could give inconsistent answers, so that (isTrue(a) ^ isFalse(a)) could sometimes yield false,,,which should never happen.
A first-rate example of how to make a program erratic.
Admin
Encore! Encore!
Maybe:
a ====== b //they are both the same constant...
Admin
It's a trust issue.
Admin
Oh No! Not the baby lemurs?!
Someone call PETA! Oh,duis the inhumanity! Oh, the suffering!
Admin
"BooleanUtilities().isTrue(val)"
While this is certrainly not necessary in languages like Java, I bet people who write this kind of code come from Smalltalk or some pure OOP language. The intent is to send the message isTrue to an object. Classic message passing. It's not necessarily bad, just another way of expression. However, naming the class BooleanUtilities then is a major oop wtf, since the message should be understood by the Boolean object directly.
Admin
I find it funny that Jeff posts about programming for humans, not computers, and then posts something like that. Booleans are very human-friendly, and the fact that they're not computer-friendly creates all the issues of using them. But it's up to the developer to create something that is straight-forward, but use the right implementation.
Admin
Adobe is a synonym for "don't touch with a 10 foot pole" even if some pointy haired guy tries to shove said pole up your...
Admin
Maybe writing stupid boolean handling functions is a popular way to obfuscate javascripts to prevent malicious users from delving much deeper into the code. Would-be hackers are deterred as their mind draws a "wtf?" and they go talk a walk by the lake to rethink their whole lives.
Admin
Admin
Dates are what computer programmers, physicists, engineers and other subspecies of humanity lampooned on TBBT allegedly don't get.
Admin
Look at the difference between strict and loose equality checks on primitives for Firefox. Loose type equality can be much slower than strict type equality in a browser.
Not everything is running on the magical pixy dust that powers Chrome's V8 engine. (Not yet, anyway.)
This would indeed be why checking strict type equality is (almost) always faster: it is at least as fast as loose type equality, because the test for loose equality is a superset of the test for strict equality.
Also, the redundancy in OP's code fragment wants to make me use it as part of an interview test for JavaScript developer positions:
Can't explain that? Then you either fail at understanding the JS weak type system or at understanding basic logic. In either case you should probably be looked over for the position, but at the least you would be pushed down the ladder substantially.Addendum (2012-11-05 17:31): Also; 'The Real WTF' of this thread is Akismet again: I had to try posting the above 10+ times, cutting it down further and further until a single reply sentence remained. Then, once I got that posted, ofcourse I could just edit the original post back in and have it accepted straight away...
Admin
Surely doing the === first would be fatter.
Admin
Give me a bool any day.
My date was so complex that first it was a success, then it was a fail, and the reason it was a fail was because it was a success. No one knows the truth because it depends on what your definition of == ==. No one knows the falsity either because != != blows your mind.
Bool FTW. Give me file not found or give me null.
Admin
Oooo...a new meme: "Give me file not found or give me null."
Admin
Admin
Admin
And even then:
Unless there is some ugly gotcha in Perl that prevents that working (I wouldn't be surprised).(And if it were PHP you'd use the (bool) operator to cast them.)
Admin
A long, long time ago, I did similar code, where I said
. Immediately after writing it, I pulled up MSDN, thinking, "Why isn't there a logical XOR? It would be a ^^ operator. I could just write " Right after I thought this, as the MSDN operators page loaded, I realized, "Why don't I just compare them with a !=? WOW. I feel dumb for wanting a logical XOR now."Basically, it seemed that I wanted to say "if one or the other is true, but not both, then do this." The != operator means something different, it means "if these two are not the same, then do this." Essentially, it's coming from the other direction. It's like how we have multiple words in English that have the same literal meaning, but different conotations. They both do the same thing, but are different logical contexts. So the logic, the context, the meaning of the code called for an xor. But in the end, I used a !=, which has a different meaning when read by a human, but has the same physical meaning. I left a comment to explain the meaning behind it, though. So does that make any sort of sense? Maybe the writer is simply trying to be very explicit as to what they're intending, and hopefully the compiler will be smart enough to simplify the machine code (assuming it's a compiled language).
Admin
I call boolshit.
Admin
And not doing a very good job, because now anyone reading it has to be careful to check that it doesn't actually say "(booleanExpression1 && booleanExpression2) || (!BooleanExpression1 && !booleanExpression2)".
Admin
I totally agree with you.
In the past, I had a developer reporting to me, whose abilities I (and others!) had severe doubts.
We had some javascript code that, quelle surprise, had some un-necessary if clauses (i.e. basic boolean statements). Very hello-world, only a few lines of code.
Alas, after a few hours, he admitted he couldn't, or didn't see the need to rationalize the code....by the end of the day, he had left the building.
The phrase, 'it's better to be cruel than to be kind', comes to mind...like you said, if you cannot deal with basic boolean clauses in a very basic if statement, then the software industry is not for you.
Admin
Admin
NOT(OR(AND(Boolean1,NOT(Boolean2)),AND(Boolean2,NOT(Boolean1))))
Admin
Yeah, and codebases with that line usually have a couple of instaces of nested ? that will, when simplified amount to boolean foo = bar; or boolean foo = !bar;
And the reason for those ? lines is that an unskilled programmer found a reasonable use of ? and was struck by how elegant it was, and started using it as sort of a golden hammer. See that a lot when I get new teammates.
Admin
Or fruits.
Just chewin'
Admin
2nd one is ActionScript - taken from here: http://aftc.googlecode.com/svn/trunk/AFTC_Template/src/aftc/utils/boolean/BooleanUtilities.as
Admin
In some situations that is completely sensible. In a hardware simulator, for example. Verilog uses four state logic:
Admin
But it is backwards... If b was 0 (false), (b) would evaluate to false. But if (b == true), and "true" is "0", this would match and evaluate to true... The opposite result.
My guess is that a pure-C fanatic did this to booby trap a Java/C# programmer that kept adding all of these useless "== true" clauses all over the place.
The people that followed the old school "if (b) " would be unaffected, but all of the code that had "if (b == true)" would start behaving like opposite-day was in effect!.
It was probably a practical joke that someone forgot to take out.