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.

You may also like...

1 Response

  1. Assertions are just as easy to read as your if statements. At least, that’s what I think. But I’m not sure what happens to assertions when you compile a project for Release mode.

    I know from experience with C/C++ compilers that assertions only raise an error in Debug mode. The following link also suggests that this is the case for C#: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdiagnosticsdebugclassasserttopic1.asp

    You if statement check however will always work. So I think you should keep using that.

Click on a tab to select how you'd like to leave your comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.