av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

經(jīng)驗(yàn)分享:PHP編程的5個(gè)良好習(xí)慣(二)

學(xué)習(xí)良好的編程習(xí)慣能夠提高代碼質(zhì)量和效率。像其他語(yǔ)言一樣,開(kāi)發(fā)人員可以用 PHP 編寫(xiě)出各種質(zhì)量級(jí)別的代碼。根據(jù)具體的情況,一般的開(kāi)發(fā)人員往往比優(yōu)秀的開(kāi)發(fā)人員的效率低 10%~20%。優(yōu)秀的開(kāi)發(fā)人員的效率更高,因?yàn)樗麄儞碛胸S富的經(jīng)驗(yàn)和良好的編程習(xí)慣。不良的編程習(xí)慣將會(huì)影響到效率。本文通過(guò)展示一些良好的編程習(xí)慣,幫助您成為更優(yōu)秀的程序員。

接上一篇>>

3. 為代碼添加注釋

要為代碼添加良好的注釋有時(shí)似乎和編寫(xiě)代碼一樣難。要了解應(yīng)該為哪些內(nèi)容添加注釋并不容易,因?yàn)槲覀兂3A向于注釋代碼當(dāng)前做的事情。注釋代碼的目的是不錯(cuò)的主意。在函數(shù)的不是很明顯的頭部代碼塊中,告訴讀者方法的輸入和輸出,以及方法的最初目標(biāo)。

注釋代碼當(dāng)前做什么是很常見(jiàn)的,但這是不必要的。如果代碼很復(fù)雜,不得不注釋它當(dāng)前在做什么,這將暗示您應(yīng)該重寫(xiě)代碼,讓它更容易理解。學(xué)會(huì)使用良好的名稱和更短的方法,在不提供注釋說(shuō)明其用途的情況下提高代碼的可讀性。

不良習(xí)慣:函數(shù)注釋過(guò)多或不足

清單 5 中的注釋僅告訴讀者代碼在做什么 — 它正在通過(guò)一個(gè)循環(huán)進(jìn)行迭代或添加一個(gè)數(shù)字。但它忽略了它為什么 做當(dāng)前的工作。這使維護(hù)該代碼的人員不知道是否可以安全地更改代碼(不引入新缺陷)。

清單 5. 不良習(xí)慣:函數(shù)注釋過(guò)多或不足

 
 
 
  1. class ResultMessage  
  2. {  
  3. private $severity;  
  4. private $message;  
  5. public function __construct($sev, $msg)  
  6. {  
  7. $this->severity = $sev;  
  8. $this->message = $msg;  
  9. }  
  10. public function getSeverity()  
  11. {  
  12. return $this->severity;  
  13. }  
  14. public function setSeverity($severity)  
  15. {  
  16. $this->severity = $severity;  
  17. }  
  18. public function getMessage()  
  19. {  
  20. return $this->message;  
  21. }  
  22. public function setMessage($msg)  
  23. {  
  24. $this->message = $msg;  
  25. }  
  26. }  
  27. function cntMsgs($messages)  
  28. {  
  29. $n = 0;  
  30. /* iterate through the messages... */ 
  31. foreach($messages as $m) {  
  32. if ($m->getSeverity() == 'Error') {  
  33. $n++; // add one to the result;  
  34. }  
  35. }  
  36. return $n;}  
  37. $messages = array(new ResultMessage("Error", "This is an error!"),  
  38. new ResultMessage("Warning", "This is a warning!"),  
  39. new ResultMessage("Error", "This is another error!"));  
  40. $errs = cntMsgs($messages);  
  41. echo("There are " . $errs . " errors in the result.\n");  
  42. ?> 

復(fù)制代碼良好習(xí)慣:帶注釋的函數(shù)和類

清單 6 中的注釋告訴讀者類和方法的目的。該注釋解釋了為什么代碼在做當(dāng)前的工作,這對(duì)未來(lái)維護(hù)代碼十分有用??赡苄枰鶕?jù)條件變更而修改代碼,如果能夠輕松了解代碼的目的,則修改起來(lái)很容易。

清單 6. 良好習(xí)慣:帶注釋的函數(shù)和類

 
 
 
  1. /**  
  2. *The ResultMessage class holds a message that can be returned  
  3. * as a result of a process. The message has a severity and  
  4. * message.  
  5. *  
  6. * @author nagood  
  7. *  
  8. */ 
  9. class ResultMessage  
  10. {  
  11. private $severity;  
  12. private $message;  
  13. /**  
  14. * Constructor for the ResultMessage that allows you to assign  
  15. * severity and message.  
  16. * @param $sev See {@link getSeverity()}  
  17. * @param $msg  
  18. * @return unknown_type  
  19. */ 
  20. public function __construct($sev, $msg)  
  21. {  
  22. $this->severity = $sev;  
  23. $this->message = $msg;  
  24. }  
  25. /**  
  26. * Returns the severity of the message. Should be one  
  27. * "Information", "Warning", or "Error".  
  28. * @return string Message severity  
  29. */ 
  30. public function getSeverity()  
  31. {  
  32. return $this->severity;  
  33. }  
  34. /**  
  35. * Sets the severity of the message  
  36. * @param $severity  
  37. * @return void  
  38. */ 
  39. public function setSeverity($severity)  
  40. {  
  41. $this->severity = $severity;  
  42. }  
  43. public function getMessage()  
  44. {  
  45. return $this->message;  
  46. }  
  47. public function setMessage($msg)  
  48. {  
  49. $this->message = $msg;  
  50. }  
  51. }  
  52. /*  
  53. * Counts the messages with the given severity in the array  
  54. * of messages.  
  55. * @param $messages An array of ResultMessage  
  56. * @return int Count of messages with a severity of "Error"  
  57. */ 
  58. function countErrors($messages)  
  59. {  
  60. $matchingCount = 0;  
  61. foreach($messages as $m) {  
  62. if ($m->getSeverity() == "Error") {  
  63. $matchingCount++;  
  64. }  
  65. }  
  66. return $matchingCount;  
  67. }  
  68. $messages = array(new ResultMessage("Error", "This is an error!"),  
  69. new ResultMessage("Warning", "This is a warning!"),  
  70. new ResultMessage("Error", "This is another error!"));  
  71. $errs = countErrors($messages);  
  72. echo("There are " . $errs . " errors in the result.\n");  
  73. ?> 

#p#

4. 處理錯(cuò)誤

根據(jù)大眾的經(jīng)驗(yàn),如果要編寫(xiě)健壯的應(yīng)用程序,錯(cuò)誤處理要遵循 80/20 規(guī)則:80% 的代碼用于處理異常和驗(yàn)證,20% 的代碼用于完成實(shí)際工作。在編寫(xiě)程序的基本邏輯(happy-path)代碼時(shí)經(jīng)常這樣做。這意味著編寫(xiě)適用于基本條件的代碼,即所有的數(shù)據(jù)都是可用的,所有的條件符合預(yù)期。這樣的代碼在應(yīng)用程序的生命周期中可能很脆弱。另一個(gè)極端是,甚至需要花大量時(shí)間為從未遇到過(guò)的條件編寫(xiě)代碼。

這一習(xí)慣要求您編寫(xiě)足夠的錯(cuò)誤處理代碼,而不是編寫(xiě)對(duì)付所有錯(cuò)誤的代碼,以致代碼遲遲不能完成。

不良習(xí)慣:根本沒(méi)有錯(cuò)誤處理代碼

清單 7 中的代碼演示了兩個(gè)不良習(xí)慣。***,沒(méi)有檢查輸入的參數(shù),即使知道處于某些狀態(tài)的參數(shù)會(huì)造成方法出現(xiàn)異常。第二,代碼調(diào)用一個(gè)可能拋出異常的方法,但沒(méi)有處理該異常。當(dāng)發(fā)生問(wèn)題時(shí),代碼的作者或維護(hù)該代碼的人員只能猜測(cè)問(wèn)題的根源。

清單 7. 不良習(xí)慣:不處理錯(cuò)誤條件

 
 
 
  1. // Get the actual name of the  
  2. function convertDayOfWeekToName($day)  
  3. {  
  4. $dayNames = array(  
  5. "Sunday",  
  6. "Monday",  
  7. "Tuesday",  
  8. "Wednesday",  
  9. "Thursday",  
  10. "Friday",  
  11. "Saturday");  
  12. return $dayNames[$day];  
  13. }  
  14. echo("The name of the 0 day is: " . convertDayOfWeekToName(0) . "\n");  
  15. echo("The name of the 10 day is: " . convertDayOfWeekToName(10) . "\n");  
  16. echo("The name of the 'orange' day is: " . convertDayOfWeekToName('orange') . "\n");  
  17. ?> 

復(fù)制代碼良好習(xí)慣:處理異常

清單 8 展示了以有意義的方式拋出和處理異常。額外的錯(cuò)誤處理不僅使代碼更加健壯,它還提高代碼的可讀性,使代碼更容易理解。處理異常的方式很好地說(shuō)明了原作者在編寫(xiě)方法時(shí)的意圖。

清單 8. 良好習(xí)慣:處理異常

 
 
 
  1. /**  
  2. * This is the exception thrown if the day of the week is invalid.  
  3. * @author nagood  
  4. *  
  5. */ 
  6. class InvalidDayOfWeekException extends Exception { }  
  7. class InvalidDayFormatException extends Exception { }  
  8. /**  
  9. * Gets the name of the day given the day in the week. Will  
  10. * return an error if the value supplied is out of range.  
  11. *  
  12. * @param $day  
  13. * @return unknown_type  
  14. */ 
  15. function convertDayOfWeekToName($day)  
  16. {  
  17. if (! is_numeric($day)) {  
  18. throw new InvalidDayFormatException('The value \'' . $day . '\' is an ' .  
  19. 'invalid format for a day of week.');  
  20. }  
  21. if (($day > 6) || ($day < 0)) {  
  22. throw new InvalidDayOfWeekException('The day number \'' . $day . '\' is an ' .  
  23. 'invalid day of the week. Expecting 0-6.');  
  24. }  
  25. $dayNames = array(  
  26. "Sunday",  
  27. "Monday",  
  28. "Tuesday",  
  29. "Wednesday",  
  30. "Thursday",  
  31. "Friday",  
  32. "Saturday");  
  33. return $dayNames[$day];  
  34. }  
  35. echo("The name of the 0 day is: " . convertDayOfWeekToName(0) . "\n");  
  36. try {  
  37. echo("The name of the 10 day is: " . convertDayOfWeekToName(10) . "\n");  
  38. } catch (InvalidDayOfWeekException $e) {  
  39. echo ("Encountered error while trying to convert value: " . $e->getMessage() . "\n");  
  40. }  
  41. try {  
  42. echo("The name of the 'orange' day is: " . convertDayOfWeekToName('orange') . "\n");  
  43. } catch (InvalidDayFormatException $e) {  
  44. echo ("Encountered error while trying to convert value: " . $e->getMessage() . "\n");  
  45. }  
  46. ?> 

復(fù)制代碼雖然檢查參數(shù)是一種確認(rèn) — 如果您要求參數(shù)處于某種狀態(tài),這將對(duì)使用方法的人很有幫助 — 但是您應(yīng)該檢查它們并拋出有意義的異常:

  • 處理異常要盡量與出現(xiàn)的問(wèn)題緊密相關(guān)。
  • 專門(mén)處理每個(gè)異常。

希望對(duì)你有幫助,接下一篇,經(jīng)驗(yàn)分享:PHP編程的5個(gè)良好習(xí)慣(三)

【編輯推薦】

  1. PHP新手 詳細(xì)介紹PHP代碼規(guī)范
  2. PHP中IIS7實(shí)現(xiàn)基本身份驗(yàn)證的方法
  3. 分享PHP網(wǎng)站建設(shè)的流程與步驟
  4. PHP新手 學(xué)習(xí)基本語(yǔ)法
  5. PHP新手 學(xué)習(xí)變量和常量

網(wǎng)站名稱:經(jīng)驗(yàn)分享:PHP編程的5個(gè)良好習(xí)慣(二)
轉(zhuǎn)載注明:http://uogjgqi.cn/article/dpphjoj.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流