Comment the class before coding it

Jun 10, 2007

The planning phase of most projects no matter the size result in some kind of documentation or manuals for coding. It could be a rough sketch of the data flow, a UML diagram, feature specifications etc. Some big enterprise projects probably also have a detailed explanation of the individual classes, so that the developers knows exactly what the class has to do and why.

Then again, most projects do not undergo such an elaborate planning phase for various reasons. It might be a one-man hobby application or a simple little website. We all do them from time to time so here is a little trick for adding a planning phase while coding.

Create a new class

Because you haven’t done any real planning, you go ahead and creates a new class with a vague idea about what it has to do and why it has to do it. Instead of just adding properties and methods right away, you can force yourself to think about it and thereby do a little planning. That will help you to keep the class more focused and refactored as you write it and you might find that you need another class instead of just the one.

Comment it

With only the class declaration written in your editor, try to comment the class as thorough as possible. At least answer the three questions what, why and how. This will force you to think about the responsibilities, interface, cohesion and a lot of other things that probably saves you a lot of coding time and effort in the end.

The best thing about this approach is that you get right into the code without doing pre-planning, which most of the smaller hobby projects don’t have. You get the benefits of the planning with the pleasure of just diving into the code right away. It can never remove the need for proper planning for more important projects, but it will get your home project running smoother and better architected with little to no effort.

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (4) -

Joe
Joe United States
6/11/2007 2:14:30 AM #

This is something I have been trying to do lately.  I must say not only does it help you think through HOW to do it, but (for me at least) it helps me stay focused during development. A big bonus is that any developer can pick up your code and right away know what is going on.  I would recommend this approach to anyone.

Great post!

Ryan Anderson
Ryan Anderson United States
6/11/2007 2:32:44 AM #

I have been doing this for several years. I also expand on a method level, when I create a method I will comment out the steps it needs to take to accomplish its purpose. One of those things that really is never talked about, but is highly recommended by many! Nice!

NinjaCross
NinjaCross Italy
6/11/2007 9:04:22 AM #

This strategy is so similar to the "test-first" one, a wisely used practise in eXtreme Programming (since Unit Tests are tightly realted to XP)
I usually develop in a similar way, applying this concept to the interfaces building.
My preferred business-logic design process starts with thinking at the functionalities in terms of methods constrained by an interface, that's usually fully documented in terms of working context.
In this way, I've got two advantages:
1- The BL is designed in terms of constrained functionalities
2- The whole architecture is enforced with a interface/implementation dichotomy, that ensure strenghtness during long-terms refactorings.

Immigration Officer
Immigration Officer India
10/16/2007 6:06:47 AM #

Hi! That was a good post for what i was looking for the .net. I am in a need for creating class in the .Net basis. This was really a good kind of explanation for the plan of how to create a class. This will certainly help me to create a new class. Thank you very much for this valuable information.

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.