A Sunny Commune - Cheng's Blog

« gnome-panel-screensh... | Main | IllegalAccessExcepti... »

http://blogs.sun.com/chengfang/date/20060328 Tuesday March 28, 2006

Please use Boolean.getBoolean(SOME_FLAG_KEY)

Boolean.getBoolean(String) is one of the most useful convenience methods.  It gets the boolean flag in system properties by the name.  But it has not been used as widely as it should be.  Oftentimes I see code (recently written, not legacy code) like this:

String flag1 = System.getProperty(FLAG_ONE_KEY);
String flag2 = System.getProperty(FLAG_TWO_KEY);
if((flag1 != null && "true".equalsIgnoreCase(flag1)) ||
    (flag2 != null && "true".equalsIgnoreCase(flag2))) {
    //ok, do something special for this condition
}

Another wheel is reinvented with a lot of typing, while this could be as simple as this:

if(Boolean.getBoolean(FLAG_ONE_KEY) || Boolean.getBoolean(FLAG_ONE_KEY)) {
    //ok, do something special for this condition
}

This method is in JDK as early as 1.0.  If such a good one got ignored by so many for so long, we can hardly blame users.  After all, an object-oriented mind wouldn't expect Boolean to retrieve a system property.  If this method had been System.getPropertyAsBoolean(String key), will it be better recognized and utilized?

technorati tags: ,

Comments:

Actually, I've found this method can cause a lot of trouble... See what I wrote about it here.

Posted by ishirav on June 12, 2006 at 06:24 AM EDT #

Post a Comment:
  • HTML Syntax: NOT allowed