自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開(kāi)發(fā) 后端
本文介紹的是PHP編程的幾個(gè)良好習(xí)慣,分為兩篇為大家介紹,希望對(duì)你有幫助,一起來(lái)看吧!

學(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)秀的程序員。

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

5. 切忌使用復(fù)制粘貼

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

一定要注意,不要將代碼從應(yīng)用程序的一部分復(fù)制粘貼到另一部分。如果您采用這種方式,請(qǐng)停止這個(gè)不良的習(xí)慣,然后考慮將這段代碼重寫(xiě)為可重用的。一般而言,將代碼放置到一個(gè)地方便于日后的維護(hù),因?yàn)檫@樣只需在一個(gè)地方更改代碼。

不良習(xí)慣:類(lèi)似的代碼段

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

清單 9. 不良習(xí)慣:類(lèi)似的代碼段

  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.?> 

復(fù)制代碼良好習(xí)慣:帶參數(shù)的可重用函數(shù)

清單 10 展示了修改后的代碼,它將復(fù)制的代碼放到一個(gè)方法中。另一個(gè)方法也進(jìn)行了更改,它現(xiàn)在將任務(wù)委托給新的方法。構(gòu)建通用的方法需要花時(shí)間設(shè)計(jì),并且這樣做使您能停下來(lái)思考,而不是本能地使用復(fù)制粘貼。但有必要進(jìn)行更改時(shí),對(duì)通用的方法投入的時(shí)間將得到回報(bào)。

清單 10. 良好習(xí)慣:帶參數(shù)的可重用函數(shù)

  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. ?> 

結(jié)束語(yǔ)

如果您在編寫(xiě) PHP 代碼的過(guò)程中養(yǎng)成本文討論的良好習(xí)慣,您將能夠構(gòu)建易讀、易理解、易維護(hù)的代碼。使用這種方式構(gòu)建的易維護(hù)代碼將降低調(diào)試、修復(fù)和擴(kuò)展代碼所面臨的風(fēng)險(xiǎn)。

使用良好的名稱(chēng)和更短的方法能夠提高代碼的可讀性。注釋代碼的目的有利于代碼理解和擴(kuò)展。適當(dāng)?shù)靥幚礤e(cuò)誤會(huì)使代碼更加健壯。***,停止使用復(fù)制粘貼,保持代碼干凈,提高可重用性。

 

到這,五個(gè)良好的習(xí)慣都給大家介紹完了。希望對(duì)你有幫助。

【編輯推薦】

  1. 提高PHP速度的幾種辦法
  2. PHP愛(ài)好者請(qǐng)堅(jiān)定你們的信念!
  3. 警惕 PHP程序員最易犯10種錯(cuò)誤
  4. 讓PHP網(wǎng)站跑的更快 如何優(yōu)化PHP
  5. 簡(jiǎn)單說(shuō)說(shuō)PHP優(yōu)化
責(zé)任編輯:于鐵 來(lái)源: 大家論壇
相關(guān)推薦

2011-07-07 15:36:51

PHP

2011-07-07 15:26:28

PHP編程習(xí)慣

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操作系統(tǒng)

2022-04-08 14:38:43

程序員習(xí)慣終端

2010-06-11 14:35:18

UML序列圖

2011-04-13 10:16:41

編程習(xí)慣

2011-03-29 12:41:49

編程

2020-04-22 10:35:07

編程學(xué)習(xí)技術(shù)

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虛擬機(jī)

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性能
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)