Smells to Refactoring

Today I was browsing a site about Java. Don’t get me wrong, I’m not switching, but I found something interesting there. And the fact that the url was www.java.net made me go look. 😉

We’ve all seen code that smells. Code that smells like refactoring. On this Java website they created a rather extensive list of smells, with proposals on how to remove the smell and create ridiculously good looking code.

The first one is about comments in your code. As the description says, it “should only be used to clarify ‘why’, not ‘what’”. One example given is the Extract Method, which I both like and dislike. I know about the SRP, but just refactoring everything in its own class and/or method somethings feels like going a bit too far. Although with the example given, you at least get an idea. The Rename Method is also one to remember if you don’t already do so, but the Introduce Assertion I’ve never used it like that. Normally I just check for null references with if statements, and this is indeed shorter but might take a little longer to read.