Nothing can be quite so helpful as a well-placed comment.Nothing can clutter up a module more than frivolous dogmatic comments.Nothing can be quite so damaging as an old crufry comment that propages lies and misinformation.

The proper use of comments is to compensate for our failure to express ourself in code.Comments are always failures.We must have them because we cannot always figure out how to express ourselves without them,but their ues is not a cause for celebration.

Comments do not make up for bad code

  One of the more common motivations for writing comments is bad code.Clear and expressive code with few comments is far superior to cluttered and complex code with lots of comments.Rather than spend your time wrting the comments that explain the mess you've made, spend it cleaning that mess.

Explain yourself in code

  There are certainly times when code makes a poor vehicle for explanation.In many cases it's simply a matter of creating a function that says the same thing as the comment you want to write.

Good comments

  Legal Comments

    Sometimes our corporate coding standards force us to write certain comments for legal reasons.For example, copyright and authorship statements are necessary and reasonable things to put into a comment at the start of each source file.Comments like this should not be contracts or legal tomes.Where possible,refer to a standard license or other external document rather than putting all the terms and conditions into the comment.

  Informative Comments

    It is sometimes useful to provide basic information with a comment.For example, consider this comment that explains the value of an abtract methos:

    // Returns an instance of the Responder being tested

    protected abstract Responder reponderInstance();

    A comment like this sometimes be useful, but it is better to use the name of the function to convey the information where possible.For example, in this case the comment could be made redundant by renaming the function:responderBeingTested.

  Explanation of Intent

    Sometimes a comment goes beyond just useful information about the implementation and provides the intent behind a decision.

  Clarification

    Sometimes it is just helpful to translate the meaning of some obscure argument or return value into something that's readable.In general it is better to find a way to make that argument or return value clear in its own right;but when its part of the standard library, or in code that you cannot alter, then a helpful clarifying comment can be useful.

  Warning of Consequences

    Sometimes it is useful to warn other programmers about certain consequences.Nowadays, of course, we'd turn off the best cast by using the @Ignore attribute with an appropriate explanatory string.But back in the days before JUnit 4, putting an underscore in front of the method name was a common convention.

  TODO Comments

    It is sometimes reasonable to leave "To do" notes in the form of //TODO comments.

    TODO are jobs that the programmer thinks should be done, but for some reason can't do at the moment.It might be a reminder to delete a deprecated feature or a plea for someone else to look at a problem.It might be a request for someone else to think of a better name or a reminder to make a change that is dependent on a planned event.Whatever else a TODO  might be, it is not an excuse to leave bad code in the system.

  Amplification

    A comment may be used to amplify the importance of something that may otherwise seem inconsequential.

  Javadocs in Public APIs

    There is nothing quite so helpful and satisfying as a well-described public API.The javadocs for the standard Java library are a case in point.It woulde be difficult,a t best, to write Java programs without them.

    If your are writing a public API, then you should certainly write good javadocs for it.

Bad Comments

  Mumbling

    Plopping in a comment just because you feel you should or because the process requires it, is a hack.If you decide to write a comment,then spend the time necessary to make sure it is the best commetn you can write.

    Our only recourse is to examine the code in other parts of the system to find out what's going on.Any comment that forces you to look in another module for the meaning of that comment has failed to communicate to you and is not worth the bits it consumes.

  Redundant Comments

    The comment certainly not more informative than the code.It dose not justify the code, or provide intent or rationale.It is not easier to read than the code.Indeed, it is less precise than the code and entices the reader to accept the lack of precision in lieu of true understanding.

  Misleading Comments

    Sometimes, with all the best intentions, a programmer makes a statement in his comments that isn't precise enough to be accurate.

  Mandated Comments

    It is just plain silly to have a rule that every function must hava a javadoc, or every variable must have a comment.Comments like this just clutter up the code,propagate lies and lend to general confusion and disorganization.

  Journal Comments

    Sometimes people add a comment to the start of a module every time they edit it.These comments accumulate as a kind of journal or log,of every change that has ever been made.  

  Noise Comments

  Scary Noise

  Don't use a comment when you can use a function or a variable

    The author of the original code may have written the comment first and then written the code to fulfill the comment.However, the author should then have refactored the code , as I did, so that the comment shoule be removed.

  Position Markers

    Sometimes programmers like to mark a particular position in a source file.There are rare times when it makes sense to gather certain functions together beneath a banner like this.But in general they are clutter that should be eliminated.A banner is startling and obvious if you don't see banners very often.So use them very sparingly, and only when the benefit is significant.

  Closing Brace Comments

    Sometimes programmers will put special comments on closing braces.Although this might make sense for long functions with deeplu nested structures,it serves only to clutter the kind of small and encapsulated functions that we prefer.So if you find yourself wanting to mark your closing braces, try to shorten your functions instead.

  Attributions and bylines

    Source code control systems are very good at remembering who added what,when.There is no need to pollute the code with little by lines.You might think that such comments would be useful in order to help others know who to talk to about the code.But the reality is that they tend to stay around for years and years, getting less and less accurate and relevant.Again,the source code control system is a better place for this kind of information.

  Commented-Out Code

    Few practices are as odious as commenting-out code.Don't do this.Others who see that commented-out codd won't have the courage to delete it.They'll think it is there for a reason and is too important to delete.

  HTML Comments

    HTML in source code comments is an abomination, as you can tell by reading the code below.It makes the comments hard to read in the one place where they should be easy to read - the editor/IDE.

  Nonlocal Information

    If you must write a comment, then make sure it describes the code it appears near.Don't offer systemwide information in the context of a local comment.

  Too Much Information

    Don't put interesting historical discussions or irrelevant descriptions of details into your comments.

  Inobvious Connection

    The connection between a comment and the code it describes should be obvious.If you are going to the trouble to write a comment, then at least you'd like the reader to be able to look at the comment and the code and understand what the comment is talking about.

  Function Headers

    Short functions don't need much description.A well-chosen name for a small function that does one thing is ususally better than a comment header.

  Javadocs in Nonpublic Code

    As useful as javadocs are for public APIs, they are anathema to code that is not intended for public comsumption.

  

Comments的更多相关文章

  1. 代码的坏味道(13)——过多的注释(Comments)

    坏味道--过多的注释(Comments) 特征 注释本身并不是坏事.但是常常有这样的情况:一段代码中出现长长的注释,而它之所以存在,是因为代码很糟糕. 问题原因 注释的作者意识到自己的代码不直观或不明 ...

  2. django 1.8 评论库comments配置问题

    comments库是django框架内置的评论库,可以快速搭建网站需要的评论系统.不过1.8的配置和1.6的出现了一点小小配置,由于刚刚接触,按照网上的文档配置,需要在 settings.py的INS ...

  3. 奇葩问题:This file could not be checked in because the original version of the file on the server was moved or deleted. A new version of this file has been saved to the server, but your check-in comments were not saved

    "This file could not be checked in because the original version of the file on the server was m ...

  4. django 添加comments app

    django 添加comments app 参看 django comments 文档 安装和配置comments 1.安装comments,运行:pip install django-contrib ...

  5. 去掉comments

    三种comments: /* Test program */ int main() { // variable declaration int a, b, c; /* This is a test m ...

  6. The string "--" is not permitted within comments

    ibatis中SAXParseException异常:The string "--" is not permitted within comments 这个异常是说sqlmap里面 ...

  7. Effective Java 44 Write doc comments for all exposed API elements

    Principle You must precede every exported class, interface, constructor, method, and field declarati ...

  8. JavaScript Patterns 2.11 Writing Comments

    Document all functions, their arguments and return values, and also any interesting or unusual algor ...

  9. PHP Deprecated: Comments starting with '#' are deprecated in *.ini 警告解决办法

    新装的ubuntu 10.04系统,使用新立得装的PHP,但是每次我在命令行下执行php脚本时都会出如下的警告信息: PHP Deprecated:  Comments starting with ' ...

  10. How to Write Doc Comments for the Javadoc Tool

    http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html This document describe ...

随机推荐

  1. 网络编程之socket(转)

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价 值,那网络中进程之间如何通信,如我们每天打开浏 ...

  2. springMVC 验证器

    采用Hibernate-validator来进行验证,Hibernate-validator实现了JSR-303验证框架支持注解风格的验证.首先我们要到http://hibernate.org/val ...

  3. BZOJ2494 Triangles and Quadrangle

    一道非常"简单"的计算几何题... 题意:给你两个三角形和一个四边形,问你能否用这两个三角形拼成这个四边形 首先...四边形可能是凹四边形...需要判断一下...这个比较简单直接分 ...

  4. Octopus系列之SQLite3常用命令

    导出脚本F:\B2CShop>sqlite3 B2CDB.db .dump > test.sql 导入脚本F:\B2CShop>sqlite3 B2CDB.db < B2C-S ...

  5. 随机分类器的ROC和Precision-recall曲线

    随机分类器,也就是对于一个分类问题,随机猜测答案.理论上,随机分类器的性能是所有分类器的下界.对随机分类器的理解,可以帮助更好的理解分类器的性能指标.随机分类器的性能也可以作为评价分类器的一个基础.所 ...

  6. PHP+mysql常用类库

    <?php /** * @title: Ekcms mysql类库 * @version: 1.0 * @author: perry <perry@1kyou.com> * @pub ...

  7. 关于Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)的问题

    找不到mysql socket的问题,我最近碰到了好多次重装系统以前,我的mysql,apache,php都是自己编译安装的,当时并没有碰到这些问题,重装系统以后,我的mysql是通过yum安装的,a ...

  8. 尽量少用Include

    当我们使用EF写查询,尤其是关联到多个表的时候,我们一般习惯使用include关联,但是过多地使用include会带来性能问题.作为替代方法,可以使用多个toList来代替. 改造前:

  9. js循环

    $('.xcarcoin_tb').each(function(i){ var aika='爱卡币';                if(data[i]==0){                }e ...

  10. HDU 4906 Our happy ending(2014 Multi-University Training Contest 4)

    题意:构造出n个数 这n个数取值范围0-L,这n个数中存在取一些数之和等于k,则这样称为一种方法.给定n,k,L,求方案数. 思路:装压 每位 第1为表示这种方案能不能构成1(1表示能0表示不能)   ...