JavaOne 2008 - Defective Java Code: Turning WTF Code Into A Learning Experience
May 7, 2008 12:04 am JavaThe session “Defective Java Code: Turning WTF Code Into A Learning Experience” was presented by William Pugh, Professor, University of Maryland. Pugh was one of the creators of the Java FindBugs application, which is a profiling tool that analyzes Java source code for common and “not-so-common” bug patterns. The presentation itself was excellent, but is very difficult for me to blog. It primarily showed instances of code with flaws, and explanations about why the code is flawed and how to fix it.
Pugh showed instances of leading software packages and libraries that he has scanned including the Java API, Eclipse, and Glassfish that contain major coding flaws. One flaw in particular that I’m guilty of doing all of the time involves the class java.text.DateFormat. I was not aware that this class is not thread-safe. Therefore, you should not create a constant like this:
public static final DateFormat DATE_FORMAT = new SimpleDateFormat(”yyy-MM-dd”);
The DateFormat class uses an internal static date for doing formatting. If two threads hit the same instance of DateFormat, incorrect results can be returned.
There is a web site called CodeSOD that contains the “Daily WTF Code Snippet of the Day“. It is a posting of a bug or bug pattern that other developers have encountered. It’s definitely worth a read.
When this presentation is posted to the JavaOne web site, I will be sure to link to it from this blog entry. Looking at some of the code bugs, is really worth the time.
