飛翔的起點

          從這里出發

          導航

          <2009年1月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          統計

          常用鏈接

          留言簿(5)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          漂亮代碼七法

          寫出漂亮代碼的七種方法

          首先我想說明我本文闡述的是純粹從美學的角度來寫出代碼,而非技術、邏輯等。以下為寫出漂亮代碼的七種方法:



          1, 盡快結束 if語句



          例如下面這個JavaScript語句,看起來就很恐怖:



          Java代碼 復制代碼
          1. 1 function findShape(flags, point, attribute, list) {   
          2.   
          3. 2    if(!findShapePoints(flags, point, attribute)) {   
          4.   
          5. 3        if(!doFindShapePoints(flags, point, attribute)) {   
          6.   
          7. 4            if(!findInShape(flags, point, attribute)) {   
          8.   
          9. 5                if(!findFromGuide(flags,point) {   
          10.   
          11. 6                    if(list.count() > 0 && flags == 1) {   
          12.   
          13. 7                          doSomething();   
          14.   
          15. 8                    }   
          16.   
          17. 9                }   
          18.   
          19. 10            }   
          20.   
          21. 11       }   
          22.   
          23. 12    }      
          24.   
          25. 13  }  


          但如果這么寫就好看得多:
          Java代碼 復制代碼
          1. 1 function findShape(flags, point, attribute, list) {   
          2.   
          3. 2    if(findShapePoints(flags, point, attribute)) {   
          4.   
          5. 3        return;   
          6.   
          7. 4    }   
          8.   
          9. 5    
          10.   
          11. 6    if(doFindShapePoints(flags, point, attribute)) {   
          12.   
          13. 7        return;   
          14.   
          15. 8    }   
          16.   
          17. 9    
          18.   
          19. 10    if(findInShape(flags, point, attribute)) {    
          20.   
          21. 11        return;   
          22.   
          23. 12    }   
          24.   
          25. 13    
          26.   
          27. 14    if(findFromGuide(flags,point) {   
          28.   
          29. 15        return;   
          30.   
          31. 16    }   
          32.   
          33. 17    
          34.   
          35. 18    if (!(list.count() > 0 && flags == 1)) {   
          36.   
          37. 19        return;   
          38.   
          39. 20    }   
          40.   
          41. 21    
          42.   
          43. 22    doSomething();   
          44.   
          45. 23    
          46.   
          47. 24 }  



          你可能會很不喜歡第二種的表述方式,但反映出了迅速返回if值的思想,也可以理解為:避免不必要的else陳述。



          2, 如果只是簡單的布爾運算(邏輯運算),不要使用if語句



          例如:
          Java代碼 復制代碼
          1. 1 function isStringEmpty(str){   
          2.   
          3. 2    if(str === "") {    
          4.   
          5. 3        return true;   
          6.   
          7. 4    }   
          8.   
          9. 5    else {   
          10.   
          11. 6        return false;   
          12.   
          13. 7    }   
          14.   
          15. 8 }  


          可以寫為:
          Java代碼 復制代碼
          1. 1 function isStringEmpty(str){   
          2.   
          3. 2    return (str === "");   
          4.   
          5. 3 }  

          3, 使用空白,這是免費的

          例如:

          1
          Java代碼 復制代碼
          1.  function getSomeAngle() {   
          2.   
          3. 2    // Some code here then   
          4.   
          5. 3    radAngle1 = Math.atan(slope(center, point1));   
          6.   
          7. 4    radAngle2 = Math.atan(slope(center, point2));   
          8.   
          9. 5    firstAngle = getStartAngle(radAngle1, point1, center);   
          10.   
          11. 6    secondAngle = getStartAngle(radAngle2, point2, center);   
          12.   
          13. 7    radAngle1 = degreesToRadians(firstAngle);   
          14.   
          15. 8    radAngle2 = degreesToRadians(secondAngle);   
          16.   
          17. 9    baseRadius = distance(point, center);   
          18.   
          19. 10    radius = baseRadius + (lines * y);   
          20.   
          21. 11    p1["x"] = roundValue(radius * Math.cos(radAngle1) + center["x"]);   
          22.   
          23. 12    p1["y"] = roundValue(radius * Math.sin(radAngle1) + center["y"]);   
          24.   
          25. 13    pt2["x"] = roundValue(radius * Math.cos(radAngle2) + center["y"]);   
          26.   
          27. 14    pt2["y"] = roundValue(radius * Math.sin(radAngle2) + center["y");   
          28.   
          29. 15    // Now some more code   
          30.   
          31. 16 }  


          很多開發者不愿意使用空白,就好像這要收費一樣。我在此并非刻意地添加空白,粗魯地打斷代碼的連貫性。在實際編寫代碼的過程中,會很容易地發現在什么地方加入空白,這不但美觀而且讓讀者易懂,如下:

          Java代碼 復制代碼
          1. 1 function getSomeAngle() {   
          2.   
          3. 2    // Some code here then   
          4.   
          5. 3    radAngle1 = Math.atan(slope(center, point1));   
          6.   
          7. 4    radAngle2 = Math.atan(slope(center, point2));   
          8.   
          9. 5    
          10.   
          11. 6    firstAngle = getStartAngle(radAngle1, point1, center);   
          12.   
          13. 7    secondAngle = getStartAngle(radAngle2, point2, center);   
          14.   
          15. 8    
          16.   
          17. 9    radAngle1 = degreesToRadians(firstAngle);   
          18.   
          19. 10    radAngle2 = degreesToRadians(secondAngle);   
          20.   
          21. 11    
          22.   
          23. 12    baseRadius = distance(point, center);   
          24.   
          25. 13    radius = baseRadius + (lines * y);   
          26.   
          27. 14    
          28.   
          29. 15    p1["x"] = roundValue(radius * Math.cos(radAngle1) + center["x"]);   
          30.   
          31. 16    p1["y"] = roundValue(radius * Math.sin(radAngle1) + center["y"]);   
          32.   
          33. 17    
          34.   
          35. 18    pt2["x"] = roundValue(radius * Math.cos(radAngle2) + center["y"]);   
          36.   
          37. 19    pt2["y"] = roundValue(radius * Math.sin(radAngle2) + center["y");   
          38.   
          39. 20    // Now some more code   
          40.   
          41. 21 }   
          42.   
          43.   
          44.   
          45. 4, 不要使用無謂的注釋   
          46.   
          47. 無謂的注釋讓人費神,這實在很討厭。不要標出很明顯的注釋。在以下的例子中,每個人都知道代碼表達的是“students id”,因而沒必要標出。   
          48.   
          49. 1 function existsStudent(id, list) {   
          50.   
          51. 2    for(i = 0; i < list.length; i++) {   
          52.   
          53. 3       student = list[i];   
          54.   
          55. 4    
          56.   
          57. 5       // Get the student's id   
          58.   
          59. 6       thisId = student.getId();   
          60.   
          61. 7    
          62.   
          63. 8       if(thisId === id) {   
          64.   
          65. 9           return true;   
          66.   
          67. 10       }   
          68.   
          69. 11    }   
          70.   
          71. 12    return false;      
          72.   
          73. 13 }  



          5, 不要在源文件中留下已經刪除的代碼,哪怕你標注了

          如果你使用了版本控制,那么你就可以輕松地找回前一個版本的代碼。如果別人大費周折地讀了你的代碼,卻發現是要刪除的代碼,這實在太恨人了。


          Java代碼 復制代碼
          1. //function thisReallyHandyFunction() {   
          2.   
          3. //      someMagic();   
          4.   
          5. //      someMoreMagic();   
          6.   
          7. //      magicNumber = evenMoreMagic();   
          8.   
          9. //      return magicNumber;   
          10.   
          11. //}  


          6,不要有太長的代碼



          看太長的代碼實在太費勁,尤其是代碼本身的功能又很小。如下:


          Java代碼 復制代碼
          1. 1 public static EnumMap<Category, IntPair> getGroupCategoryDistribution(EnumMap<Category, Integer> sizes, int groups) {   
          2.   
          3. 2        EnumMap<Category, IntPair> categoryGroupCounts = new EnumMap<Category,IntPair>(Category.class);   
          4.   
          5. 3    
          6.   
          7. 4        for(Category cat : Category.values()) {   
          8.   
          9. 5            categoryGroupCounts.put(cat, getCategoryDistribution(sizes.get(cat), groups));   
          10.   
          11. 6        }  


          #



          我并不是說非要堅持70個字符以內,但是一個比較理想的長度是控制在120個字符內。如果你把代碼發布在互聯網上,用戶讀起來就很困難。

          7,不要在一個功能(或者函數內)有太多代碼行

          我的一個老同事曾經說Visual C++很臭,因為它不允許你在一個函數內擁有超過10,000行代碼。我記不清代碼行數的上限,不知道他說的是否正確,但我很不贊成他的觀點。如果一個函數超過了50行,看起來有多費勁你知道么,還有沒完沒了的if循環,而且你還的滾動鼠標前后對照這段代碼。對我而言,超過35行的代碼理解起來就很困難了。我的建議是超過這個數字就把一個函數代碼分割成兩個。

          posted on 2009-01-18 14:33 forgood 閱讀(223) 評論(0)  編輯  收藏 所屬分類: java

          主站蜘蛛池模板: 江城| 镇赉县| 新泰市| 科技| 綦江县| 玉田县| 鄂伦春自治旗| 阿拉尔市| 兰坪| 乐都县| 泰宁县| 上杭县| 金寨县| 东光县| 腾冲县| 南靖县| 达尔| 枣阳市| 邯郸县| 铜梁县| 额尔古纳市| 城步| 吉安市| 千阳县| 黄浦区| 华坪县| 台南市| 菏泽市| 忻城县| 三都| 阳城县| 盐城市| 彭山县| 县级市| 宁陕县| 万山特区| 泸定县| 纳雍县| 西峡县| 辽宁省| 呼和浩特市|