DEV Community

Discussion on: Removing Comments

Collapse
 
codemouse92 profile image
Jason C. McDonald

I also agree with commenting intent, but maybe not with some of the "removal" suggestions herein.

My company has actually seen significant gains in coding speed, acclimation time, and code quality after adopting the Commenting Showing Intent standard, wherein every logical block/statement has an intent comment, mandatory. If someone went through and "boyscouted" our code, it would do serious damage to our code base, and probably get them banned. ;)

Take, for example, that bug you mentioned finding because of the comment. Yes, you might have found that on your own, but keep in mind the sheer MASS of code you're looking at. Usually, by time one encounters that, their eyes have already glazed over. What stops them? The discrepancy between the comment and the code. It encourages further investigation, and has led to a LOT of bugs being caught and fixed, even by complete newbies to the language/code.

Thus...while one should definitely prune bad comments, I'd say a better thing to do would be to tidy the comments up.

  • Make sure they're commenting intent, not "what" or "how" (unless you're explaining an especially bizarre algorithmic choice or whatnot).

  • Check for discrepancies, and after resolving them, ensure the comments are accurate.

  • Remove cruft.

  • Fix grammar (it's more important than it sounds).

In other words, just make it more readable. But PLEASE don't go "boyscouting" in CSI-standardized code!

Collapse
 
gonedark profile image
Jason McCreary • Edited

Agreed, don't go removing comments, or any other boyscouting practice, from any standardized codebase.

With that said, CSI seems to say the same thing I do:

CSI comments state WHY

I have no problem with comments relaying why where the code can not.

Collapse
 
codemouse92 profile image
Jason C. McDonald

Except, in my experience, code rarely ever states WHY. It states WHAT. You almost always need intent-comments, because...

$contacts = $unsubscribedFilter->filter($contacts);
$contacts = $duplicateFilter->filter($contacts);

...does NOT tell me why you're doing that, only what you're doing. Therein lie a host of bugs and logic errors.

Thread Thread
 
gonedark profile image
Jason McCreary • Edited

Code is not a "cooking show" where you are guided line by line, step by step through commentary. There is a basic level of trust and skill required to be a programmer. If you can't glean enough about the code through reading it, then it should be refactored - not commented. There is a limit to this. So at some point, it's the programmers responsibility to understand. Let's not lower the bar with comments everywhere.

Thread Thread
 
codemouse92 profile image
Jason C. McDonald • Edited

No, that's not the point at all. The reason for CSI, for intent-commenting in general, is that intent cannot be reconstructed easily. You can figure out WHAT with any degree of conmpentency, but NOT why. This is a major reason we have so much bad code; we spend so much of our time trying to guess what we and other devs were intending, we waste hours trying to grok what a few comments could have helped us grok in minutes.

See, mind-reading is not part of the programmer's skill set. You cannot even necessarily remember your own intent six months after you wrote the code; if anything, you are intent-commenting for your own good.

Now, to clarify...that code you posted almost certainly doesn't appear alone in the code. You aren't facing a two-line block, you're facing a wall of code. What's the intent in context? Perhaps this is in a function that loads your contact list...

// We only want to see contacts we're subscribed to.
$contacts = $unsubscribedFilter->filter($contacts);
$contacts = $duplicateFilter->filter($contacts);

See? Now it's clear why the code is there. We can tell we're filtering out unsubscribed and duplicate contacts, but we now know why we're doing it - to filter the displayed results. Now if you are finding that you're seeing duplicates in the results, and go through, you might realize "oops, I'm storing the results in $contacts, but I should be storing them in $display! That's only easily grokked because of the intent content; ordinarily, you'd probably have looked right past it (code blindness is nothing to mess around with!)

And, for the record, I have encountered those sorts of situations in my own code for years.

Thread Thread
 
jasonlotito profile image
Jason Lotito

Why do we only want to see contacts we're subscribed to? That comment just says what is being done, and I can't even be sure that's what the code is doing.

Well, you should refactor that, because as of right now, you have code that does something very specific (return a list of contacts you are subscribed to, not display that as the comment implies, btw). A better refactoring of that would be to do something like this:

function getContactsSubscribedTo($contacts) {
    $contacts = $unsubscribedFilter->filter($contacts);
    $contacts = $duplicateFilter->filter($contacts);
}

There. We've removed the unnecessary, confusing, "what's it doing" comment with a clearly defined and testable function.

Thread Thread
 
codemouse92 profile image
Jason C. McDonald

...I can't even be sure that's what the code is doing.

Ergo, the intent comment. What is my intention? Why am I writing that code there? If you can't be sure that's correct, then perhaps something is wrong.

I know we can parse semantics here, but I'm not talking theory. The ROI on the commenting standard I describe is actually observable within my own company.

So, yes, you might be describing what you intend for it to be doing. And, yes, there are rare cases where a comment is pointless. But the problem with examples like the above is that they're oversimplified. In the real world, those two little lines of code appear within the context of hundreds or thousands of lines of code, and hundreds of functions with potentially similar names. In practice, the benefit of those intent comments becomes apparent when you begin using it in a real-world context.

Thread Thread
 
jasonlotito profile image
Jason Lotito

So how do I verify your intent without you and automatically?

Thread Thread
 
codemouse92 profile image
Jason C. McDonald • Edited

...how do I verify your intent with you...

The same as how you verify my code without me. Read it.

...and automatically...

The same as how you verify my code automatically. On a large scope, either that's what happens in the tests, or it isn't. On a smaller scope, you don't.


Beyond this, I really doubt this conversation is going to be productive. I know intent-comments work because I actually use them in production. But, I have learned that there are camps who believe that comments are a waste of time, and who will continue to believe they're a waste of time regardless of what I have to say about it. ;) So, I think we'd better agree-to-disagree at this point.