這幾天在看《重構:改善既有代碼的設計》英文評註版這本書,之前簡單看過這本書,但並非看的特別深刻。通過了一段時間的「磨練」,如今回來從新看這本書,又不少想法。程序員
首先,這本書是一本好書,我感受是軟件工程師必看的一本書,並且若是有大量的編碼經驗看這本書會收穫很大。這本書主要內容是代碼重構。less
在書中第3章中有這樣一段話是用來描述代碼註釋的:函數
A good time to use a comment is when you don't know what to do. In addition to describe what is going on,comments can indicate areas in which you aren't sure. A comments is a good place to say why you did something. 學習
書中的中文評註以下:this
高明的程序員絕對不會將註釋做爲說明代碼的重要手段。註釋就像咖啡,適量飲用能夠提精神,一旦過量,就變得過分亢奮了。編碼
我我的感受上面兩端話言簡意賅的說出了註釋的做用,註釋只是代碼的一個輔助,是爲了咱們更好的理解代碼。其實關於代碼風格和註釋風格,每一個人都有本身的見解。我以爲都沒有錯,只不過每一個人的見解不同。就像我,我我的認爲代碼首先是給程序員看的,其次是給機器看的。由於,發明高級程序語言的目的之一就是爲了讓人們更好的閱讀代碼。變量和方法的命名就能夠說明變量的做用,若是說不清楚,可使用註釋。但若是是簡單易懂的變量,我以爲寫註釋不必。spa
1 string personName; // 人員姓名
就像上面的一行代碼,我認爲不必寫這個註釋,不要由於寫註釋而去寫註釋。此外,在最近閱讀Java源代碼的時候,我發現Java源代碼註釋有不少特色。設計
1 /** 2 * Compares two strings lexicographically. 3 * The comparison is based on the Unicode value of each character in 4 * the strings. The character sequence represented by this 5 * {@code String} object is compared lexicographically to the 6 * character sequence represented by the argument string. The result is 7 * a negative integer if this {@code String} object 8 * lexicographically precedes the argument string. The result is a 9 * positive integer if this {@code String} object lexicographically 10 * follows the argument string. The result is zero if the strings 11 * are equal; {@code compareTo} returns {@code 0} exactly when 12 * the {@link #equals(Object)} method would return {@code true}. 13 * <p> 14 * This is the definition of lexicographic ordering. If two strings are 15 * different, then either they have different characters at some index 16 * that is a valid index for both strings, or their lengths are different, 17 * or both. If they have different characters at one or more index 18 * positions, let <i>k</i> be the smallest such index; then the string 19 * whose character at position <i>k</i> has the smaller value, as 20 * determined by using the < operator, lexicographically precedes the 21 * other string. In this case, {@code compareTo} returns the 22 * difference of the two character values at position {@code k} in 23 * the two string -- that is, the value: 24 * <blockquote><pre> 25 * this.charAt(k)-anotherString.charAt(k) 26 * </pre></blockquote> 27 * If there is no index position at which they differ, then the shorter 28 * string lexicographically precedes the longer string. In this case, 29 * {@code compareTo} returns the difference of the lengths of the 30 * strings -- that is, the value: 31 * <blockquote><pre> 32 * this.length()-anotherString.length() 33 * </pre></blockquote> 34 * 35 * @param anotherString the {@code String} to be compared. 36 * @return the value {@code 0} if the argument string is equal to 37 * this string; a value less than {@code 0} if this string 38 * is lexicographically less than the string argument; and a 39 * value greater than {@code 0} if this string is 40 * lexicographically greater than the string argument. 41 */ 42 public int compareTo(String anotherString) { 43 int len1 = value.length; 44 int len2 = anotherString.value.length; 45 int lim = Math.min(len1, len2); 46 char v1[] = value; 47 char v2[] = anotherString.value; 48 49 int k = 0; 50 while (k < lim) { 51 char c1 = v1[k]; 52 char c2 = v2[k]; 53 if (c1 != c2) { 54 return c1 - c2; 55 } 56 k++; 57 } 58 return len1 - len2; 59 }
這是String類型的compareTo()函數的源代碼,註釋比較多,但註釋比較集中,並無影響了代碼的閱讀。我以爲把一些說明性的註釋寫在文件、類和函數的開頭,這樣不只有助於閱讀代碼,也不會喧賓奪主。其次Java源代碼的變量命名很是優美,值得我去學習。code
這就是我對註釋的一些嘮叨。blog