The FindBugs Blog

Monday, May 14, 2007

A closed source WTF: the longest second

OK, since I've pointed out a number of times that open source code contains a number of embarrassing bugs, I want to just emphasize that I've seen embarrassing code in every code base I've examined, including some propriatary closed source code bases.

I just want to share the following code, found in the code base of a highly respected tech company.

private static final String SECOND = "second";

public static String getTimeDelayStr(long delay) {

double roundDelay = 0;

// seconds
if (delay == 1)
return String.valueOf(new Double(Math.ceil(delay)).intValue()) + " " + SECOND;
....

2 Comments:

At 1:52 AM, Blogger Deepak Jain said...

Hi,
I started using FindBugs and came up with this error
"Usage of GetResource may be unsafe if class is extended
Calling this.getClass().getResource(...) could give results other than expected if this class is extended by a class in another package."
I tried a lot to understand it but I could not understand. Can anyone please give more details and as to why this is a bug?

Sorry I was not able to post to this blog so i had to make a comment. If you dont mind please add me to the blog so that i can post questions.[deepujain@gmail.com]
Thanks
Deepak

 
At 7:23 AM, Blogger Deepak Jain said...

I have another question.
Please answer:
After running findBugs() i had an error that describes. I understood the solution that inner class must be made static but i did not fully understand the solution. Can anyone please explain this.

Should be a static inner class
This class is an inner class, but does not use its embedded reference to the object which created it.  This reference makes the instances of the class larger, and may keep the reference to the creator object alive longer than necessary.  If possible, the class should be made static.
Thanks
Deepak

 

Post a Comment

<< Home