hit counter
   
 

Random ramblings of a paranoid git
"The question is not if you are paranoid, it is if you are paranoid enough."


All | Security | Work | Wine & Dine | Leisure

   
   
20070305 Monday March 05, 2007
Stupid ways to check the condition of a boolean
Permalink | Comments [6] | 2007-03-05 12:58

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 02:17 PM 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 02:17 PM PST #

Time for some Jackpot cleanup.

Posted by Alexis MP on March 05, 2007 at 03:00 PM 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 06, 2007 at 02:55 AM 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 06, 2007 at 04:23 AM 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 11:36 AM PST #

Post a Comment:

Comments are closed for this entry.
   
XML
« May 2008
SunMonTueWedThuFriSat
    
1
2
3
4
5
6
7
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
       
Today


Old entries


Bloggtoppen.se
OpenSolaris: Love at First Boot