自拍偷在线精品自拍偷,亚洲欧美中文日韩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)秀的程序員。

上一篇>>

3. 為代碼添加注釋

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

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

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

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

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

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

復(fù)制代碼良好習(xí)慣:帶注釋的函數(shù)和類(lèi)

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

清單 6. 良好習(xí)慣:帶注釋的函數(shù)和類(lèi)

  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. 處理錯(cuò)誤

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

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

不良習(xí)慣:根本沒(méi)有錯(cuò)誤處理代碼

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

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

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

復(fù)制代碼良好習(xí)慣:處理異常

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

清單 8. 良好習(xí)慣:處理異常

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

復(fù)制代碼雖然檢查參數(shù)是一種確認(rèn) — 如果您要求參數(shù)處于某種狀態(tài),這將對(duì)使用方法的人很有幫助 — 但是您應(yīng)該檢查它們并拋出有意義的異常:

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

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

【編輯推薦】

  1. PHP新手 詳細(xì)介紹PHP代碼規(guī)范
  2. PHP中IIS7實(shí)現(xiàn)基本身份驗(yàn)證的方法
  3. 分享PHP網(wǎng)站建設(shè)的流程與步驟
  4. PHP新手 學(xué)習(xí)基本語(yǔ)法
  5. PHP新手 學(xué)習(xí)變量和常量
責(zé)任編輯:于鐵 來(lái)源: 大家論壇
相關(guān)推薦

2011-07-07 15:48:22

PHP編程習(xí)慣

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)