日韩无码专区无码一级三级片|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)秀的程序員。

創(chuàng)新互聯(lián)建站自2013年創(chuàng)立以來,先為宣化等服務(wù)建站,宣化等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為宣化企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

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

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

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

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

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

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

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

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

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

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

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

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

結(jié)束語

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

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

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


當前標題:經(jīng)驗分享:PHP編程的5個良好習(xí)慣(三)
鏈接URL:http://www.5511xx.com/article/cdhgcgd.html