我最近开始做一个小型的CMS。我通常用C#开发.NET应用程序,并且我非常习惯于注释我的字段和方法。我的朋友早些时候告诉我,在PHP脚本中有注释是非常糟糕的,因为PHP是动态的,在请求时会被解析,所以解析注释会花费更长的时间。
这句话适用于我的情况吗?我的情况是注释每个方法和字段?
我的一个类的示例:
<?php
/* 
 *     jWeb
 *     Copyright (C) 2010 AJ Ravindiran
 * 
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * Controls database connections.
 *
 * @author AJ Ravindiran
 * @version 1.0.0 Jan-02-2010
 */
class database {
    /**
     * The database connection ip address.
     * @var string
     */
    private $host = "";
    /**
     * The database user's name.
     * @var string
     */
    private $username = "";
    /**
     * The database user's password.
     * @var string
     */
    private $password = "";
    /**
     * The database that this instance will write to, and read from.
     * @var string
     */
    private $database = "";
    /**
     * Holds the mysql connection instance.
     * @var resource
     */
    private $connection = null;
    /**
     * Whether the instance is connected to the specified database.
     * @var bool
     */
    private $connected = false;
    /**
     * Constructs a new database instance.
     * @param string $host The database server host.
     * @param string $port The database server port.
     * @param string $username The database's username authentication.
     * @param string $password The username's specified password.
     */
    public function __construct($host = "localhost", $username = "root", $password = "") {
        $this->host = $host;
        $this->username = $username;
        $this->password = $password;
    }
    /**
     * Connects to the given database.
     * @param string $database
     */
    public function connect($database) {
        $this->database = $database;
        // TODO: handle errors.
        $this->connection = @mysql_connect($this->host, $this->username, $this->password) or die();
        @mysql_select_db($this->database, $this->connection) or die();
        /*
         * If the connection was successful, we can now
         * identify that the connection is sustained.
         */
        if ($this->connect != null) {
            $this->connected = true;
        }
    }
    /**
     * Gets the specified connection details from this instance.
     * @param boolean $show_details
     * @return string The connection string.
     */
    public function getConnectionString($show_details = false) {
        if ($show_details) {
            return "database[host=" . $this->host 
                    . ", port=" . $this->port
                    . ", user=" . $this->username
                    . ", pass=" . $this->password
                    . ", database=" . $this->database . "]";
        } else {
            return "database[host=" . $this->host
                    . ", port=" . $this->port . "]";
        }
    }
}
?>发布于 2010-01-03 08:57:46
这里的其他评论者在性能方面是完全正确的。在代码中注释时,性能不应该是一个考虑因素。
但是,为了评论您的具体示例,我认为您的类中确实有太多的注释。例如,看一下这个字段:
/**
 * The database user's name.
 * @var string
 */
private $username = "";那里有很多“视觉噪音”,而且评论也不能真正解释任何事情。你有4行评论,并没有告诉读者任何有趣的细节。如果你想在那里添加注释,它应该解释代码中一些有趣的东西,而不仅仅是重复代码所做的事情。例如:
/**
 * The database user's name. This field has to be 5 to 10 characters long. It
 * is not required if the connection security is disabled.
 * @var string
 */
private $username = "";要选择另一个示例,请查看此注释:
/** 
 * Gets the specified connection details from this instance. 
 * @param boolean $show_details 
 * @return string The connection string. 
 */
public function getConnectionString($show_details = false) {
    ...这个注释是一个很好的开始,但是它遗漏了一些关键信息:show_details参数到底做了什么?如果未启用,将缺少哪些详细信息?或者,当它被启用时,将包括什么?
评论从根本上来说并不是坏事,更多的评论并不总是比更少的评论更糟糕。有正确的评论是很重要的!
发布于 2010-01-03 08:57:05
你的朋友说的是傻话。
PHP是动态的,必须按请求解析脚本,但解析注释所需的时间可以忽略不计(因为一旦遇到注释,它就会跳转到下一个相关行;它的开销可能只比空格略多一点),并且注释对您自己和系统未来的维护者的价值远远大于任何潜在的性能影响。
请随意发表评论。
不过,您可以通过使用诸如APC或eCache之类的操作码缓存机制来大大提高PHP的速度。把你的精力和时间投入到像这样的实际解决方案上,而不是像省略评论这样的愚蠢之举。
发布于 2010-01-03 08:55:10
这是胡扯,评论的数量与实际处理时间无关或几乎没有关系,因为评论不会被解析。
你的代码中的注释越有意义,对下一个维护它的人来说就越好。
代码示例中提供的注释样式是典型的,因为您在需要注释的地方使用注释,并且使用phpdoc语法,这将使文档的创建变得轻而易举。
有些人可能会批评每个类变量的注释,但在我看来,这就是使用phpdoc语法的意义所在,每个有意义的变量、方法和类都有一个解释。
https://stackoverflow.com/questions/1993534
复制相似问题