More Comments on Code Comments

In my last blog post, Comments on Comments, I was perhaps a bit harsh on the practice of comments in code. What I meant was that excessive what comments, which are comments that explain what the code is doing, are a possible indicator that the developer who wrote them was uncertain that the code could speak for itself.

There are other reasons to write comments in code. Sometimes it’s clear what we’re doing but not always clear why we’re doing it. Why comments, as I call them, say why something is being done in code. This could be because of a federal regulation or it could be expressing how a design comes together at a higher level. These kinds of comments can be breadcrumbs to the people maintaining the code so that they can get a sense of what is actually happening and maybe call out some design features that aren’t entirely obvious. But don’t resort to teaching your reader things they should already know.

Well-encapsulated code is often presented outside of the context in which it lives. This can make it difficult to understand, so visualization tools can be very helpful.

The domain model should be represented in the objects’ relationships to each other in the system. This makes the system understandable to other people. But in a lot of systems I’ve seen there were huge swaths of missing objects in their domain model. Sure they call out the important ones, the ones that are most visible, but many of them are actually made up of many smaller objects that they haven’t called out. Nor have they called out the main collaborator objects that their main objects interact with. This creates a shallow domain model and it forces developers to read every line of code in order to understand a system.

I don’t believe developers should have to read all of my code in order to understand what it does. I believe developers should have to read my method signature and understand what my method does. I don’t like surprises so I keep my APIs clear and to the point. If I’m developing a library or a framework, I will spend extra attention on writing good developer documentation and probably provide a range of good examples through unit tests. Developers can get a good sense of how to use my APIs but I’ll never bore them with lots of superfluous text.

Just to go back to a previous quote by the author: "There is an art and a science to this but unfortunately I don’t often see it taught in school". When it comes to inserting comments into code, there's always a *balance* between revealing the sublime and stating the obvious. I, for one, have never minded the "increment by one" comments (probably because I've seen and coded enough assembly language to be immune to it's deleterious effect in higher-order languages). Also, I would take exception with the sentiment that comments bring little benefit for their authors. Yes, they have to be maintained and updated, but only if they're wrong or misleading to begin with. Comments act as a "double-check" (If the code *doesn't* match the comments then you better be mighty wary that the original coder might have been confused about something). Finally, with tools like "Doxygen", developers have found a way to make code comments useful *beyond* mere semantic regurgitation. Doxygen takes those comments and produces documentation for non-coders to read (thereby solving the problem of coders not wanting to produce such documentation in the first place).

回复
Rémy Fannader

Author of 'Enterprise Architecture Fundamentals', Founder & Owner of Caminao

8 年

The main drawbacks of comments is that they bring little benefit for their authors yet have to be continuously maintained and updated. One remedy is to insert comments as write instructions in try/catch exceptions as well as in regular code.

Michael Wolf

Uncovering better ways ... by doing it and helping others do it.

8 年

blog_count++; # increment blog count by 1

要查看或添加评论,请登录

David Scott Bernstein的更多文章

  • Green Tests and Red Tests

    Green Tests and Red Tests

    In practice, I found that there are times that I want a bit more test coverage than I get from just doing test-first…

  • Radiators and Silos

    Radiators and Silos

    In the old days of corporate America, the way you got ahead was through hard work and perseverance. You strove to…

    1 条评论
  • Makers and Menders

    Makers and Menders

    I’ve been getting back into some research interests of mine that require data acquisition from a variety of sensors so…

  • Core Developer Practices

    Core Developer Practices

    Every field of engineering has a core set of practices that they follow and software engineering is no different. But…

    1 条评论
  • Still XP After All These Years

    Still XP After All These Years

    Are you humming in your head Paul Simon’s “Still Crazy After All These Years”? I am. And it does seem crazy.

  • The Importance of Continuous Integration

    The Importance of Continuous Integration

    Perhaps the most important yet easiest to implement of all the software development practices in Agile is continuous…

  • The Importance of Technical Practices (Again)

    The Importance of Technical Practices (Again)

    Software development has undergone many revolutions over the last few decades. The way we build software today is…

  • Summary of Seven Strategies Series

    Summary of Seven Strategies Series

    I finished my “Seven Strategies” series of 72 blog posts with seven strategies for implementing each of the nine…

    1 条评论
  • Refactor to Learn What Not to Do

    Refactor to Learn What Not to Do

    One of the things that I was not expecting when I started refactoring other people’s code was that I started to see…

    4 条评论
  • Refactor to Clean Up Before Moving On

    Refactor to Clean Up Before Moving On

    Of course, the best time to refactor code is while it’s fresh in your mind, right after having worked with it. Once I…

社区洞察

其他会员也浏览了