[心得] 註解的方式
其實Java有很多Coding Standard,是很適合拿來給大家參考使用的.
下面是Java所推薦的comment格式:我覺得這樣寫可以讓comment變的比較漂亮:)
也完全可以套用在C/C++裡面
主要是四種用法:
1./* 多行的註解
*
*
*/
2. // 放在一行的後面
3. /* */ 單獨一行
4. // 用來註解掉一塊程式碼
//
//
//
真的爽度會提高喔!!
VAL的大家可以跟我拿全文的電子書
Our Comments About Comments
Earlier we talked about being a team player. The orientation of the exam is
to see if you can create software that is readable, understandable, and
usable by other programmers. Commenting your code correctly is one of the
key ways that you can create developer-friendly software. As you might
expect, the assessors will be looking to see if your code comments are
appropriate, consistent, and in a standard form.
This chapter will focus on implementation comments; Chapter 17 will cover
javadoc comments. There are several standard forms that your implementation
comments can take. Based on the results of extensive research and worldwide
polling we will recommend an approach, which we believe represents the most
common of the standard approaches. If you choose not to use our recommendation,
the most important thing that you can do is to pick a standard approach
and stick with it.
There are several types of comments that commonly occur within source
code listings. We will discuss each of them with our recommendations and other
possible uses.
Block Comments
Use a block comment in your code when you have to describe aspects of your
program that require more than a single line. They can be used most anywhere,
as source file or method headers, within methods, or to describe key
variables.
Typically, they should be preceded by a blank line and they should take the
following form:
/*
* this is a block comment
* it occupies several lines
*/
Single Line Comments
Use a single line comment in the same place you would block comments, but for
shorter descriptions. They should also be preceded by a blank line for
readability, and we recommend the following form:
/* this is a single line comment */
End of Line Comments
When you want to add a comment to the end of a line of code, use the aptly
named end of line comment. If you have several of these comments in a row,
make sure to align them vertically. We recommend the following form:
doRiskyStuff(); // this method might throw a FileNotFoundException
doComplexStuff(); // instantiate the rete network
Masking Comments
Often in the course of developing software, you might want to mask a code
segment from the compiler without removing it from the file. This technique
is useful during development, but be sure to remove any such code segments
from your code before finishing your project. Masking comments should look
like this:
// if (moreRecs == true) {
// ProcessRecord();
// }
// else {
// doFileCleanUp();
// }
General Tips About Comments
It is important to use comments where the code itself may not be clear, and
it is equally important to avoid comments where the code is obvious. The
following is a classic, from the Bad Comments Hall of Fame:
x = 5; // set the variable x equal to 5
Comments should be used to provide summaries of complex code and to reveal
information about the code that would otherwise be difficult to determine.
Avoid comments that will fall out of date, i.e., write your comments as if
they might have to last forever.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.42.215
推
04/19 01:57, , 1F
04/19 01:57, 1F