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!






Posted by Diego Femenias on March 05, 2007 at 02:17 PM PST #
Posted by Damon Hart-Davis on March 05, 2007 at 02:17 PM PST #
Posted by Alexis MP on March 05, 2007 at 03:00 PM PST #
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 :-DPosted by Igor on March 06, 2007 at 02:55 AM PST #
Posted by Jonas on March 06, 2007 at 04:23 AM PST #
if (b.toString.substring(0, 1).equals("t")) { // do stuff }Posted by Maintainer on March 07, 2007 at 11:36 AM PST #