成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

經驗分享:PHP編程的5個良好習慣(二)

開發 后端
本文介紹的是PHP編程的幾個良好習慣,分為兩篇為大家介紹,希望對你有幫助,一起來看吧!

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

上一篇>>

3. 為代碼添加注釋

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

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

不良習慣:函數注釋過多或不足

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

清單 5. 不良習慣:函數注釋過多或不足

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

復制代碼良好習慣:帶注釋的函數和類

清單 6 中的注釋告訴讀者類和方法的目的。該注釋解釋了為什么代碼在做當前的工作,這對未來維護代碼十分有用。可能需要根據條件變更而修改代碼,如果能夠輕松了解代碼的目的,則修改起來很容易。

清單 6. 良好習慣:帶注釋的函數和類

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

#p#

4. 處理錯誤

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

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

不良習慣:根本沒有錯誤處理代碼

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

清單 7. 不良習慣:不處理錯誤條件

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

復制代碼良好習慣:處理異常

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

清單 8. 良好習慣:處理異常

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

復制代碼雖然檢查參數是一種確認 — 如果您要求參數處于某種狀態,這將對使用方法的人很有幫助 — 但是您應該檢查它們并拋出有意義的異常:

  • 處理異常要盡量與出現的問題緊密相關。
  • 專門處理每個異常。

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

【編輯推薦】

  1. PHP新手 詳細介紹PHP代碼規范
  2. PHP中IIS7實現基本身份驗證的方法
  3. 分享PHP網站建設的流程與步驟
  4. PHP新手 學習基本語法
  5. PHP新手 學習變量和常量
責任編輯:于鐵 來源: 大家論壇
相關推薦

2011-07-07 15:26:28

PHP編程習慣

2011-07-07 15:48:22

PHP編程習慣

2009-01-03 14:34:49

ibmdwPHP

2009-01-03 10:40:41

PHP編程代碼

2011-07-14 22:04:16

VC++

2010-04-08 11:17:06

Unix操作系統

2022-04-08 14:38:43

程序員習慣終端

2010-06-11 14:35:18

UML序列圖

2011-04-13 10:16:41

編程習慣

2011-03-29 12:41:49

編程

2020-04-22 10:35:07

編程學習技術

2011-07-15 15:10:37

PHP

2011-03-24 09:25:54

程序員編程

2024-08-20 14:19:29

2024-05-23 12:09:01

2022-10-08 10:42:20

Linux虛擬機

2010-09-02 12:54:30

CSS

2020-11-02 13:03:28

MySQLSQL索引

2024-02-26 08:13:51

MySQLSQL性能

2021-08-17 09:55:50

pandas 8indexPython
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久精品小视频 | 欧美激情综合 | av网站在线看 | 亚洲婷婷一区 | 亚洲国产精品精华素 | 伊人爽| 亚洲精品1| 国产蜜臀 | 亚洲综合伊人 | 亚洲iv一区二区三区 | www九色| 一区二区免费视频 | 午夜精品一区二区三区在线视频 | 国产乱码精品一区二区三区中文 | 色精品 | 一区二区三区四区在线视频 | 国产一级精品毛片 | 亚洲国产成人精品女人久久久 | 二区av| 欧美福利在线 | 国产在线a | 亚洲第一成人影院 | 一级毛片网 | 视频一区二区在线观看 | 久久国产精品视频免费看 | 香蕉视频黄色 | 欧美日韩久久精品 | 成年女人免费v片 | 国产内谢 | 精产嫩模国品一二三区 | 亚洲视频在线观看 | 91在线一区 | 欧美日韩在线一区二区 | 国产中文| 欧美一区二区在线看 | 天天夜天天操 | 影视先锋av资源噜噜 | 中文字幕不卡在线观看 | 国产美女一区二区 | 午夜影院在线观看 | 在线黄色影院 |