The FindBugs Blog

Monday, September 18, 2006

How do you fix an obvious bug?

Consider the following code (from Eclipse 3.2, org.eclipse.jdk.internal.core.NamedMember):

for (int i = 0; i < length; i++) {
String typeArgument = typeArguments[i];
typeArgument.replace('/', '.');
if (i < length-1)

FindBugs points out that the return value of replace('/', '.') is being ignored and thus the call has no effect. It is very tempting to just "fix" the code to get

for (int i = 0; i < length; i++) {
String typeArgument = typeArguments[i];
typeArgument = typeArgument.replace('/', '.');
if (i < length-1)

But does this actually fix the code? We probably don't have any test cases where the type argument contains a '/', or else we would have already have fixed the code. Or perhaps the buffer should contain slashes rather than dots, and the fact that the return value from replace is ignored is what makes the code correct. Or perhaps '.' would be the correct answer, but other code in the system depends upon the error in this code.

Static analysis tools, such as FindBugs, don't actually know what your code is supposed to do. Instead, they typically just find inconsistencies in your code. Invoking a String replace method and ignoring the return value is weird or inconsistent, but we don't actually know that using the return value is more correct.

So no easy answers. On recommendation is that when you see an obvious bug, maybe you shouldn't just bang out a quick fix. Instead, figure out why tests aren't showing a problem with this code, try to document the intended behavior and write a test case showing the existing code gives incorrect behavior.

Then, go make the obvious fix.

Bill Pugh


At 10:22 AM, Blogger Isabella Olivia said...

This information is impressive; I am inspired with your post writing style & how continuously you describe this topic.

Pawn Shop

Pawn Loans

Pawn Shops

Pawn Loan

Pawn Shop near me

At 2:19 AM, Blogger CIIT Noida said...

What a wonderfull post! thank you... read with pleasure!

Best Linux training in Noida
Linux Training Institute in Noida

At 2:25 AM, Blogger CIIT Noida said...

Thank you for your post, I look for such article along time, today i find it finally. this post give me lots of advise it is very useful for me.

Best bca college in noida
Top bca colleges in noida

At 4:22 AM, Blogger mohit sona said...

Its like you learn my thoughts! You seem to understand a lot approximately this, like you
wrote the ebook in it or something.

ivanka trump hot pics


Post a Comment

<< Home