Saturday, October 08, 2005

Java API Pitfalls: Boolean.getBoolean(String)

Creating a public API is a task that should not be taken lightly. Any bad decision at this stage tends to become baked in, remaining there for posterity. One such lousy decision was made by an unnamed Sun engineer, eons ago when dinosaurs roamed the earth and the Java programming language was born.

I can imagine this developer thinking to himself "I really need a method that gets a boolean value from the system properties. Since this method returns a boolean, it obviously belongs in the Boolean class! Now let me think, should I call it getBooleanSystemProperty? Nah, that's way too long and I don't have an IDE with code completion. I'll just call it getBoolean - it's so much catchier and saves typing fourteen characters!".

And thus, the Boolean.getBoolean(String) method was born.

Cut to a few years later, a programmer needs to convert a String to a boolean value. Remembering that there's some static method in the Boolean class for this, she types "Boolean." and waits for the code completion popup to appear. Her eyes scan the list of method names, and stop at getBoolean. "This is it" she thinks, failing to notice that further down the list there's also a method called valueOf.

A few weeks pass until somebody notices that no matter what string value the program receives, it always treats it as "false". It takes another few hours to trace the cause of this bug to the innocent-looking call to getBoolean.

Now you may think that this story is fictitious, but I've seen this mistake being made at least twice. And the fault lies entirely with this method, which is located in the wrong class and has a name that does not convey its purpose accurately. If you don't believe me, just ask Glen Stampoultzis.

What surprises me the most is that Sun didn't choose to deprecate this method, as a way of admitting that they screwed up and flagging that it should not be used. And so, it remains a part of the core Java API, like a landmine waiting quietly to be stepped on by a poor victim.

27 Comments:

At October 08, 2005 2:41 PM, Blogger A. Hunter said...

Good, helpful page. Keep on going.

Best wishes

Adam H.

 
At January 19, 2006 12:58 PM, Anonymous Anonymous said...

For everything that's toad and holy - what does Boolean.getBoolean do?

I would also like to notice, that khmkm is NOT a word, contrary to what the little Word Verification window says.

 
At January 22, 2006 10:53 AM, Blogger Itai Shirav said...

Boolean.getBoolean("xyz") returns the value of the xyz system property, after interpreting it as a boolean. So mistakenly doing Boolean.getBoolean("true") will always return false (unless there's a system property called "true", whose value is also "true"... that's actually a workaround!)

 
At June 09, 2006 9:11 AM, Anonymous Edmund said...

haha, I am the one who is cheated by getBoolean() and guided by google to here..
Thanks very much for ur point.

 
At December 13, 2006 1:48 PM, Anonymous ivy said...

I just found a bug based on this 'feature' in some code I have to bugfix. Convinced I was not the only one amazed by this method, I googled and found this page.

Couldn't agree more!

 
At January 30, 2007 10:55 AM, Anonymous Bruce Mundin said...

Amen!

A fellow victim

 
At January 16, 2008 2:41 AM, Anonymous Anonymous said...

latest casualty. this stupid bug caused a $50,000 billing error in my company. The API designer should be shot. SUN should also be shot for not marking this API as deprecated.

 
At January 22, 2008 8:08 PM, Anonymous ragchi said...

Am also misled by this method and googled my way here in search of explanation. The explanation here is dramatic, though very helpful. Thanks

 
At January 29, 2008 8:18 AM, Blogger niroshinie said...

happened to me too! took me hours to locate it.
:(
changed it to Boolean.parseBoolean().

 
At March 27, 2008 8:17 AM, Anonymous Anonymous said...

I am another victim ...

 
At April 19, 2008 6:58 AM, Anonymous Anonymous said...

hours and hours of head banging, then I find this post. THANKS. What an annoying waste of time it's been using this method!!

 
At May 20, 2008 7:34 PM, Blogger Matt said...

<--- Victim. Thank You!

 
At June 26, 2008 10:26 AM, Anonymous Anonymous said...

victimcount++

 
At August 05, 2008 1:34 PM, Anonymous Ariel said...

I'm another victim. This is a poor method indeed

 
At August 13, 2008 1:41 PM, Anonymous Anonymous said...

++

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4533891

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4206601

Looks like Java developers cannot fix that bug because of a deprecation policy requirement.

Perhaps the fact Java is open source might allow revising that policy ?

 
At November 10, 2008 7:59 PM, Blogger Joga said...

++

 
At November 12, 2008 2:30 PM, Anonymous Anonymous said...

Fellow victim
it cost me 3h...

 
At December 11, 2008 8:24 PM, Blogger TheMuuj said...

I just fixed a series of bugs caused by the existence of this method. The worst part is, I'm sure the same code was copy-and-pasted into other project that I'm not working on.

I spotted the problem, and I've only been doing serious Java work for a few months.

 
At February 13, 2009 3:05 PM, Anonymous Mike said...

After 2 hours of making 100 sysouts around Boolean.getBoolean I noticed your page :-) Meanwhile doing some silly things like

if ("true".equals(x)) {
y= true;
}

Thanks for the clarification of getBoolean...

 
At March 12, 2009 6:42 AM, Blogger Michael Roger said...

I was fooled by getBoolean!

Thanks for letting me know I'm not alone.

 
At April 28, 2009 12:51 PM, Blogger Barry DCunha said...

i got fooled as well. googled to check the api comment and saw this helpful page.

 
At July 03, 2009 11:21 PM, Blogger Cokoyan said...

Grrr thanks...1 hour for me ^^

 
At August 22, 2009 2:08 AM, Anonymous Anonymous said...

2 hours for me on this bug... much worse, it was 2 hours before making a demo and the stupid thing stopped working just because of this bug...

 
At October 09, 2009 10:17 PM, Anonymous Anonymous said...

Commenters: Oh sure. Let's shoot the API guy! Let's burn getBoolean() in effigy! You losers. How about you being more careful of the sharp edges of the tools you have, instead of crying "victim" for every clumsy nick?

 
At January 14, 2010 10:58 AM, Anonymous Anonymous said...

I have to agree with the previous anonymous coward.. The method is clearly documented. Every developer should know that making assumptions is potentially deadly. So is not writing tests to verify assumptions.

Using Eclipse it is easy enough to have an open javadoc view. The documentation is there.. use it.

 
At October 18, 2011 2:46 AM, Anonymous Anonymous said...

This requires a Good Bump...Was pulling a few strands of hair over this one.

(this came up in Google's top few search)

*Damm the getBoolean(string)*

 
At December 04, 2012 11:10 PM, Blogger sdk22 said...

A fellow victim as well. Almost at the end of 2012. Sun(err..Oracle - fix this).

 

Post a Comment

<< Home