Disputed Code Comments vs. Self Describing Self Documenting Code

Last week I have been reviewing some implemented features on our product, it works fine but not well written, got to have some refactoring specially my rule on comments.

Perhaps this is one the most disputed practices in software development, code comments. Personally, I seldom/don't comment my code. A well defined and structured code with self-describing and self documenting names and declarations is sufficient enough to make it understandable and maintainable at some point. Task aware lines such as TODO, HACK, REVISE tags works well but not all times unless it is integrated with the Issue Tracking tool or IDE. In some cases code comments is a residue of pseudo-code and I suggest it be removed from release code.

I have seen several cases where comments serve as documentation and explanation of a plain and simple methods. It often comes to a point that members of the team or API users are forced to read a bunch of comments first before understand the program flow. Even experienced developers tried to compensate their complex code, lacks of abstractness and intuitiveness, complex class' responsibility, and lacks of refactoring by explaining how code works on code comments.

Code comments also occupies a lot space on code view and added complexity if not well written. A comment that says "//initialize the treeview with list of products" on a method "InitializeProductTree(List<Product> productList)" doesn't make sense!

Consistency of the comment to the intent of the code it describes is not guaranteed. Developers that writes pseudo code first before implementing through methods will likely forget to update the comments if a logic in code change and thus code and comment becomes inconsistent and inaccurate.

Vanity. This is funny as I have seen some codes that serve as vanity and aesthetics to show style. It looks beautiful seeing those green lines. Taking a closer look and reading it, it tells me something. "I'm here, but you ain't gonna need me".

XML documentation is best placed in a separate xml file so the code base size will be reduced. A fully xml inline-documented class when not collapsed gives complex view and forces the API user read the comments first.

When to put comments anyway?

I don't think there's general rule when and when not to place comments. IMHO, its mainly common sense.

Intent. Comments describes the intent of the method or class. It should tell why the code was written, at what feature or requirement it serves.
Responsibility. Commends describes the responsibility of the code and its participation in the implementation feature or fullfillment of a requirement.

Guilty? Share your side.

Comments

# cruizer said:

agree with you. i prefer self-documenting code. when self-documenting code escapes me, i prefer to put in comments that document intent (the "why," not the "what" or "how")

Sunday, September 02, 2007 10:51 PM
# lamia said:

Guilty your honor! Well, I feel guilty most of the time for not putting comments on my code. Sometimes though, I feel really stupid for doing things such as

/*close the output stream*/

outputStream.close();

hehehe. I'm fond of putting comments on my XML such as

<!-- Whatever I can think of -->

because I guess, in my current project we really need to keep track of everything that changes(thus the need for comments). IMHO, self-documenting code is ok if

1. Your method tells what it does

2. Your variables tell who they are and what they are for

3. You don't get up to atleast 3 levels deep in your inheritance tree

Darn... I want my own copy of code complete... :(

Monday, September 03, 2007 12:34 AM
# cruizer said:

well i don't see any reason for

1. a method that isn't true to its name

2. a variable that doesn't agree with how it was named

3. too much inheritance ("favor composition over inheritance" -- GoF)

:)

Monday, September 03, 2007 1:09 AM
# lamia said:

But things like those exists! A sad truth!

Tuesday, September 04, 2007 3:04 AM
# cruizer said:

well those things do exist, but at least I can say they're not from me :P so we should do well to avoid making trash code like that.

Tuesday, September 04, 2007 6:13 AM
# cruizer said:

and if you ever see something like that and you have the system privilege to change it, by all means REFACTOR ;)

Tuesday, September 04, 2007 6:14 AM
# lamia said:

Unfortunately, I don't... -_-

Tuesday, September 04, 2007 6:53 AM

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: