// 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.