根据具体的状况,通常的开发人员每每比优秀的开发人员的效率低 10%~20%。优秀的开发人员的效率更高,由于他们拥有丰富的经验和良好的编程习惯。不良的编程习惯将会影响到效率。本文经过展现一些良好的编程习惯,帮助您成为更优秀的程序员。php
这 些良好的编程习惯不只能提升效率,还能让您编写出在应用程序的整个生命周期中易于维护的代码。编写出来的代码可能须要大量的维护;应用程序的维护是一笔很 大的开支。养成良好的编程习惯可以提升设计质量(好比模块化),从而使代码更加容易理解,所以维护就更加容易,同时也下降维护成本。html
不良的编程习惯会形成代码缺陷,使其难以维护和修改,而且极可能在修改时又引入其余缺陷。如下是 5 个良好的编程习惯,可以帮助 PHP 代码避免这些缺陷:mysql
下一小节将详细介绍这些习惯。程序员
使用良好的命名是最重要的编程习惯,由于描述性强的名称让代码更加容易阅读和理解。代码是否好理解取决因而否能在将来维护它。即使代码不带有注释,若是它很容易理解,将大大方便往后的更改。这个习惯的目标是让您编写的代码像书本同样容易阅读和理解。sql
清单 1 中的代码包含太短的变量名、难以辨认的缩写词,而且方法名不能反映该方法的功能。若是方法名给人的感受是它应该作这件事情,而实际中它却作另外的事情,这将带来严重的问题,由于它会误导人。安全
<?php
function getNBDay($d)
{
switch($d) {
case 5:
case 6:
case 7:
return 1;
default:
return ($d + 1);
}
}
$day = 5;
$nextDay = getNBDay($day);
echo ("Next day is: " . $nextDay . "n");
?>
|
清单 2 中的代码体现了良好的编程习惯。新的方法名具备很强的说明性,反映了方法的用途。一样,更改后的变量名也更具说明性。唯一的保持最短的变量是 $i,在本清单中,它是一个循环变量。尽管不少人不赞同使用太短的名称,但在循环变量中使用仍是能够接受的(甚至有好处),由于它明确代表了代码的功能。
<?php
define ('MONDAY', 1);
define ('TUESDAY', 2);
define ('WEDNESDAY', 3);
define ('THURSDAY', 4);
define ('FRIDAY', 5);
define ('SATURDAY', 6);
define ('SUNDAY', 7);
/*
*
* @param $dayOfWeek
* @return int Day of week, with 1 being Monday and so on.
*/
function findNextBusinessDay($dayOfWeek)
{
$nextBusinessDay = $dayOfWeek;
switch($dayOfWeek) {
case FRIDAY:
case SATURDAY:
case SUNDAY:
$nextBusinessDay = MONDAY;
break;
default:
$nextBusinessDay += 1;
break;
}
return $nextBusinessDay;
}
$day = FRIDAY;
$nextBusDay = findNextBusinessDay($day);
echo ("Next day is:" . $nextBusDay . "n");
?>
|
咱们鼓励您将大的条件拆分为一个方法,而后用可以描述该条件的名字命名方法。这个技巧可以提升代码的可读性,而且可以将条件具体化,使之可以被提取甚至重用。若是条件发生变化,更新方法也很容易。由于方法拥有一个有意义的名字,因此它能反映代码的用途,让代码更容易阅读。
![]() ![]() |
![]() |
专心解决一个问题以后再继续编程,这样会让您更轻松。在解决一个紧急的问题时,若是继续编程,会使函数愈来愈长。从长远来讲,这并非一个问题,但您要记得回过头来将它重构为更小的部分。
重构是个不错的主意,但您应该养成编写更短、功能更集中的代码。短的方法可以在一个窗口中一次看完,而且容易理解。若是方法过长,不能在一个窗口中一次看完,那么它就变得不容易理解,由于您不能快速地从头至尾了解它的整个思路。
构建方法时,您应该养成这样的习惯,让每一个方法只完成一件事情。这个习惯很好,由于:首先,若是方法只完成一件事情,那么它就更容易被重用;其次,这样的方法容易测试;第三,这样的方法便于理解和更改。
清单 3 展现了一个很长的函数,其中存在不少问题。它完成不少件事情,所以不够紧凑。它也不便于阅读、调试和测试。它要作的事情包括遍历一个文件、构建一个列表、为每一个对象赋值、执行计算等等。
<?php
function writeRssFeed($user)
{
// Get the DB connection information
// look up the user's preferences...
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
OR die(mysql_error());
// Query
$perfsQuery = sprintf("SELECT max_stories FROM user_perfs WHERE user= '%s'",
mysql_real_escape_string($user));
$result = mysql_query($query, $link);
$max_stories = 25; // default it to 25;
if ($row = mysql_fetch_assoc($result)) {
$max_stories = $row['max_stories'];
}
// go get my data
$perfsQuery = sprintf("SELECT * FROM stories WHERE post_date = '%s'",
mysql_real_escape_string());
$result = mysql_query($query, $link);
$feed = "<rss version="2.0">" .
"<channel>" .
"<title>My Great Feed</title>" .
"<link>http://www.example.com/feed.xml</link>" .
"<description>The best feed in the world</description>" .
"<language>en-us</language>" .
"<pubDate>Tue, 20 Oct 2008 10:00:00 GMT</pubDate>" .
"<lastBuildDate>Tue, 20 Oct 2008 10:00:00 GMT</lastBuildDate>" .
"<docs>http://www.example.com/rss</docs>" .
"<generator>MyFeed Generator</generator>" .
"<managingEditor>editor@example.com</managingEditor>" .
"<webMaster>webmaster@example.com</webMaster>" .
"<ttl>5</ttl>";
// build the feed...
while ($row = mysql_fetch_assoc($result)) {
$title = $row['title'];
$link = $row['link'];
$description = $row['description'];
$date = $row['date'];
$guid = $row['guid'];
$feed .= "<item>";
$feed .= "<title>" . $title . "</title>";
$feed .= "<link>" . $link . "</link>";
$feed .= "<description> " . $description . "</description>";
$feed .= "<pubDate>" . $date . "</pubDate>";
$feed .= "<guid>" . $guid . "</guid>";
$feed .= "</item>";
}
$feed .= "</rss";
// write the feed out to the server...
echo($feed);
}
?>
|
若是多编写几个这样的方法,维护就成了真正的难题了。
清单 4 将原来的方法改写为更加紧凑、易读的方法。在这个示例中,将一个很长的方法分解为几个短方法,而且让每一个短方法负责一件事情。这样的代码对未来的重用和测试都是大有裨益的。
<?php
function createRssHeader()
{
return "<rss version="2.0">" .
"<channel>" .
"<title>My Great Feed</title>" .
"<link>http://www.example.com/feed.xml</link>" .
"<description>The best feed in the world</description>" .
"<language>en-us</language>" .
"<pubDate>Tue, 20 Oct 2008 10:00:00 GMT</pubDate>" .
"<lastBuildDate>Tue, 20 Oct 2008 10:00:00 GMT</lastBuildDate>" .
"<docs>http://www.example.com/rss</docs>" .
"<generator>MyFeed Generator</generator>" .
"<managingEditor>editor@example.com</managingEditor>" .
"<webMaster>webmaster@example.com</webMaster>" .
"<ttl>5</ttl>";
}
function createRssFooter()
{
return "</channel></rss>";
}
function createRssItem($title, $link, $desc, $date, $guid)
{
$item .= "<item>";
$item .= "<title>" . $title . "</title>";
$item .= "<link>" . $link . "</link>";
$item .= "<description> " . $description . "</description>";
$item .= "<pubDate>" . $date . "</pubDate>";
$item .= "<guid>" . $guid . "</guid>";
$item .= "</item>";
return $item;
}
function getUserMaxStories($db_link, $default)
{
$perfsQuery = sprintf("SELECT max_stories FROM user_perfs WHERE user= '%s'",
mysql_real_escape_string($user));
$result = mysql_query($perfsQuery, $db_link);
$max_stories = $default;
if ($row = mysql_fetch_assoc($result)) {
$max_stories = $row['max_stories'];
}
return $max_stories;
}
function writeRssFeed($user)
{
// Get the DB connection information
$settings = parse_ini_file("rss_server.ini");
// look up the user's preferences...
$link = mysql_connect($settings['db_host'], $settings['user'],
$settings['password']) OR die(mysql_error());
$max_stories = getUserMaxStories($link, 25);
// go get my data
$newsQuery = sprintf("SELECT * FROM stories WHERE post_date = '%s'",
mysql_real_escape_string(time()));
$result = mysql_query($newsQuery, $link);
$feed = createRssHeader();
$i = 0;
// build the feed...
while ($row = mysql_fetch_assoc($result)) {
if ($i < $max_stories) {
$title = $row['title'];
$link = $row['link'];
$description = $row['description'];
$date = $row['date'];
$guid = $row['guid'];
$feed .= createRssItem($title, $link, $description, $date, $guid);
$i++;
} else {
break;
}
}
mysql_close($link);
$feed .= createRssFooter();
// write the feed out to the server...
echo($feed);
}
?>
|
将长方法拆分为短方法也是有限制的,过分拆分将拔苗助长。所以,不要滥用这个良好的习惯。将代码分红大量的片断就像没有拆分长代码同样,都会形成阅读困难。
![]() ![]() |
![]() |
要为代码添加良好的注释有时彷佛和编写代码同样难。要了解应该为哪些内容添加注释并不容易,由于咱们经常倾向于注释代码当前作的事情。注释代码的目的是不错的主意。在函数的不是很明显的头部代码块中,告诉读者方法的输入和输出,以及方法的最初目标。
注释代码当前作什么是很常见的,但这是没必要要的。若是代码很复杂,不得不注释它当前在作什么,这将暗示您应该重写代码,让它更容易理解。学会使用良好的名称和更短的方法,在不提供注释说明其用途的状况下提升代码的可读性。
清单 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 中的注释告诉读者类和方法的目的。该注释解释了为何代码在作当前的工做,这对将来维护代码十分有用。可能须要根据条件变动而修改代码,若是可以轻松了解代码的目的,则修改起来很容易。
<?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% 的代码用于完成实际工做。在编写程序的基本逻辑(happy-path)代码 时常常这样作。这意味着编写适用于基本条件的代码,即全部的数据都是可用的,全部的条件符合预期。这样的代码在应用程序的生命周期中可能很脆弱。另外一个极端是,甚至须要花大量时间为从未遇到过的条件编写代码。
这一习惯要求您编写足够的错误处理代码,而不是编写对付全部错误的代码,以至代码迟迟不能完成。
清单 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 展现了以有意义的方式抛出和处理异常。额外的错误处理不只使代码更加健壮,它还提升代码的可读性,使代码更容易理解。处理异常的方式很好地说明了原做者在编写方法时的意图。
<?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");
}
?>
|
虽然检查参数是一种确认 — 若是您要求参数处于某种状态,这将对使用方法的人颇有帮助 — 可是您应该检查它们并抛出有意义的异常:
![]() ![]() |
![]() |
您能够从其余地方将代码复制粘贴到本身的代码编辑器,但这样作有利也有弊。好的一面是,从一个示例或模板中复制代码可以避免不少错误。很差的一面是,这容易带来大量的相似编程方式。
必定要注意,不要将代码从应用程序的一部分复制粘贴到另外一部分。若是您采用这种方式,请中止这个不良的习惯,而后考虑将这段代码重写为可重用的。通常而言,将代码放置到一个地方便于往后的维护,由于这样只需在一个地方更改代码。
清单 9 给出了几个几乎同样的方法,只是其中的值不一样而已。有一些工具能够帮助找到复制粘贴过来的代码(参见 参考资料)。
<?php
/**
* Counts the number of messages found in the array of
* ResultMessage with the getSeverity() value of "Error"
*
* @param $messages An array of ResultMessage
* @return unknown_type
*/
function countErrors($messages)
{
$matchingCount = 0;
foreach($messages as $m) {
if ($m->getSeverity() == "Error") {
$matchingCount++;
}
}
return $matchingCount;
}
/**
* Counts the number of messages found in the array of
* ResultMessage with the getSeverity() value of "Warning"
*
* @param $messages An array of ResultMessage
* @return unknown_type
*/
function countWarnings($messages)
{
$matchingCount = 0;
foreach($messages as $m) {
if ($m->getSeverity() == "Warning") {
$matchingCount++;
}
}
return $matchingCount;
}
/**
* Counts the number of messages found in the array of
* ResultMessage with the getSeverity() value of "Information"
*
* @param $messages An array of ResultMessage
* @return unknown_type
*/
function countInformation($messages)
{
$matchingCount = 0;
foreach($messages as $m) {
if ($m->getSeverity() == "Information") {
$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");
?>
|
清单 10 展现了修改后的代码,它将复制的代码放到一个方法中。另外一个方法也进行了更改,它如今将任务委托给新的方法。构建通用的方法须要花时间设计,而且这样作使您能停下来思考,而不是本能地使用复制粘贴。但有必要进行更改时,对通用的方法投入的时间将获得回报。
<?php
/*
* Counts the messages with the given severity in the array
* of messages.
*
* @param $messages An array of ResultMessage
* @return int Count of messages matching $withSeverity
*/
function countMessages($messages, $withSeverity)
{
$matchingCount = 0;
foreach($messages as $m) {
if ($m->getSeverity() == $withSeverity) {
$matchingCount++;
}
}
return $matchingCount;
}
/**
* Counts the number of messages found in the array of
* ResultMessage with the getSeverity() value of "Error"
*
* @param $messages An array of ResultMessage
* @return unknown_type
*/
function countErrors($messages)
{
return countMessages($messages, "Errors");
}
/**
* Counts the number of messages found in the array of
* ResultMessage with the getSeverity() value of "Warning"
*
* @param $messages An array of ResultMessage
* @return unknown_type
*/
function countWarnings($messages)
{
return countMessages($messages, "Warning");
}
/**
* Counts the number of messages found in the array of
* ResultMessage with the getSeverity() value of "Warning"
*
* @param $messages An array of ResultMessage
* @return unknown_type
*/
function countInformation($messages)
{
return countMessages($messages, "Information");
}
$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");
?>
|
![]() ![]() |
![]() |
若是您在编写 PHP 代码的过程当中养成本文讨论的良好习惯,您将可以构建易读、易理解、易维护的代码。使用这种方式构建的易维护代码将下降调试、修复和扩展代码所面临的风险。
使用良好的名称和更短的方法可以提升代码的可读性。注释代码的目的有利于代码理解和扩展。适当地处理错误会使代码更加健壮。最后,中止使用复制粘贴,保持代码干净,提升可重用性。