日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
經(jīng)驗分享:PHP編程的5個良好習(xí)慣(二)

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

接上一篇>>

3. 為代碼添加注釋

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

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

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

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

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

 
 
 
  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)前的工作,這對未來維護代碼十分有用??赡苄枰鶕?jù)條件變更而修改代碼,如果能夠輕松了解代碼的目的,則修改起來很容易。

清單 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. 處理錯誤

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

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

不良習(xí)慣:根本沒有錯誤處理代碼

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

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

 
 
 
  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 展示了以有意義的方式拋出和處理異常。額外的錯誤處理不僅使代碼更加健壯,它還提高代碼的可讀性,使代碼更容易理解。處理異常的方式很好地說明了原作者在編寫方法時的意圖。

清單 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),這將對使用方法的人很有幫助 — 但是您應(yīng)該檢查它們并拋出有意義的異常:

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

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

【編輯推薦】

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

標(biāo)題名稱:經(jīng)驗分享:PHP編程的5個良好習(xí)慣(二)
分享鏈接:http://www.5511xx.com/article/dpphjoj.html