把长函数分割会导致效率降低,所以要注意,这个好习惯不要使用过度。这样做可能也会引起阅读性差,跟原来人家是一个整体时没什么区别。
注释代码
注释你的代码有时就像你刚着手写代码一样困难。明确注释内容很棘手,因为他要写出代码要做什么。注释变量是一个好主意。在函数头部注释可能不太明显时,就可以告诉阅读者函数要什么参数,有什么返回以及主要的意图。
通常大家会注释代码是做什么的,但这并不必要。如果代码让人困惑以至于你不得不写注释说它是做什么的,这就提示你应该重写它,使它更好懂。命名良好、更加短小、组织合理的代码习惯会让你的代码用不着注释就拥有很高的可读性。
坏习惯:压根没有或者叽叽歪歪的函数注释 (^_^)
例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");
?>
异常处理
写健壮应用时经常会提到的异常处理,一般遵循着80/20原则: 80%的代码用于处理异常或者验证,20%的代码没什么实际的用途。原始的代码通常都是在乐观的环境下编写的。这意味着代码可以在数据正常、一切理解的基 础环境中工作的很好。但是这种代码在其生命周期内是脆弱的。在极端的情形中,你得花更多的时间来未很可能永远不会发生的状况编写相应代码。
这个习惯就是要你处理全部的出错情况,而且如果要是不这么做,你的代码永远也完不成。
坏习惯:不处理任何异常
<?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"); }
?>
(责任编辑:admin) |