瘋狂Java學習筆記(77)-----------凝視注意事項

來源:互聯網
上載者:User

標籤:rri   href   ret   state   var   hose   children   習慣   開發   

代碼凝視,能夠說是比代碼本身更重要。這裡有一些方法能夠確保你寫在代碼中的凝視是友好的:

不要反覆閱讀者已經知道的內容

能明白說明代碼是做什麼的凝視對我們是沒有協助的。

// If the color is red, turn it greenif (color.is_red()) {  color.turn_green();}
要凝視說明推理和曆史

假設代碼中的商務邏輯以後可能須要更新或更改,那就應該留下凝視:)

/* The API currently returns an array of itemseven though that will change in an upcoming ticket.Therefore, be sure to change the loop style here so thatwe properly iterate over an object */var api_result = {items: ["one", "two"]},    items = api_result.items,    num_items = items.length;for(var x = 0; x < num_items; x++) {  ...}
同一行的凝視不要寫得非常長

沒什麼比拖動水平滾動欄來閱讀凝視更令開發人員髮指的了。其實,大多數開發人員都會選擇忽略這類凝視。由於讀起來真的非常不方便。

function Person(name) {  this.name = name;  this.first_name = name.split(" ")[0]; // This is just a shot in the dark here. If we can extract the first name, let‘s do it}
要把長凝視放在邏輯上面,短凝視放在後面

凝視假設不超過120個字元那能夠放在代碼旁邊。否則,就應該直接把凝視放到語句上面。

if (person.age < 21) {  person.can_drink = false; // 21 drinking age  /* Fees are given to those under 25, but only in     some states. */  person.has_car_rental_fee = function(state) {    if (state === "MI") {      return true;    }  };}
不要為了凝視而加入不必要的凝視

畫蛇添足的凝視會造成混亂。或許在學校裡老師教你要給全部語句加入凝視,這會協助開發人員更好地理解。但這是錯的。

誰要這麼說,那你就立刻上給他個兩大耳刮子。

代碼應該保持乾淨簡潔,這是毋庸置疑的。假設你的代碼須要逐行解釋說明,那麼你最須要做的是重構。

if (person.age >= 21) {  person.can_drink = true; // A person can drink at 21  person.can_smoke = true; // A person can smoke at 18  person.can_wed = true; // A person can get married at 18  person.can_see_all_movies = true; // A person can see all movies at 17  //I hate babies and children and all things pure because I comment too much}
凝視要拼字正確

不要為代碼凝視中的拼字錯誤找借口。IDE能夠為你檢查拼字。

假設沒有這個功能,那就去下載外掛程式,自己動手!

要多多練習

熟能生巧。試著寫一些實用的凝視。能夠問問其它開發人員你的凝視是否實用。隨著時間的推移。你會慢慢懂得如何才算是友好的凝視。

要審查別人的凝視

在代碼審查時,我們往往會忽略查看凝視。不要怕要求很多其它的凝視。你應該提出質疑。假設每一個人都養成寫好凝視的好習慣,那麼世界將會更美好。

總結

凝視是開發進程中很重要的一部分,但我們不應該為了凝視而凝視。凝視應該是實用的。簡潔的,應該是對代碼的一種補充。

凝視不應該用於逐行地解釋代碼,相反,它應該用於解釋商務邏輯,推理以及對將來的啟發。

 

轉自http://www.codeceo.com/article/comments-do-and-dont.html

瘋狂Java學習筆記(77)-----------凝視注意事項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.