分享
 
 
 

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

王朝delphi·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有