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.
26 Comments:
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.
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!)
haha, I am the one who is cheated by getBoolean() and guided by google to here..
Thanks very much for ur point.
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!
Amen!
A fellow victim
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.
Am also misled by this method and googled my way here in search of explanation. The explanation here is dramatic, though very helpful. Thanks
happened to me too! took me hours to locate it.
:(
changed it to Boolean.parseBoolean().
I am another victim ...
hours and hours of head banging, then I find this post. THANKS. What an annoying waste of time it's been using this method!!
<--- Victim. Thank You!
victimcount++
I'm another victim. This is a poor method indeed
++
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 ?
++
Fellow victim
it cost me 3h...
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.
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...
I was fooled by getBoolean!
Thanks for letting me know I'm not alone.
i got fooled as well. googled to check the api comment and saw this helpful page.
Grrr thanks...1 hour for me ^^
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...
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?
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.
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)*
A fellow victim as well. Almost at the end of 2012. Sun(err..Oracle - fix this).
Post a Comment
<< Home