October 25, 2007
Comments in Code
A trip down memory lane: Years ago, while attending the fine institution known as The Pennsylvania State University, I anxiously awaited a grade on my latest programming assignment. Pardon the pun, but I was trying to avoid another C on my C++ programming project. I had thought that the last program I turned in was completed perfectly, but I got knocked by the professor for not commenting my code. I thought it was nonsense. And then when I got my 2nd consecutive grade of a C –, I thought it was even more nonsensical. Why would I need to comment my code? My thoughts were that the professor was an expert in this field and that he and should know exactly what my code meant. … Now back to the present day.
What a fool I was! (Note - I am sure that those who know me are probably taking a screen print of this right now to provide evidence of my admitting this fact.) I have come full circle since my college days, and thinking back, if I were that professor, I would give students an F for lack of comments.
Recently, I have been fortunate enough to be working with some historical code. How I wish that some of these developers had taken a class with my old professor! Why is it that our natural reaction is to not want to explain what it is we are coding? Do our brains start to only think in code while coding so that we can not translate our thoughts into friendly English text? Even when comments do exist, often the sentence structure is such that it appears that the developer was only allocated 20 words with which to explain themselves. Is there some misconception amongst programmers that if you can read prose, or actually write in prose, then you can not be a true programmer?
I’d like to change that notion. The problem is that, in many instances, comments are still added because the professor requires them, or the development guidelines require them to be there. They should be added to not just repeat what the code does in pseudo-code, but also to answer the question that can never be apparent – why is this code here and what is its purpose? I already know the answer that some programmers will give me when reading this – “that will make the code less clean.” I disagree with the notion that comments explaining the code dirty it up. Personally, I think it is cleaner when you can transition it to somebody years later and they can understand it.
Now excuse me while I go back and try and figure out what the heck I was thinking when writing this code from 2 years ago…
Posted by Bryan J. Holmes at October 25, 2007 09:37 AM
Trackback Pings
TrackBack URL for this entry:
http://www.jandrconsult.com/cgi-bin/mt/mt-tb.cgi/89
Comments
Post a comment
Thanks for signing in, . Now you can comment. (sign out)
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)