經(jīng)驗(yàn)分享:PHP編程的5個(gè)良好習(xí)慣(二)
學(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)槲覀兂3A向于注釋代碼當(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ò)多或不足
- <?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");
- ?>
復(fù)制代碼良好習(xí)慣:帶注釋的函數(shù)和類(lèi)
清單 6 中的注釋告訴讀者類(lèi)和方法的目的。該注釋解釋了為什么代碼在做當(dāng)前的工作,這對(duì)未來(lái)維護(hù)代碼十分有用。可能需要根據(jù)條件變更而修改代碼,如果能夠輕松了解代碼的目的,則修改起來(lái)很容易。
清單 6. 良好習(xí)慣:帶注釋的函數(shù)和類(lèi)
- <?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. 處理錯(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ò)誤條件
- <?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");
- ?>
復(fù)制代碼良好習(xí)慣:處理異常
清單 8 展示了以有意義的方式拋出和處理異常。額外的錯(cuò)誤處理不僅使代碼更加健壯,它還提高代碼的可讀性,使代碼更容易理解。處理異常的方式很好地說(shuō)明了原作者在編寫(xiě)方法時(shí)的意圖。
清單 8. 良好習(xí)慣:處理異常
- <?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");
- }
- ?>
復(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í)慣(三)
【編輯推薦】