More Evil

Posted June 25, 2009

There are many areas of Java which are considered "evil" - static methods, as an example. In general terms, I do agree with the idea of avoiding such mechanism ("import static" anyone?) even going so far as to try to avoid singletons as much as possible.

However, I do wish to make a case for when using such things is not necessarily a bad thing. I lean towards the idea that such things are not inherently bad in isolated environments, where not even threading is something that needs to be considered. A good example of this is JUnit tests. They are isolated, self-contained, single-threaded little programs where readability is typically vastly more important than "correctness" of code.

We clearly see this in the way NetBeans makes use of JUnit 4 - by default the line "import static org.junit.Assert.*;" is in your newly generated source file. What this facilitates is that the actual test class no longer needs to extend "TestCase" (as was necessary under JUnit 3), but on a readability level, we will code "assertXXX" instead of "Assert.assertXXX".

Sometimes, "evil" really is necessary ...

Comments

There are no comments for this post.

No comments found

Add comment

Comments for this post have been disabled.

Visit my Friends and Family

If you've enjoyed my site, please take a moment to visit my friends and family, many of whom have some interesting insights, and entertaining thoughts and ideas.