有時(shí)我們會(huì)希望在表達(dá)式中使用表達(dá)式(比如條件嵌套),這時(shí)應(yīng)注意else表達(dá)式,它的位置很容易出錯(cuò)!如例:
代碼: |
if (expr1) { statement1; statement2; } else if (expr2) statement3; else if (expr3) { statement4; statement5; } else { statement6; statement7; } |
注意大括號(hào)位置!
Loops
while-loop語法如下:
代碼: |
while (expr) { statement1; statement2; } |
for-loop語法如下:
代碼: |
for (expr1; expr2; expr3){ statement1; statement2; } |
注意大括號(hào)位置!僅一條聲明時(shí)大括號(hào)省略:
代碼: |
while (expr) statement; for (expr1; expr2; expr3) statement; |
例如,我們寫一個(gè)procedure寫出1到10這十個(gè)數(shù)字:
代碼: |
for (i = 1; i <= 10; i++) System.out.println(i); |
try-catch語法形如:
代碼: |
try { statements; } catch (ExceptionClass e) { statements; } |
如果try-catch語句后跟隨finally子句則形如:
代碼: |
try { statements; } catch (ExceptionClass e) { statements; } finally { statements; } |
新行
每一行最好只闡述一件事情。比如,一行包含一個(gè)聲明、一個(gè)條件語句、一個(gè)循環(huán)等。不論多小,最好不要一行辦兩件事及以上。例如不要把一個(gè)if表達(dá)式或循環(huán)語句的主體放置在同一行,這樣的表達(dá)式斷行的易讀性會(huì)更高。通常,互相協(xié)作的代碼應(yīng)放在一起,為保證代碼美觀可讀,我們應(yīng)將代碼的不同代碼段放置在不同的段落。不過要牢記斷行不要太過分!比如:
代碼: |
public int factorial(int n) { int result = 1; for(int i = 1; i <= n; i++) result*=i; return result; } |
給自己的代碼加入注釋
注釋就是類的描繪、方法存在的原因、它完成了什么以及它對(duì)它其中(變量)的作用域。假定閱讀你代碼的人已經(jīng)知道這是什么語言,所以不需要注釋語句功能,盡量使用簡短而有描述力的注釋。
Java有兩種類型的注釋:
//This is a comment that continues until the end of the line.
/* This is a comment. It goes on and on and on and on and on and on and on
and on and on and on and on and on and on and on and on and on and on and
on and on and on and on and on and on and on and on and ends like this: */
/**
* This is a JavaDoc comment. More about JavaDoc in the next section.
*/
如果在注釋中加入注釋則會(huì)出錯(cuò):
/* You are not allowed to do anything like this /* because the compiler will
complain, if you are lucky */ DON'T DO THIS! And don't write comments in
upper case either... */
注釋應(yīng)放在它要解釋內(nèi)容上下,這樣會(huì)讓代碼更易于理解。
不要注釋一些語言的語句功能:
i++; // Add 1 to i
更不要讓自己的代碼處于這種狀態(tài):
for(int i = 1; i <= n; i++)
/* don't place comments where
they don't belong */
result*=i;
較短的注釋既可被放在被注釋代碼上下,而長注釋則習(xí)慣性的放在代碼之上:
/* Comments can be placed before the
block that is to be commented */
for(int i = 1; i <= n; i++)
result*=i;
或者:
for(int i = 1; i <= n; i++){
result*=i; // short comments can be placed like this
tmp++; // if necessary, they continue here
}
不要寫沒用的注釋:
i++; // change this later
Excuse me,這句肯定是胡扯!
不要寫自己都看不懂的注釋:
i++; // BMW
BMW? 如果你能連續(xù)十天記住這是什么意思的話,那么你的記憶真是不錯(cuò)了。所以不要寫沒人能看懂的注釋,ok?
最后重申一下:寫簡短而富于描述性的注釋,把它們放在該放的地方,而不要考驗(yàn)?zāi)阕约旱挠洃浟Γ?
JavaDoc - 文檔工具
JavaDoc不僅是另一種給代碼加注釋的仿佛咱,更是一個(gè)文檔工具。類、方法和一些重要地方需要用JavaDoc來注釋。這并不是說你可以放棄常規(guī)的注釋,這兩者在代碼中應(yīng)該是相輔相成、互相彌補(bǔ)的關(guān)系。
類被注釋如:
/**
* Car represents cars ... A description of the class
* should be place here. Note that the description begins
* on the second line and that there is a space between
* the asterix and the text. Next we will add some fields
* indicating who the authors of the class are and
* other useful information. Notice the newline!
*
* @author Jerry Meng
* @version %I%, %G%
*/
public class Car {
注意JavaDoc結(jié)束和類開始間無空行。
方法被注釋如:
/**
* A description of what the method does...
*
* @param n a description of the parameter
* @return a description of the return value
*/
public int factorial(int n) {
某些不是全部,被JavaDoc注釋區(qū)域如:
/**
* Short description of the variable (one line)
*/
type variable;