• 技术文章 >后端开发 >php教程

    PHP 编程的 5个良好习惯_php技巧

    2016-05-17 09:33:42原创269
    根据具体的情况,一般的开发人员往往比优秀的开发人员的效率低 10%~20%。优秀的开发人员的效率更高,因为他们拥有丰富的经验和良好的编程习惯。不良的编程习惯将会影响到效率。本文通过展示一些良好的编程习惯,帮助您成为更优秀的程序员。

    这些良好的编程习惯不仅能提高效率,还能让您编写出在应用程序的整个生命周期中易于维护的代码。编写出来的代码可能需要大量的维护;应用程序的维护是一笔很大的开支。养成良好的编程习惯能够提高设计质量(比如模块化),从而使代码更加容易理解,因此维护就更加容易,同时也降低维护成本。

    不良的编程习惯会造成代码缺陷,使其难以维护和修改,并且很可能在修改时又引入其他缺陷。以下是 5 个良好的编程习惯,能够帮助 PHP 代码避免这些缺陷:

    1. 使用良好的命名。
    2. 分成更小的部分。
    3. 为代码添加注释。
    4. 处理错误条件。
    5. 切忌使用复制粘贴。

    下一小节将详细介绍这些习惯。

    使用良好的命名

    使用良好的命名是最重要的编程习惯,因为描述性强的名称让代码更加容易阅读和理解。代码是否好理解取决于是否能在未来维护它。即便代码不带有注释,如果它很容易理解,将大大方便日后的更改。这个习惯的目标是让您编写的代码像书本一样容易阅读和理解。

    不良习惯:含糊的或无意义的名称

    清单 1 中的代码包含过短的变量名、难以辨认的缩写词,并且方法名不能反映该方法的功能。如果方法名给人的感觉是它应该做这件事情,而实际中它却做另外的事情,这将带来严重的问题,因为它会误导人。


    清单 1. 不良习惯:含糊的或无意义的名称



    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,在本清单中,它是一个循环变量。尽管很多人不赞同使用过短的名称,但在循环变量中使用还是可以接受的(甚至有好处),因为它明确表明了代码的功能。


    清单 2. 良好习惯:说明性强并且简洁的名称



    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 展示了一个很长的函数,其中存在很多问题。它完成很多件事情,因此不够紧凑。它也不便于阅读、调试和测试。它要做的事情包括遍历一个文件、构建一个列表、为每个对象赋值、执行计算等等。


    清单 3. 不良习惯:过长的函数



    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 = "" .
    "" .
    "My Great Feed" .
    "http://www.example.com/feed.xml" .
    "The best feed in the world" .
    "en-us" .
    "Tue, 20 Oct 2008 10:00:00 GMT" .
    "Tue, 20 Oct 2008 10:00:00 GMT" .
    "http://www.example.com/rss" .
    "MyFeed Generator" .
    "editor@example.com" .
    "webmaster@example.com" .
    "5";

    // 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 .= "";
    $feed .= "" . $title . "";
    $feed .= "" . $link . "";
    $feed .= " " . $description . "";
    $feed .= "" . $date . "";
    $feed .= "" . $guid . "";
    $feed .= "
    ";
    }

    $feed .= "
    // write the feed out to the server...
    echo($feed);

    }

    ?>

    如果多编写几个这样的方法,维护就成了真正的难题了。

    良好习惯:易管理、功能专一的方法

    清单 4 将原来的方法改写为更加紧凑、易读的方法。在这个示例中,将一个很长的方法分解为几个短方法,并且让每个短方法负责一件事情。这样的代码对将来的重用和测试都是大有裨益的。


    清单 4. 良好习惯:易管理、功能专一的方法



    function createRssHeader()
    {
    return "" .
    "" .
    "My Great Feed" .
    "http://www.example.com/feed.xml" .
    "The best feed in the world" .
    "en-us" .
    "Tue, 20 Oct 2008 10:00:00 GMT" .
    "Tue, 20 Oct 2008 10:00:00 GMT" .
    "http://www.example.com/rss" .
    "MyFeed Generator" .
    "editor@example.com" .
    "webmaster@example.com" .
    "5";
    }

    function createRssFooter()
    {
    return "
    ";
    }

    function createRssItem($title, $link, $desc, $date, $guid)
    {
    $item .= "";
    $item .= "" . $title . "";
    $item .= "" . $link . "";
    $item .= " " . $description . "";
    $item .= "" . $date . "";
    $item .= "" . $guid . "";
    $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 中的注释仅告诉读者代码在做什么 — 它正在通过一个循环进行迭代或添加一个数字。但它忽略了它为什么做当前的工作。这使维护该代码的人员不知道是否可以安全地更改代码(不引入新缺陷)。


    清单 5. 不良习惯:函数注释过多或不足



    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. 良好习惯:带注释的函数和类


    /**
    * 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 中的代码演示了两个不良习惯。第一,没有检查输入的参数,即使知道处于某些状态的参数会造成方法出现异常。第二,代码调用一个可能抛出异常的方法,但没有处理该异常。当发生问题时,代码的作者或维护该代码的人员只能猜测问题的根源。


    清单 7. 不良习惯:不处理错误条件



    // 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. 良好习惯:处理异常



    /**
    * 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 给出了几个几乎一样的方法,只是其中的值不同而已。有一些工具可以帮助找到复制粘贴过来的代码(参见参考资料)。


    清单 9. 不良习惯:类似的代码段


    /**
    * 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 展示了修改后的代码,它将复制的代码放到一个方法中。另一个方法也进行了更改,它现在将任务委托给新的方法。构建通用的方法需要花时间设计,并且这样做使您能停下来思考,而不是本能地使用复制粘贴。但有必要进行更改时,对通用的方法投入的时间将得到回报。


    清单 10. 良好习惯:带参数的可重用函数


    /*
    * 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 代码的过程中养成本文讨论的良好习惯,您将能够构建易读、易理解、易维护的代码。使用这种方式构建的易维护代码将降低调试、修复和扩展代码所面临的风险。

    使用良好的名称和更短的方法能够提高代码的可读性。注释代码的目的有利于代码理解和扩展。适当地处理错误会使代码更加健壮。最后,停止使用复制粘贴,保持代码干净,提高可重用性。

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:PHP 编程
    上一篇:用mysql内存表来代替php session的类_php技巧 下一篇:PHP 加密/解密函数 dencrypt(动态密文,带压缩功能,支持中文)_php技巧
    Web大前端开发直播班

    相关文章推荐

    • 详细解析PHP文件包含• php实现文件下载更能介绍_PHP教程• PHP常用技巧总结(附函数代码)_PHP教程• php常用表单验证类用法实例_PHP• Apache实现Web Server负载均衡详解(不考虑Session版)_PHP
    新人一分购

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网