Friday, December 09, 2005

Refactoring and comments

If you have not read Martin Fowlers book Refactoring then you should. There is no book that I have read that has helped me more in what I do everyday. The book basically gives techniques on how to make code better and easier to work with after it has been written. One of the most used and common refactorings is "extract method". This is where you take a chunk of code and pull it into its own method. (eg. http://www.refactoring.com/catalog/extractMethod.html).

I so love the idea that a method does one task and that is it. If it does two tasks then the method calls two methods (one for each task).

Now that I have been using this technique for a while I have found that you don't need comments anymore. The reason for this is that when you break everything into small and well named function that anyone can read the code without comments.


if I gave a method like this:
function LoadTransactionType() as boolean
sub SelectFirstRow()
sub AddTransaction()
sub DeleteTransaction()

It is pretty easy to see what these methods do and the code in those methods only has 5 - 20 lines of code which then might call another simply named method.

I have now noticed that whenever I open code I look for comments. Comments usually indicate that the code is doing something not obvious so I extract the method and delete the comment.

I still feel weird that most of my code now has little to no comments in it but it is still increddibly readable and easy to follow.

0 Comments:

Post a Comment

<< Home