Stupid ways to check the condition of a boolean

I thought I had seen most things, but yesterday was given some code snippets (not produced at Sun) on how you can make things much harder than they really are.

Say that you want to check the condition of a Boolean. Most people would use

if (b) {
    // do stuff
}

But someone didn't think that was clear enough, so they used

if (b != false) {
    // do stuff
}

I though that was pretty bad, but then this masterpiece appeared

if (b.toString().length() < 5) {
    // do stuff
}

How on earth did they come up with that? The worst thing is that it is supposed to come from a real code base. I pity the ones who have to maintain it!

Comments:

It only makes me laugh. :)

Posted by Diego Femenias on March 05, 2007 at 06:17 AM PST #

That last one has to be a hoax! If not, I see a written warning heading to the coder concerned for gross stupidity: you have to be doing that ON PURPOSE to piss everyone off, like the time one of my guys ... ahem, my lawyer says to stop here ... B\^>

Posted by Damon Hart-Davis on March 05, 2007 at 06:17 AM PST #

Time for some Jackpot cleanup.

Posted by Alexis MP on March 05, 2007 at 07:00 AM PST #

Actually there is a valid reason for using b != false and that is in situation like this:
Boolean b;
if (b != false) {
    // do stuff
}
where b is a Boolean wrapper class and the java version is 5.0+, because then Auto-Unboxing is involved and you are testing if b is true or null But there is nothing that comes to my mind to justify the second snippet :-D

Posted by Igor on March 05, 2007 at 06:55 PM PST #

Well, the snippet below IS produced at Sun (from /kernel/drv/scsi_vhci.conf).
#
# mpxio global enable/disable switch: setting mpxio-disable="no" will activate
# I/O multipathing; setting mpxio-disable="yes" disables this feature (do
# not remove this property).
#
mpxio-disable="no";

Posted by Jonas on March 05, 2007 at 08:23 PM PST #

Why on earth do people tend to complicate things ?
if (b.toString.substring(0, 1).equals("t"))  {
   // do stuff
}

Posted by Maintainer on March 07, 2007 at 03:36 AM PST #

Post a Comment:
Comments are closed for this entry.
About

martin

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today