[轉載] 寫出編排漂亮程式碼的七種方法
來源:[url=http://news.csdn.net/n/20081217/121810.html]http://news.csdn.net/n/20081217/121810.html[/url]首先我想說明我本文闡述的是純粹從美學的角度來寫出程式碼,而非技術、邏輯等。以下為寫出漂亮程式碼的七種方法:
[b]1、儘快結束 if 語句[/b]
例如下面這個JavaScript語句,看起來就很恐怖:
[code]
function findShape(flags, point, attribute, list) {
if(!findShapePoints(flags, point, attribute)) {
if(!doFindShapePoints(flags, point, attribute)) {
if(!findInShape(flags, point, attribute)) {
if(!findFromGuide(flags,point) {
if(list.count() > 0 && flags == 1) {
doSomething();
}
}
}
}
}
}[/code]
但如果這麼寫就好看得多:
[code]
function findShape(flags, point, attribute, list) {
if(findShapePoints(flags, point, attribute)) {
return;
}
if(doFindShapePoints(flags, point, attribute)) {
return;
}
if(findInShape(flags, point, attribute)) {
return;
}
if(findFromGuide(flags,point) {
return;
}
if (!(list.count() > 0 && flags == 1)) {
return;
}
doSomething();
}[/code]
你可能會很不喜歡第二種的表述方式,但反映出了迅速返回 if 值的思想,也可以理解為:避免不必要的 else 陳述。
[b]2、如果只是簡單的布林運算(邏輯運算),不要使用 if 語句[/b]
例如:
[code]
function isStringEmpty(str){
if(str === "") {
return true;
}
else {
return false;
}
}[/code]可以寫為:[code]function isStringEmpty(str){
return (str === "");
}[/code]
[b]3、使用空白行 (這是免費的)[/b]
例如:
[code]function getSomeAngle() {
// Some code here then
radAngle1 = Math.atan(slope(center, point1));
radAngle2 = Math.atan(slope(center, point2));
firstAngle = getStartAngle(radAngle1, point1, center);
secondAngle = getStartAngle(radAngle2, point2, center);
radAngle1 = degreesToRadians(firstAngle);
radAngle2 = degreesToRadians(secondAngle);
baseRadius = distance(point, center);
radius = baseRadius + (lines * y);
p1["x"] = roundValue(radius * Math.cos(radAngle1) + center["x"]);
p1["y"] = roundValue(radius * Math.sin(radAngle1) + center["y"]);
pt2["x"] = roundValue(radius * Math.cos(radAngle2) + center["y"]);
pt2["y"] = roundValue(radius * Math.sin(radAngle2) + center["y");
// Now some more code
}[/code]
很多開發者不願意使用空白行,就好像這要收費一樣。我在此並非刻意地添加空白行,粗魯地打斷程式碼的連貫性。在實際編寫程式碼的過程中,會很容易地發現在什麼地方加入空白行,這不但美觀而且讓讀者易懂,如下:
[code]function getSomeAngle() {
// Some code here then
radAngle1 = Math.atan(slope(center, point1));
radAngle2 = Math.atan(slope(center, point2));
firstAngle = getStartAngle(radAngle1, point1, center);
secondAngle = getStartAngle(radAngle2, point2, center);
radAngle1 = degreesToRadians(firstAngle);
radAngle2 = degreesToRadians(secondAngle);
baseRadius = distance(point, center);
radius = baseRadius + (lines * y);
p1["x"] = roundValue(radius * Math.cos(radAngle1) + center["x"]);
p1["y"] = roundValue(radius * Math.sin(radAngle1) + center["y"]);
pt2["x"] = roundValue(radius * Math.cos(radAngle2) + center["y"]);
pt2["y"] = roundValue(radius * Math.sin(radAngle2) + center["y");
// Now some more code
}[/code]
[b]4、不要使用無謂的注釋[/b]
無謂的注釋讓人費神,這實在很討厭。不要標出很明顯的注釋。在以下的例子中,每個人都知道程式碼表達的是“students id”,因而沒必要標出。
[code]function existsStudent(id, list) {
for(i = 0; i < list.length; i++) {
student = list;
// Get the student's id
thisId = student.getId();
if(thisId === id) {
return true;
}
}
return false;
}[/code]
[b]5、不要在原始碼留下已經刪除的程式,包含那些註解起來的[/b]
如果你使用了版本控制,那麼你就可以輕鬆地找回前一個版本的程式碼。如果別人大費周折地讀了你的程式碼,卻發現是要刪除的程式碼,這實在太恨人了。
[code]//function thisReallyHandyFunction() {
// someMagic();
// someMoreMagic();
// magicNumber = evenMoreMagic();
// return magicNumber;
//}[/code]
[b]6、不要有太長的程式碼[/b]
看太長的程式碼實在太費勁,尤其是程式碼本身的功能又很小。如下:[code]public static EnumMap<Category, IntPair> getGroupCategoryDistribution(EnumMap<Category, Integer> sizes, int groups) {
EnumMap<Category, IntPair> categoryGroupCounts = new EnumMap<Category,IntPair>(Category.class);
for(Category cat : Category.values()) {
categoryGroupCounts.put(cat, getCategoryDistribution(sizes.get(cat), groups));
}
}[/code]
我並不是說非要堅持 70 個字元以內,但是一個比較理想的長度是控制在 120 個字元內。如果你把程式碼發佈在網路上,用戶讀起來就很困難。
[b]7、不要在一個功能(或者函數內)有太多行程式碼[/b]
我的一個老同事曾經說 Visual C++ 很臭,因為它不允許你在一個函數內擁有超過 10000 行程式碼。我記不清程式碼行數的上限,不知道他說的是否正確,但我很不贊成他的觀點。如果一個函數超過了 50 行,看起來有多費勁你知道麼,還有沒完沒了的 if 迴圈,而且你還的滾動滑鼠前後對照這段程式碼。對我而言,超過 35 行的程式碼理解起來就很困難了。我的建議是超過這個數位就把一個函數程式碼分割成兩個。 謝謝分享~
很正確且受用的文章 :)
頁:
[1]