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

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

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

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

接上一篇,經驗分享:PHP編程的5個良好習慣(二)

5. 切忌使用復制粘貼

您可以從其他地方將代碼復制粘貼到自己的代碼編輯器,但這樣做有利也有弊。好的一面是,從一個示例或模板中復制代碼能夠避免很多錯誤。不好的一面是,這容易帶來大量的類似編程方式。

一定要注意,不要將代碼從應用程序的一部分復制粘貼到另一部分。如果您采用這種方式,請停止這個不良的習慣,然后考慮將這段代碼重寫為可重用的。一般而言,將代碼放置到一個地方便于日后的維護,因為這樣只需在一個地方更改代碼。

不良習慣:類似的代碼段

清單 9 給出了幾個幾乎一樣的方法,只是其中的值不同而已。有一些工具可以幫助找到復制粘貼過來的代碼(參見 參考資料)。

清單 9. 不良習慣:類似的代碼段

  1. <?php  
  2. /**  
  3. * Counts the number of messages found in the array of  
  4. * ResultMessage with the getSeverity() value of "Error"  
  5. * @param $messages An array of ResultMessage  
  6. * @return unknown_type  
  7. */ 
  8. function countErrors($messages)  
  9. {  
  10. $matchingCount = 0;  
  11. foreach($messages as $m) {  
  12. if ($m->getSeverity() == "Error") {  
  13. $matchingCount++;  
  14. }  
  15. }  
  16. return $matchingCount;  
  17. }  
  18. /**  
  19. * Counts the number of messages found in the array of  
  20. * ResultMessage with the getSeverity() value of "Warning"  
  21. *  
  22. * @param $messages An array of ResultMessage  
  23. * @return unknown_type  
  24. */ 
  25. function countWarnings($messages)  
  26. {  
  27. $matchingCount = 0;  
  28. foreach($messages as $m) {  
  29. if ($m->getSeverity() == "Warning") {  
  30. $matchingCount++;  
  31. }  
  32. }  
  33. return $matchingCount;  
  34. }  
  35. /**  
  36. * Counts the number of messages found in the array of  
  37. * ResultMessage with the getSeverity() value of "Information"  
  38. *  
  39. * @param $messages An array of ResultMessage  
  40. * @return unknown_type  
  41. */ 
  42. function countInformation($messages)  
  43. {  
  44. $matchingCount = 0;  
  45. foreach($messages as $m) {  
  46. if ($m->getSeverity() == "Information") {  
  47. $matchingCount++;  
  48. }  
  49. }  
  50. return $matchingCount;  
  51. }  
  52. $messages = array(new ResultMessage("Error""This is an error!"),  
  53. new ResultMessage("Warning""This is a warning!"),  
  54. new ResultMessage("Error""This is another error!"));  
  55. $errs = countErrors($messages);  
  56. echo("There are " . $errs . " errors in the result.\n");  
  57. 63.?> 

復制代碼良好習慣:帶參數的可重用函數

清單 10 展示了修改后的代碼,它將復制的代碼放到一個方法中。另一個方法也進行了更改,它現在將任務委托給新的方法。構建通用的方法需要花時間設計,并且這樣做使您能停下來思考,而不是本能地使用復制粘貼。但有必要進行更改時,對通用的方法投入的時間將得到回報。

清單 10. 良好習慣:帶參數的可重用函數

  1. <?php  
  2. /*  
  3. * Counts the messages with the given severity in the array  
  4. * of messages.  
  5. * @param $messages An array of ResultMessage  
  6. * @return int Count of messages matching $withSeverity  
  7. */ 
  8. function countMessages($messages$withSeverity)  
  9. {  
  10. $matchingCount = 0;  
  11. foreach($messages as $m) {  
  12. if ($m->getSeverity() == $withSeverity) {  
  13. $matchingCount++;  
  14. }  
  15. }  
  16. return $matchingCount;  
  17. }  
  18. /**  
  19. * Counts the number of messages found in the array of  
  20. * ResultMessage with the getSeverity() value of "Error"  
  21. * @param $messages An array of ResultMessage  
  22. * @return unknown_type  
  23. */ 
  24. function countErrors($messages)  
  25. {  
  26. return countMessages($messages"Errors");  
  27. }  
  28. /**  
  29. * Counts the number of messages found in the array of  
  30. * ResultMessage with the getSeverity() value of "Warning"  
  31. * @param $messages An array of ResultMessage  
  32. * @return unknown_type  
  33. */ 
  34. function countWarnings($messages)  
  35. {  
  36. return countMessages($messages"Warning");}  
  37. /**  
  38. * Counts the number of messages found in the array of  
  39. * ResultMessage with the getSeverity() value of "Warning"  
  40. *  
  41. * @param $messages An array of ResultMessage  
  42. * @return unknown_type  
  43. */ 
  44. function countInformation($messages)  
  45. {  
  46. return countMessages($messages"Information");  
  47. }  
  48. $messages = array(new ResultMessage("Error""This is an error!"),  
  49. new ResultMessage("Warning""This is a warning!"),  
  50. new ResultMessage("Error""This is another error!"));  
  51. $errs = countErrors($messages);  
  52. echo("There are " . $errs . " errors in the result.\n");  
  53. ?> 

結束語

如果您在編寫 PHP 代碼的過程中養成本文討論的良好習慣,您將能夠構建易讀、易理解、易維護的代碼。使用這種方式構建的易維護代碼將降低調試、修復和擴展代碼所面臨的風險。

使用良好的名稱和更短的方法能夠提高代碼的可讀性。注釋代碼的目的有利于代碼理解和擴展。適當地處理錯誤會使代碼更加健壯。***,停止使用復制粘貼,保持代碼干凈,提高可重用性。

 

到這,五個良好的習慣都給大家介紹完了。希望對你有幫助。

【編輯推薦】

  1. 提高PHP速度的幾種辦法
  2. PHP愛好者請堅定你們的信念!
  3. 警惕 PHP程序員最易犯10種錯誤
  4. 讓PHP網站跑的更快 如何優化PHP
  5. 簡單說說PHP優化
責任編輯:于鐵 來源: 大家論壇
相關推薦

2011-07-07 15:36:51

PHP

2011-07-07 15:26:28

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

2021-08-17 09:55:50

pandas 8indexPython

2020-11-02 13:03:28

MySQLSQL索引

2024-02-26 08:13:51

MySQLSQL性能
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲一区在线日韩在线深爱 | 国产成人99久久亚洲综合精品 | 欧美一区二区三区的 | 一级午夜aaa免费看三区 | 亚洲成人观看 | 国产精品亚洲一区二区三区在线 | 亚洲情综合五月天 | 福利片在线 | 欧美不卡一区二区 | 亚洲成年人免费网站 | 91就要激情 | 国产一区二区三区久久久久久久久 | 91免费看片| 精品亚洲一区二区三区四区五区 | 精品国产欧美日韩不卡在线观看 | 精品久久久一区 | 91福利网 | 蜜桃在线一区二区三区 | 久草精品视频 | 欧美一级特黄aaa大片在线观看 | www.成人在线视频 | 夜夜骑天天干 | 亚洲精品乱码久久久久久久久久 | 亚洲3级| 国产精品国产三级国产aⅴ原创 | 亚洲一区二区三区免费视频 | 欧美日韩中文字幕在线 | 亚洲国产精品一区二区久久 | 成人在线观看免费 | 久久天堂网 | 狠狠狠色丁香婷婷综合久久五月 | 久久精品亚洲欧美日韩精品中文字幕 | av黄色免费| 中文字幕精品一区二区三区在线 | 国产在线观看一区二区 | 国产一区2区| 高清国产午夜精品久久久久久 | 在线视频日韩 | 91久久精品一区二区二区 | 午夜影院在线观看免费 | 91精品国产777在线观看 |