The Pragmatic Craftsman         :: Simplicity from complexity :: | About Me |

You are here: The Pragmatic Craftsman > Better Coder

Go to: « Thoughts on Object Oriented Programming | Keep Your Methods Cohesive: Short and Concrete »

Write Self-Documented Code

I don't know if anybody actually reads the comments that we sometimes write as Javadoc. Do you? I don't, most of the time.

That's why I think it's important to write code that speaks for itself and that does not need a comment. I'd go this far: if you need a comment, your code should be refactored and made easier to understand. (What does it mean easy? Easy for somebody else to understand -- important to keep this in mind as well.)

So how do you write self-documented code?

Use good names. For class names. For variable names. And for method names. Don't name your class WriteData or ConnectionInfo. They should have better names. They should be more specific. You should be able to tell what a class or a method does by its name.

Be short and specific. I hate it when I have to modify a 1000+ line class. I hate it when I have to modify a method over 100+ lines (you should have rules for these :- )). Have classes that have a specific set of tasks to accomplish. Have methods that do one thing and do it well. In this regard, it helps to have good and specific names so you know exactly what your class or method is doing, and whether you need to assign the new responsibility to a new class (assigning responsibility is crucial in OO development, as I said before).

Abstract it when you can. When you have some complicated logic, encapsulate it in a class, hide the complexity. By doing so, you're putting it in one place and you don't have to delve into details of how it works, as long as it works -- you assign it a responsibility and you hold it accountable. If you keep adding complicated logic to a class or a method, you're messing up the class, making it harder to undertand and modify. If it was seperate and nicely abstracted, you can modify it with more confidence. Maybe it's better to add a new class, a new method? Ask yourself that before adding any complex logic.

When writing a comment, focus on "why" not "how." Make every comment be of substance. Comment surprises.

In some cases you have to have complex logic. Add a comment then. Make it easier for you and for others. Prepare the reader for complex logic with a comment, explain why it is being done that way.

I think writing a self-commented code, code that is easy to read, is a crucial part in our day-to-day programming. It's one of the things that has huge effect -- in both positive and negative direction.

To be a good coder, to become a better coder, make sure your code is easy to read.

Related
Code Complete, Steve McConnell's excellent book (my previous recommendation), required if you want to become a better coder


Comments

Post a comment









Remember personal info?







Go to:
   « previous entry: Thoughts on Object Oriented Programming
   » next entry: Keep Your Methods Cohesive: Short and Concrete

Random Quote

Search

 

Topics

Architecture & Design :12
Better Coder :29
Books :43
Books Recommended :18
Buzzwords :5
Career :25
Craftsmanship :15
Java :15
Quotes :25
Recommended :9
Software Engineering :3
Uncategorized :33
Web Development :1

Archive

May 2008 (1)
April 2008 (2)
March 2008 (1)
February 2008 (1)
January 2008 (2)
November 2007 (1)
October 2007 (3)
August 2007 (3)

...since January 2002

Currently Reading


:: See list of books I finished reading

Info

© 2001-2008 Stanley Kubasek About me :: Contact me