// DeadStoreExample.java
import java.util.ArrayList;
import java.util.List;
public class DeadStoreExample {
public static void main(String[] args) {
Listlist = new ArrayList (); // Dead Store here.
list = getList();
System.out.println(list);
}
private static ListgetList() {
// 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
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:
How to solve this problem please give me solution. I am also facing the same error in fund bugs.
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!
helped me! thanks :¬)
wow thanks!
i didn't realize i actually got a list as a return value ^^
firebug is great! :)
lol, i was trying to say findbugs is great *laugh*
LOL, I would say both are great!
For a moment, I was confused if you were making fun of me :)
very useful on srikath
it's very usefull..thanks
it's very usefull.. thanks
http://qpleset.wordpress.com
Post a Comment