It's Always Time to Refactor- Part IIIProgram wilth Refactoring Principles in Mind | | Why should I refactor my code? In fact, what is refactoring, and why should it concern me? Part III. You can catch up to speed on refactoring by reading my first article on Refactoring or Refactoring Part II.
This is my final article (until I write another) on refactoring. I have covered four topics in my first two articles and will cover two additional topics in this article.
Organize your code in your form and classes into VB.NET #Regions (#regions in C#). If you are like me, many of your classes will have hundreds, sometimes thousands of lines of code. I am not going against my earlier suggestions of writing modular code. There I was speaking of keeping your methods small and performing one logical function in each method. That does not mean that one object (Class or Form) may not have scores of methods. I do not think that you should split up the functionality of an object to meet any arbitrary maximum number of methods.
However, I do believe one of the greatest organizational tools that Visual Studio .NET provides is the concept of #Regions. I have gotten so much in the habit of organizing all of my code into #Regions that I wrote an Add-in called Net Class Organizer. For several months that powerful add-in has been free for the downloading on KnowDotNet. That code has now been included in NET Refactor.
I personally use a variation of sets of regions such as the following general regions.
#Region " Class Level Variables and Propertys "
#Region " Public Methods "
#Region " Private Methods "
#Region " Event Handlers "
Sometimes, when my classes are larger, and especially when I nest several Classes in a Namespace or nest Classes within Classes, I will set up regions that organize methods by functional names that describe what the methods in the region do. When you organize your methods into region blocks like this, you can collapse the regions that you are not currently working with and then your classes do not appear to be large and unmanageable even though they in fact may be very large. Regions make your large classes manageable.
At the risk of becoming too commercial, NET Refactor has several features that make the organizing of your classes into regions an almost automatic process. You can move a method from its current location to an existing region by a right-click in the method and selecting a move option. The code will be moved to the selected region automatically without your having to Cut and Paste.
Keep Your Comments Up To Date
I am a firm believer in comments. When a block of code is several weeks old, even if I wrote it, I can not easily remeber what I was trying to accomplish if there are no comments. Someone has said that "no memory is as long as a pencil." You could say the same thing about a comment...as long as it is kept up to date. If you change the code, change the comment. The time spent will not be wasted. In fact, if the code changes its functionality, and there are comments that are no longer valid, it really would be better if the comments were not there. This does not mean they should be deleted; rather they should be updated to reflect the code changes.
One of the greatest deterrents to updating comments is that they are not easy to maintain without winding up with long lines and short lines, choppy thoughts, etc. Again, this is one of the reasons I wrote such a product as NET Refactor. The Smart Commenter feature provides a word wrapping technology that marks comments, in an unobtrusive manner, so that you can easily retrieve them into the wrapping dialog in the future and change them just as if they were being written and maintained in a word processor. It even has a little artificial intelligence that does a good job of figuring out where the developer was using sentences and lists even when they did not use the Smart Commenter to compose the comments originally.
That just about exhausts my thoughts on refactoring for today, but I plan to write other articles to suggest ways to keep your code clean, readable, and maintainable.
Check out the many features of NET Refactor.
|