转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
// I have a comment ----Delphi 研发人员谈注释         ★★★★

// I have a comment ----Delphi 研发人员谈注释

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1418 更新时间:2009/4/23 18:42:52
Abstract:Robert Kozak, from Delphi R&D, talks about proper coding techniques.

// I have a comment…

by Robert Kozak (Delphi R&D)

A couple of months ago I had the opportunity to review some code that I wrote when I first started working with Delphi. I hate to admit it but it was pretty bad. A very humbling experience for sure. My skills are vastly improved since that time. In fact, I''''ve notice changes in just a few months. Have you ever gone back and looked at the code that you wrote six months ago? I bet that the thought going through your head was "What was I thinking? There is no way I wrote that."

If you ever had to debug your own code from a previous project I bet it was painful. It is even worse if you have to debug someone else''''s code. The only thing that can help the pain is good comments. Notice I said "good" comments. Bad comments are worse than no comments at all. For example look at the code snippit below. What does it do?

The comments in the above snippit are utterly worthless. It doesn''''t help you tell what the code does. Is this better?

The secret to writing a good comment is to ask yourself why rather than how. In your comments don''''t try to write how the code works (that code itself is the how) but explain why you wrote it. Typically, when commenting is done right there is less of it.

 

(* Categories of comments *)

There are basically 5 categories of kinds of comments. I''''ll explain them from the worse to the best:

Repeat the code

This type of comment restates the code in different words without adding any additional information. This type of comment is to avoided like the plague.

Explanation of the code

This type of comment is used to explain complicated or tricky code. Although these types of comments can help it much better to rewrite the code so that it is easier to understand.

Markers

Markers are used as a temporary note to yourself or other programmers on your team. With Delphi 5 you now have //TODO comments recognized by the IDE. A friend of mine uses //\\ to mark code he wants to come back to later while I tend to use my initials //rk: When the project is completed these markers should be removed.

Summary of the code

This type of comment summarizes the code in a few sentences. This is better than just repeating the code because it allows someone not familiar with your code to scan through quickly.

Description of the code''''s intent

This explains the purpose of the code, or in other words, the why. Comments of this type are easy to read and give you the information required to understand the code it refers to.

 

{Commenting Techniques}

Okay, now that we know we want to use summary and intent commenting types lets take a look at some techniques to use them properly. There are basically 5 areas you want to use comments: individual lines, code paragraphs, data structures, routines, units and projects.

Individual lines

If you are writing good code (that is the point, right?) then you will only comment individual lines in two cases. In the first case, the individual line is complicated enough that it needs an explanation. Although, it is easy to rewrite a complicated routine to clarify its intent it is hard to rewrite an individual line. In the second case, the individual line had an error and you want to record it.

I used to use endline comments a lot. These are comments that are added to the end of the line, as in:

The endline comment usually doesn''''t have enough room to clarify the intent and so degenerates into restating the how. Endline comments also are hard to keep aligned and tend to use abbreviations. The only time that endline comments would be appropriate would be to annotate data declarations, maintenance notes, or to mark the end of a block (//end if, //end while, etc. ). Data declarations tend to be short so there is more room for an endline comment. If you did place the comment above each data declaration it would be harder to follow the data structure itself. Maintenance notes tend to be short and feature initials, date and bug numbers and since they don''''t actually comment the intent of the code an endline comment is appropriate.

Code paragraphs

This is the most common way (and best) to comment your code. Commenting this way should describe the intent of the code following the comment. Don''''t forget, that you should concentrate of the why and not the how. These types of comments should prepare the reader for what is to follow. They should be able to scan the comments and get a good idea what the code does.

Data declarations

Comments that describe variable declarations describe aspects of the variable that the name itself can not, such as its units (as kilometres, feet, pounds, dollars etc.). Variables should be commented where they are declared and if you have any flag variables where each bit has its own meaning then each of these should be commented.

Routines

Some people while tell you that each routine needs this huge monolithic comment header. This is ridiculous. For one, this makes for comments that a laborious to read and far away from the code it describes. Good comments should be at close proximity to the code it describes, usually a few lines of comments are sufficient. There are times though a particular routine does need a little more info. Take for example the following snippit:

I always had trouble with this because I kept forgetting which was the Sender and which was the Target. Even in my own code, for routines with a Source and Target, I kept forgetting which was the incoming and which was the outgoing variable. A comment can go a long way in helping alleviate this confusion. Some other things you may want to put in a routine comment (as needed) are: interface assumptions, change history, routine limitations, global effects, algorithm''''s source, and design patterns used.

Units and projects

Finally we come to units and projects. When commenting units and projects you want to give a short description of the project or unit and describe the purpose of each file. You may also want a change history, description of major objects and design patterns used. You should also include you name and how to reach you either by phone number or email so that anyone maintaining your code can get a hold of you so you can explain the silliness you wrote. And if appropriate a copyright notice.

 

//end file

If you are like me you need to work on your commenting skills. Commenting doesn''''t have to be hard or time consuming. Just remember that your code is the how and your comments are the why. If you keep this in mind I have no doubt that even I could make sense of your code.

{ TODO: For additional reading see Code Complete by Steve McConnell, Microsoft Press }

 

Biography

Robert Kozak is a member of the Kylix R&D team and has been with Borland since the later half of 1999. Robert has been involved as a user of Delphi since the initial beta and since he joined Borland he has been involved in the development of Borland C++ Builder 5 and now Kylix (Top Secret project to bring Delphi and BCB to the Linux platform.) Robert was involved with the start of TaDDA! (Toronto Area Delphi Developers Association) which later merged with TDUG (Toronto Delphi Users Group). Robert continues to stay active in the user community and is active on the Borland newsgroups.


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台