自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP編程的五個(gè)良好習(xí)慣

開(kāi)發(fā) 后端
像其他語(yǔ)言一樣,開(kāi)發(fā)人員可以用 PHP 編寫(xiě)出各種質(zhì)量級(jí)別的代碼。學(xué)習(xí)良好的編程習(xí)慣能夠提高代碼質(zhì)量和效率。

根據(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)秀的程序員。

這些良好的編程習(xí)慣不僅能提高效率,還能讓您編寫(xiě)出在應(yīng)用程序的整個(gè)生命周期中易于維護(hù)的代碼。編寫(xiě)出來(lái)的代碼可能需要大量的維護(hù);應(yīng)用程序的維護(hù)是一筆很 大的開(kāi)支。養(yǎng)成良好的編程習(xí)慣能夠提高設(shè)計(jì)質(zhì)量(比如模塊化),從而使代碼更加容易理解,因此維護(hù)就更加容易,同時(shí)也降低維護(hù)成本。

不良的編程習(xí)慣會(huì)造成代碼缺陷,使其難以維護(hù)和修改,并且很可能在修改時(shí)又引入其他缺陷。以下是 5 個(gè)良好的編程習(xí)慣,能夠幫助 PHP 代碼避免這些缺陷:

◆使用良好的命名。

◆分成更小的部分。

◆為代碼添加注釋。

◆處理錯(cuò)誤條件。

◆切忌使用復(fù)制粘貼。

下面將詳細(xì)介紹這些習(xí)慣:

使用良好的命名

使用良好的命名是最重要的編程習(xí)慣,因?yàn)槊枋鲂詮?qiáng)的名稱(chēng)讓代碼更加容易閱讀和理解。代碼是否好理解取決于是否能在未來(lái)維護(hù)它。即便代碼不帶有注釋?zhuān)绻苋菀桌斫?,將大大方便日后的更改。這個(gè)習(xí)慣的目標(biāo)是讓您編寫(xiě)的代碼像書(shū)本一樣容易閱讀和理解。

不良習(xí)慣:含糊的或無(wú)意義的名稱(chēng)

清單 1 中的代碼包含過(guò)短的變量名、難以辨認(rèn)的縮寫(xiě)詞,并且方法名不能反映該方法的功能。如果方法名給人的感覺(jué)是它應(yīng)該做這件事情,而實(shí)際中它卻做另外的事情,這將帶來(lái)嚴(yán)重的問(wèn)題,因?yàn)樗鼤?huì)誤導(dǎo)人。

清單 1. 不良習(xí)慣:含糊的或無(wú)意義的名稱(chēng)

<?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");

?>

良好習(xí)慣:說(shuō)明性強(qiáng)并且簡(jiǎn)潔的名稱(chēng)

清單 2 中的代碼體現(xiàn)了良好的編程習(xí)慣。新的方法名具有很強(qiáng)的說(shuō)明性,反映了方法的用途。同樣,更改后的變量名也更具說(shuō)明性。惟一的保持最短的變量是 $i,在本清單中,它是一個(gè)循環(huán)變量。盡管很多人不贊同使用過(guò)短的名稱(chēng),但在循環(huán)變量中使用還是可以接受的(甚至有好處),因?yàn)樗鞔_表明了代碼的功能。

清單 2. 良好習(xí)慣:說(shuō)明性強(qiáng)并且簡(jiǎn)潔的名稱(chēng)

<?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");
?>

我們鼓勵(lì)您將大的條件拆分為一個(gè)方法,然后用能夠描述該條件的名字命名方法。這個(gè)技巧能夠提高代碼的可讀性,并且能夠?qū)l件具體化,使之能夠被提取甚至重用。如果條件發(fā)生變化,更新方法也很容易。因?yàn)榉椒〒碛幸粋€(gè)有意義的名字,所以它能反映代碼的用途,讓代碼更容易閱讀。

#p#

分成更小的部分

專(zhuān)心解決一個(gè)問(wèn)題之后再繼續(xù)編程,這樣會(huì)讓您更輕松。在解決一個(gè)緊急的問(wèn)題時(shí),如果繼續(xù)編程,會(huì)使函數(shù)越來(lái)越長(zhǎng)。從長(zhǎng)遠(yuǎn)來(lái)說(shuō),這并不是一個(gè)問(wèn)題,但您要記得回過(guò)頭來(lái)將它重構(gòu)為更小的部分。

重構(gòu)是個(gè)不錯(cuò)的主意,但您應(yīng)該養(yǎng)成編寫(xiě)更短、功能更集中的代碼。短的方法能夠在一個(gè)窗口中一次看完,并且容易理解。如果方法過(guò)長(zhǎng),不能在一個(gè)窗口中一次看完,那么它就變得不容易理解,因?yàn)槟荒芸焖俚貜念^到尾了解它的整個(gè)思路。

構(gòu)建方法時(shí),您應(yīng)該養(yǎng)成這樣的習(xí)慣,讓每個(gè)方法只完成一件事情。這個(gè)習(xí)慣很好,因?yàn)椋菏紫?,如果方法只完成一件事情,那么它就更容易被重用;其次,這樣的方法容易測(cè)試;第三,這樣的方法便于理解和更改。

不良習(xí)慣:過(guò)長(zhǎng)的方法(完成很多件事情)

清單 3 展示了一個(gè)很長(zhǎng)的函數(shù),其中存在很多問(wèn)題。它完成很多件事情,因此不夠緊湊。它也不便于閱讀、調(diào)試和測(cè)試。它要做的事情包括遍歷一個(gè)文件、構(gòu)建一個(gè)列表、為每個(gè)對(duì)象賦值、執(zhí)行計(jì)算等等。

清單 3. 不良習(xí)慣:過(guò)長(zhǎng)的函數(shù)

<?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);

}

?>

如果多編寫(xiě)幾個(gè)這樣的方法,維護(hù)就成了真正的難題了。

良好習(xí)慣:易管理、功能專(zhuān)一的方法

清單 4 將原來(lái)的方法改寫(xiě)為更加緊湊、易讀的方法。在這個(gè)示例中,將一個(gè)很長(zhǎng)的方法分解為幾個(gè)短方法,并且讓每個(gè)短方法負(fù)責(zé)一件事情。這樣的代碼對(duì)將來(lái)的重用和測(cè)試都是大有裨益的。

清單 4. 良好習(xí)慣:易管理、功能專(zhuān)一的方法

				
<?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);
}
?>

將長(zhǎng)方法拆分為短方法也是有限制的,過(guò)度拆分將適得其反。因此,不要濫用這個(gè)良好的習(xí)慣。將代碼分成大量的片段就像沒(méi)有拆分長(zhǎng)代碼一樣,都會(huì)造成閱讀困難。

#p#

為代碼添加注釋

要為代碼添加良好的注釋有時(shí)似乎和編寫(xiě)代碼一樣難。要了解應(yīng)該為哪些內(nèi)容添加注釋并不容易,因?yàn)槲覀兂3A向于注釋代碼當(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");

?>

良好習(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#

處理錯(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");

?>

良好習(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");
}

?>

雖然檢查參數(shù)是一種確認(rèn) — 如果您要求參數(shù)處于某種狀態(tài),這將對(duì)使用方法的人很有幫助 — 但是您應(yīng)該檢查它們并拋出有意義的異常:

◆處理異常要盡量與出現(xiàn)的問(wèn)題緊密相關(guān)。

◆專(zhuān)門(mén)處理每個(gè)異常。

#p#

切忌使用復(fù)制粘貼

您可以從其他地方將代碼復(fù)制粘貼到自己的代碼編輯器,但這樣做有利也有弊。好的一面是,從一個(gè)示例或模板中復(fù)制代碼能夠避免很多錯(cuò)誤。不好的一面是,這容易帶來(lái)大量的類(lèi)似編程方式。

一定要注意,不要將代碼從應(yīng)用程序的一部分復(fù)制粘貼到另一部分。如果您采用這種方式,請(qǐng)停止這個(gè)不良的習(xí)慣,然后考慮將這段代碼重寫(xiě)為可重用的。一般而言,將代碼放置到一個(gè)地方便于日后的維護(hù),因?yàn)檫@樣只需在一個(gè)地方更改代碼。

不良習(xí)慣:類(lèi)似的代碼段

清單 9 給出了幾個(gè)幾乎一樣的方法,只是其中的值不同而已。有一些工具可以幫助找到復(fù)制粘貼過(guò)來(lái)的代碼(參見(jiàn) 參考資料)。

清單 9. 不良習(xí)慣:類(lèi)似的代碼段

<?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");
?>

良好習(xí)慣:帶參數(shù)的可重用函數(shù)

清單 10 展示了修改后的代碼,它將復(fù)制的代碼放到一個(gè)方法中。另一個(gè)方法也進(jìn)行了更改,它現(xiàn)在將任務(wù)委托給新的方法。構(gòu)建通用的方法需要花時(shí)間設(shè)計(jì),并且這樣做使您能停下來(lái)思考,而不是本能地使用復(fù)制粘貼。但有必要進(jìn)行更改時(shí),對(duì)通用的方法投入的時(shí)間將得到回報(bào)。

清單 10. 良好習(xí)慣:帶參數(shù)的可重用函數(shù)

<?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");

?>

結(jié)束語(yǔ)

如果您在編寫(xiě) PHP 代碼的過(guò)程中養(yǎng)成本文討論的良好習(xí)慣,您將能夠構(gòu)建易讀、易理解、易維護(hù)的代碼。使用這種方式構(gòu)建的易維護(hù)代碼將降低調(diào)試、修復(fù)和擴(kuò)展代碼所面臨的風(fēng)險(xiǎn)。
使用良好的名稱(chēng)和更短的方法能夠提高代碼的可讀性。注釋代碼的目的有利于代碼理解和擴(kuò)展。適當(dāng)?shù)靥幚礤e(cuò)誤會(huì)使代碼更加健壯。***,停止使用復(fù)制粘貼,保持代碼干凈,提高可重用性。

【編輯推薦】

  1. PHP面向?qū)ο缶幊痰?個(gè)好習(xí)慣
  2. PHP程序員五大兵器排行
  3. PHP網(wǎng)站提速三大“軟”招
  4. 利用PHP創(chuàng)建由Oracle驅(qū)動(dòng)的SOAP服務(wù)
  5. PHP中Session會(huì)話的使用和分析
責(zé)任編輯:楊鵬飛 來(lái)源: PHP5研究室
相關(guān)推薦

2009-01-03 14:34:49

ibmdwPHP

2011-07-07 15:26:28

PHP編程習(xí)慣

2011-07-07 15:48:22

PHP編程習(xí)慣

2011-07-07 15:36:51

PHP

2010-04-08 11:17:06

Unix操作系統(tǒng)

2011-07-14 22:04:16

VC++

2022-04-08 14:38:43

程序員習(xí)慣終端

2010-06-11 14:35:18

UML序列圖

2011-03-29 12:41:49

編程

2011-04-13 10:16:41

編程習(xí)慣

2024-08-20 14:19:29

2020-04-22 10:35:07

編程學(xué)習(xí)技術(shù)

2011-07-15 15:10:37

PHP

2011-03-24 09:25:54

程序員編程

2022-10-08 10:42:20

Linux虛擬機(jī)

2024-05-23 12:09:01

2020-11-02 13:03:28

MySQLSQL索引

2024-02-26 08:13:51

MySQLSQL性能

2021-08-17 09:55:50

pandas 8indexPython

2009-02-12 09:44:48

Web應(yīng)用高性能習(xí)慣
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)