Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

On the curiously overvalued nature of comments in source

Started by Oluseyi Jul 23, 2007 at 4:08 PM 65 replies 13.1k views
Original Post
Oluseyi
Oluseyi
We use subversion at work. A piece of code is refactored, such that a method that was previously called from one site is now called from another. Co-worker who did the refactoring left the old code in, commented out, and then placed above it:
# this is now called in <new call site>
Separately, a new routine was added to a source file and it concludes with:
# everything works
return 1;
In a third case, I see the following:
# if the volume doesn't exist, fail
return 0 unless -d $vol and -w $vol;
When did programmers stop understanding how to read code? Where did the notion that large blobs of editorial constitute good practice? (And don't get me started on density-reducing formatting practices, like turning a one- to two-line conditional into six lines comprised mostly of whitespace and a delimiter. It is not more readable!) Here's my position: comments are not documentation. Comments are context-providing quick notices to explain a rationale or behavior that can not be gleaned from the source itself. You changed something between two revisions? Write a commit log. Writing code effectively requires familiarity, which requires going over the code to determine what it does - no amount of commenting is going to change that, so don't use that as a rationale. Worse, comments have to be maintained - and ditto for perldoc/doxygen. We have a case of a function that used to return a hash now returning an object, so access to fields has been replaced with method calls of the same name. The perldoc, however, hasn't been updated, so an unfamiliar user would get the wrong idea. Write real documentation - concise, to the point, detailing overall behavior. Make interfaces self-documenting as much as possible by using legible, meaningful identifiers. In dynamic languages without type declarations, add a docstring and simple validation that spits out the correct usage (I love that I can type help(some_object) in Python and get a printout of how to use it in just a few lines on screen). Above all, use good tools! Too many bad commenting and nomenclature habits (appending a comment to indicate the end of every function, most Systems Hungarian atrocities, etc are a function of people not using their IDEs; dynamic language programmers writing inane comments is a symptom of not using interactive environments to explore the tool domain) simply come down to improper tool usage. These things are out there for you to take advantage of. Please, for the love of programmers everywhere, do! I think there's a misperception that entrée to a piece of code should be trivial. No, doggie. A programmer spends more time reading code than writing it, so acknowledge that situation and make that fact useful. Comment on unintuitive or grossly unfamiliar things. Don't make him waste his time reading your code to learn absolutely nothing. Thank you.
Ravuya
Ravuya
Comments: Why, not what.
Moe
Moe
I comment as heavily as I do (which isn't as bad as other people's code I have seen) because I don't do that much programming. Coming back 6 months later and trying to figure out what the heck I did isn't as bad. That, and if I ever get around to it, I could easily dump my source code into a tutorial and still have people make sense of it.

I do know where you are coming from though. It seems as though a lot of post secondary schools are putting more emphasis on commenting code rather than writing decent documentation.
Sneftel
Sneftel
I know this topic's been done to death before, but your last paragraph piqued my interest. A company that I worked for a million years ago had this really cool software development process thing they did called "code open-house". Every week or so, we'd all sit down around someone's PC, and they'd show us around some largish piece of code they'd recently been designing. The tone was that of someone explaining to a new maintainer how the code was structured, surprising design decisions, organizational idioms being used, etc. It was sort of like a code review, but with an emphasis on widening code ownership rather than improving the code. It wasn't a perfect process by any means--about a third of the team (you know.. the gammas) had their eyes glazed over the entire time, and it was sort of painful when the guy presenting had no idea how to convey concepts to other people. Nevertheless, I think it was good practice on the sort of skills that make someone a good commenter. If you can't see your code from the outside, not just WRT familiarity with the code but with your personal coding style as a while, you can't write useful comments.
RivieraKid
RivieraKid
ive never been anoyed by too many comments.
rip-off
rip-off
Is this something that falls into the area of:
Quote:
Source
Nerds are preoccupied with things that don't really matter.


[wink]?

Only kidding

I think comments are good, simply because I don't use them enough myself. Up to now I have been the only developer on most of my (personal) projects, but I'm now in a team (small, but still a team) at work so I really have to "get into" using them.

I'd take the extra pointless comment or two over an uncommented mess any day I think. However if there were no comments of actual value then there is a different storey altogether. In that case one might as well write a small script to strip them and save yourself the extra .00000001% disk space and time taken by reading them.
Extrarius
Extrarius
Quote:
Original post by Oluseyi
[...]Separately, a new routine was added to a source file and it concludes with:
# everything worksreturn 1;
[...]
That comment is only superfluous in a large context consisting of the standards in place and the function's other code. Out of context, it's perfectly valid, because functions can be written with either "error-depth" or "success-depth"[1]. Of course, a simple comment at the top of the function saying it is "error-depth" would help if some such terminology was understood by all involved or explained in a style standard of some kind.

Whether the return value makes the point obvious depends on whether the function's return value indicates there was an error (0 = no error) or there was success (0 = error).


[1] ..., by which I mean one dimension [2] typically maps to the flow of errors during execution and the other typically maps to success. In the example, "error-depth" was the chosen style, and it should be obvious from the code, but it isn't always.

[2] In C++, the two dimensions would be (1, "depth") the number of blocks enclosing a statement and (2, "length" or maybe "breadth" to match) the number of blocks before a statement. They are approximately equal to the indentation and line number for any particular statement.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
pinacolada
pinacolada
Quote:
Original post by Oluseyi

In a third case, I see the following:
# if the volume doesn't exist, failreturn 0 unless -d $vol and -w $vol;



I kinda like having human-friendly comments like that, even when the code is obvious. In that example you gave, it takes me about 1 second to understand the comment, and maybe 3 seconds to understand the code. It's not a big difference for just one line, but when you're scanning through hundreds of lines of someone else's code, it adds up.
T1Oracle
T1Oracle
I often do stuff like "return false; // return failure" and "return true; // return success"

I don't find that excessive, although if exceptions are available in the language I'm using I generally prefer them. With an exception it is obvious that you are reporting an error condition, however with a return statement that isn't true.

I never use comments to explain the meaning of language symbols or syntax. I do use it to describe things that are not evident by the code or to label the stages in a large procedural block of code (fortunately such things are rarely needed). Comments are no alternative to understanding the programming language.
Programming since 1995.
Programmer One
Programmer One
Our company business lawyer had the perfect answer for this:

As the cleanliness and understandability of a company's source code increases, the resell value of the company increases along with it.

From a programmer's point-of-view, useless comments are useless. From the point-of-view of a potential buyer (who may or may not know that much about code), it could mean everything. It makes it look better.
Crypter
Crypter
I useually comment whenever I feel is needed, and no more.
T1Oracle
T1Oracle
I have a tendency to put a one line comment (minimum, but very rarely is it more than 1 line) on every object/variable, typedef, and function that I make. The only exceptions are object constructors and operator overloads. I keep them brief and simple and I write them fast, but it's a natural habit of mines to put a comment for every function, object/variable, and typedef.

I've been doing that since I started with C at 13, it's pretty well ingrained in me.
Programming since 1995.
superpig
superpig
A comment should be present iff reading and understanding the comment will be faster or more valuable than reading and understanding the code.

But how do you know how easily other people will be able to read/understand the code you write? Assuming that they know the basics of the language is doubtless reasonable or else they would probably not be looking at your code in the first place. But is the reader familiar with all the libraries you're using? All the specified behaviours your code represents? All the history behind the code?

I want to pick on something you said in particular:

Quote:
You changed something between two revisions? Write a commit log.


That works really well until I look at that code a few weeks later and there is absolutely no indication in the code of any kind of past history. Sure, I could check the commit logs for every single file I work with and learn all the history, but that's totally contrary to why these histories are recorded in the first place. In a codebase of thousands of files it's also a serious productivity hit.

You want me to stop duplicating information between tools? Show me integration. Show me an add-in for Visual Studio that keeps in my mind at all times all relevant information for what I'm currently doing - commit histories, bug reports, specification references, etc.

Want a more concrete goal? Show me an add-in that detects when code I am writing is similar to code that was previously removed, pointing me at the commit logs for the removal so I can see why it wasn't made this way previously. When I'm editing a block of code, have it find all previous commits that were made that affected that block of code, and list all the bugs associated with them so I can quickly see which nuances of the code are there to fix which bugs.

A lack of integration between all these "good tools" is what leads to excessive commenting - consider it data denormalization. Put enterprise search in the development pool - make it automatic, context-sensitive, and intelligent - and superfluous comments will disappear.
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
T1Oracle
T1Oracle
^ Like that idea. If such a tool existed I would use Ctrl+C a whole lot less often (on comments, not code).
Programming since 1995.
MaulingMonkey
MaulingMonkey
Quote:
Original post by T1Oracle
I often do stuff like "return false; // return failure" and "return true; // return success"


I find that excessive. I'd use:
return false; // (failure)return true ; // (success)


Of course, as soon as you pass boolean success/fail, named return codes make sense immediately and eliminate the need for such comments immediately (except to note, for example, the circumstances involved in the failure beyond that already encoded into the return code name)




I heavily prefer to lean towards readable code. I'll say that again: readable code. Comments are not code, and often get in the way of what would otherwise be clearly readable code (by inserting, in effect, random noise, often of no value) --- or worse yet, serve as an excuse to write unreadable code.

I try to refactor my comments to code where possible.
Luctus
Luctus
Quote:
Original post by Oluseyi
Worse, comments have to be maintained - and ditto for perldoc/doxygen.
..snip..
Write real documentation - concise, to the point, detailing overall behavior.

You mean to say that maintaining two separate documents, the source itself and the documentation of the source, is easier than generating/embedding the documentation from/in the source?

Personally, I've made a habit of inserting a TODO note in comments whenever they are made obsolete and I don't have the time or motivation to update them properly, which ensures I catch them when I later grep the files prior to checking them in.

As for commenting habits, I suppose it's as opinionated as where to put opening and closing braces. I am of the opinion however that comments should be inserted where they would make it quicker to read and understand the code. Sprinkling obvious comments every other line works contrary to that.
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams
Sandman
Sandman
'Dumb' comments like those mentioned in the OP occasionally crop up in my code as a result of filling out a stub with comments describing what is expected to happen in the function.

This occurs for two reasons:
1. The function is part of an interface which needs to be defined for compilation but is not a high priority. However to make it easier on myself when I do get back to it I'll comment it with a TODO and sometimes a quick analysis of an expected implementation.
2. The function involves a bit of thinking, and so in order to get my head around how to implement it I jot a few ideas down in comment form before settling on a solution and coding it up.

In either case, unless/until the comments end up being inaccurate, I probably won't go back through and delete them.
eedok
eedok
Quote:
Original post by T1Oracle
I often do stuff like "return false; // return failure" and "return true; //

what ever happened to:
const boolean success = true;
const boolean failure = false;

Warning: the above may not be a real language but it should put the point across.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.