Monday, August 27, 2007

Java :: FindBugs :: Dead Store

I ran FindBugs on one of our projects and I had a warning on some code similar to this, let's see how the code was written. This is just a sample code to show an example, don't expect any logic here :-)

// DeadStoreExample.java
import java.util.ArrayList;
import java.util.List;

public class DeadStoreExample {
public static void main(String[] args) {
List list = new ArrayList(); // Dead Store here.
list = getList();
System.out.println(list);
}

private static List getList() {
// Some intense operation and finally we return a java.util.List
return new ArrayList();
}
}

FindBugs reported me this problem: Dead store to list in DeadStoreExample.main(String[]). When I first saw it, I was like "What do you mean by Dead Store?" And when I looked into the code I saw a List object being created but not used. Instead, in the next line the reference variable is pointing to some other object in the heap. The object created on line number 6 is never used and hence it's a dead store.

So, what is a Dead Store? If we assign a value to a local variable, but the value is not read by any subsequent instruction, then it's called a Dead Store.

I love FindBugs, the thing that I like the most about FindBugs is that it tries to find all the lame bugs which many veteran programmers accidentally make. I always like to set my compiler warnings very high (even for my Javadoc comments). And now I have developed a habit of running FindBugs before I make a build or commit my code. It's one of those things like Unit Testing, you must try and get infected.

If a compiler or a tool can catch a bug, then I will always go for it. No one can write bug free code, at least I have not seen anyone till now. And I believe in automating anything that is possible so that we never have to do that manually again. That's why we have computers :-)

If you're not using FindBugs, I highly recommend that you to use it.

9 comments:

Anonymous said...

How to solve this problem please give me solution. I am also facing the same error in fund bugs.

Srikanth said...

It completely depends on what you are doing in your code. Look for where it is complaining, and see if you're reassigning the same variable to some other value without using the previously calculated one.

As an example to solve the example code above, I'll change the following statements

from:

List list = new ArrayList(); // Dead Store here.
list = getList();

to:

List list = getList();

Hope it helps!

Anonymous said...

helped me! thanks :¬)

Anonymous said...

wow thanks!
i didn't realize i actually got a list as a return value ^^

firebug is great! :)

Anonymous said...

lol, i was trying to say findbugs is great *laugh*

Srikanth said...

LOL, I would say both are great!

For a moment, I was confused if you were making fun of me :)

பார்த்தசாரதி செல்லதுரை said...

very useful on srikath

mayor said...

it's very usefull..thanks

mayor said...

it's very usefull.. thanks

http://qpleset.wordpress.com

Post a Comment