經驗分享:PHP編程的5個良好習慣(二)
學習良好的編程習慣能夠提高代碼質量和效率。像其他語言一樣,開發人員可以用 PHP 編寫出各種質量級別的代碼。根據具體的情況,一般的開發人員往往比優秀的開發人員的效率低 10%~20%。優秀的開發人員的效率更高,因為他們擁有豐富的經驗和良好的編程習慣。不良的編程習慣將會影響到效率。本文通過展示一些良好的編程習慣,幫助您成為更優秀的程序員。
3. 為代碼添加注釋
要為代碼添加良好的注釋有時似乎和編寫代碼一樣難。要了解應該為哪些內容添加注釋并不容易,因為我們常常傾向于注釋代碼當前做的事情。注釋代碼的目的是不錯的主意。在函數的不是很明顯的頭部代碼塊中,告訴讀者方法的輸入和輸出,以及方法的最初目標。
注釋代碼當前做什么是很常見的,但這是不必要的。如果代碼很復雜,不得不注釋它當前在做什么,這將暗示您應該重寫代碼,讓它更容易理解。學會使用良好的名稱和更短的方法,在不提供注釋說明其用途的情況下提高代碼的可讀性。
不良習慣:函數注釋過多或不足
清單 5 中的注釋僅告訴讀者代碼在做什么 — 它正在通過一個循環進行迭代或添加一個數字。但它忽略了它為什么 做當前的工作。這使維護該代碼的人員不知道是否可以安全地更改代碼(不引入新缺陷)。
清單 5. 不良習慣:函數注釋過多或不足
- <?php
- class ResultMessage
- {
- private $severity;
- private $message;
- public function __construct($sev, $msg)
- {
- $this->severity = $sev;
- $this->message = $msg;
- }
- public function getSeverity()
- {
- return $this->severity;
- }
- public function setSeverity($severity)
- {
- $this->severity = $severity;
- }
- public function getMessage()
- {
- return $this->message;
- }
- public function setMessage($msg)
- {
- $this->message = $msg;
- }
- }
- function cntMsgs($messages)
- {
- $n = 0;
- /* iterate through the messages... */
- foreach($messages as $m) {
- if ($m->getSeverity() == 'Error') {
- $n++; // add one to the result;
- }
- }
- return $n;}
- $messages = array(new ResultMessage("Error", "This is an error!"),
- new ResultMessage("Warning", "This is a warning!"),
- new ResultMessage("Error", "This is another error!"));
- $errs = cntMsgs($messages);
- echo("There are " . $errs . " errors in the result.\n");
- ?>
復制代碼良好習慣:帶注釋的函數和類
清單 6 中的注釋告訴讀者類和方法的目的。該注釋解釋了為什么代碼在做當前的工作,這對未來維護代碼十分有用。可能需要根據條件變更而修改代碼,如果能夠輕松了解代碼的目的,則修改起來很容易。
清單 6. 良好習慣:帶注釋的函數和類
- <?php
- /**
- *The ResultMessage class holds a message that can be returned
- * as a result of a process. The message has a severity and
- * message.
- *
- * @author nagood
- *
- */
- class ResultMessage
- {
- private $severity;
- private $message;
- /**
- * Constructor for the ResultMessage that allows you to assign
- * severity and message.
- * @param $sev See {@link getSeverity()}
- * @param $msg
- * @return unknown_type
- */
- public function __construct($sev, $msg)
- {
- $this->severity = $sev;
- $this->message = $msg;
- }
- /**
- * Returns the severity of the message. Should be one
- * "Information", "Warning", or "Error".
- * @return string Message severity
- */
- public function getSeverity()
- {
- return $this->severity;
- }
- /**
- * Sets the severity of the message
- * @param $severity
- * @return void
- */
- public function setSeverity($severity)
- {
- $this->severity = $severity;
- }
- public function getMessage()
- {
- return $this->message;
- }
- public function setMessage($msg)
- {
- $this->message = $msg;
- }
- }
- /*
- * Counts the messages with the given severity in the array
- * of messages.
- * @param $messages An array of ResultMessage
- * @return int Count of messages with a severity of "Error"
- */
- function countErrors($messages)
- {
- $matchingCount = 0;
- foreach($messages as $m) {
- if ($m->getSeverity() == "Error") {
- $matchingCount++;
- }
- }
- return $matchingCount;
- }
- $messages = array(new ResultMessage("Error", "This is an error!"),
- new ResultMessage("Warning", "This is a warning!"),
- new ResultMessage("Error", "This is another error!"));
- $errs = countErrors($messages);
- echo("There are " . $errs . " errors in the result.\n");
- ?>
#p#
4. 處理錯誤
根據大眾的經驗,如果要編寫健壯的應用程序,錯誤處理要遵循 80/20 規則:80% 的代碼用于處理異常和驗證,20% 的代碼用于完成實際工作。在編寫程序的基本邏輯(happy-path)代碼時經常這樣做。這意味著編寫適用于基本條件的代碼,即所有的數據都是可用的,所有的條件符合預期。這樣的代碼在應用程序的生命周期中可能很脆弱。另一個極端是,甚至需要花大量時間為從未遇到過的條件編寫代碼。
這一習慣要求您編寫足夠的錯誤處理代碼,而不是編寫對付所有錯誤的代碼,以致代碼遲遲不能完成。
不良習慣:根本沒有錯誤處理代碼
清單 7 中的代碼演示了兩個不良習慣。***,沒有檢查輸入的參數,即使知道處于某些狀態的參數會造成方法出現異常。第二,代碼調用一個可能拋出異常的方法,但沒有處理該異常。當發生問題時,代碼的作者或維護該代碼的人員只能猜測問題的根源。
清單 7. 不良習慣:不處理錯誤條件
- <?php
- // Get the actual name of the
- function convertDayOfWeekToName($day)
- {
- $dayNames = array(
- "Sunday",
- "Monday",
- "Tuesday",
- "Wednesday",
- "Thursday",
- "Friday",
- "Saturday");
- return $dayNames[$day];
- }
- echo("The name of the 0 day is: " . convertDayOfWeekToName(0) . "\n");
- echo("The name of the 10 day is: " . convertDayOfWeekToName(10) . "\n");
- echo("The name of the 'orange' day is: " . convertDayOfWeekToName('orange') . "\n");
- ?>
復制代碼良好習慣:處理異常
清單 8 展示了以有意義的方式拋出和處理異常。額外的錯誤處理不僅使代碼更加健壯,它還提高代碼的可讀性,使代碼更容易理解。處理異常的方式很好地說明了原作者在編寫方法時的意圖。
清單 8. 良好習慣:處理異常
- <?php
- /**
- * This is the exception thrown if the day of the week is invalid.
- * @author nagood
- *
- */
- class InvalidDayOfWeekException extends Exception { }
- class InvalidDayFormatException extends Exception { }
- /**
- * Gets the name of the day given the day in the week. Will
- * return an error if the value supplied is out of range.
- *
- * @param $day
- * @return unknown_type
- */
- function convertDayOfWeekToName($day)
- {
- if (! is_numeric($day)) {
- throw new InvalidDayFormatException('The value \'' . $day . '\' is an ' .
- 'invalid format for a day of week.');
- }
- if (($day > 6) || ($day < 0)) {
- throw new InvalidDayOfWeekException('The day number \'' . $day . '\' is an ' .
- 'invalid day of the week. Expecting 0-6.');
- }
- $dayNames = array(
- "Sunday",
- "Monday",
- "Tuesday",
- "Wednesday",
- "Thursday",
- "Friday",
- "Saturday");
- return $dayNames[$day];
- }
- echo("The name of the 0 day is: " . convertDayOfWeekToName(0) . "\n");
- try {
- echo("The name of the 10 day is: " . convertDayOfWeekToName(10) . "\n");
- } catch (InvalidDayOfWeekException $e) {
- echo ("Encountered error while trying to convert value: " . $e->getMessage() . "\n");
- }
- try {
- echo("The name of the 'orange' day is: " . convertDayOfWeekToName('orange') . "\n");
- } catch (InvalidDayFormatException $e) {
- echo ("Encountered error while trying to convert value: " . $e->getMessage() . "\n");
- }
- ?>
復制代碼雖然檢查參數是一種確認 — 如果您要求參數處于某種狀態,這將對使用方法的人很有幫助 — 但是您應該檢查它們并拋出有意義的異常:
- 處理異常要盡量與出現的問題緊密相關。
- 專門處理每個異常。
希望對你有幫助,接下一篇,經驗分享:PHP編程的5個良好習慣(三)
【編輯推薦】