写出漂亮代码的七种方法
首先我想說明我本文闡述的是純粹從美學的角度來寫出代碼,而非技術、邏輯等。以下為寫出漂亮代碼的七種方法:
????1.盡快結束 if 語句
????例如下面這個JavaScript語句,看起來就很恐怖:
| ?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(); ????????????????????} ????????????????} ????????????} ???????} ????} ??} |
????但如果這么寫就好看得多:
| ?function findShape(flags, point, attribute, list) { ????if(doFindShapePoints(flags, point, attribute)) { ????if(findInShape(flags, point, attribute)) { ????if(findFromGuide(flags,point) { ????if (!(list.count() > 0 && flags == 1)) { ????doSomething(); ?} |
????你可能會很不喜歡第二種的表述方式,但反映出了迅速返回if值的思想,也可以理解為:避免不必要的else陳述。
????2.如果只是簡單的布爾運算(邏輯運算),不要使用if語句
????例如:
| ?function isStringEmpty(str){ ????if(str === "") { ????????return true; ????} ????else { ????????return false; ????} ?} |
????可以寫為:
| ?function isStringEmpty(str){ ????return (str === ""); ?} |
????3.使用空白,這是免費的
????例如:
| ?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 ?} |
????很多開發者不愿意使用空白,就好像這要收費一樣。我在此并非刻意地添加空白,粗魯地打斷代碼的連貫性。在實際編寫代碼的過程中,會很容易地發現在什么地方加入空白,這不但美觀而且讓讀者易懂,如下:
| ?function getSomeAngle() { ????firstAngle = getStartAngle(radAngle1, point1, center); ????radAngle1 = degreesToRadians(firstAngle); ????baseRadius = distance(point, center); ????p1["x"] = roundValue(radius * Math.cos(radAngle1) + center["x"]); ????pt2["x"] = roundValue(radius * Math.cos(radAngle2) + center["y"]); |
????4.不要使用無謂的注釋
????無謂的注釋讓人費神,這實在很討厭。不要標出很明顯的注釋。在以下的例子中,每個人都知道代碼表達的是“students id”,因而沒必要標出。
?function existsStudent(id, list) {
????for(i = 0; i < list.length; i++) {
???????student = list[i];
???????// Get the student's id
???????thisId = student.getId();
???????if(thisId === id) {
???????????return true;
???????}
????}
????return false;
?}
5.不要在源文件中留下已經刪除的代碼,哪怕你標注了
????如果你使用了版本控制,那么你就可以輕松地找回前一個版本的代碼。如果別人大費周折地讀了你的代碼,卻發現是要刪除的代碼,這實在太恨人了。
| ?//function thisReallyHandyFunction() { //??????someMagic(); //??????someMoreMagic(); //??????magicNumber = evenMoreMagic(); //??????return magicNumber; //} |
????6.不要有太長的代碼
????看太長的代碼實在太費勁,尤其是代碼本身的功能又很小。如下:
| ?public static EnumMap getGroupCategoryDistribution(EnumMap sizes, int groups) { ????????for(Category cat : Category.values()) { |
????我并不是說非要堅持70個字符以內,但是一個比較理想的長度是控制在120個字符內。如果你把代碼發布在互聯網上,用戶讀起來就很困難。
????7.不要在一個功能(或者函數內)有太多代碼行
????我的一個老同事曾經說Visual?C++很 臭,因為它不允許你在一個函數內擁有超過10,000行代碼。我記不清代碼行數的上限,不知道他說的是否正確,但我很不贊成他的觀點。如果一個函數超過了 50行,看起來有多費勁你知道么,還有沒完沒了的if循環,而且你還的滾動鼠標前后對照這段代碼。對我而言,超過35行的代碼理解起來就很困難了。我的建 議是超過這個數字就把一個函數代碼分割成兩個。
轉載于:https://www.cnblogs.com/flying-roc/archive/2012/03/21/2410398.html
總結
以上是生活随笔為你收集整理的写出漂亮代码的七种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL Connector/ODBC
- 下一篇: 解决i9001WiFi频繁断线