简体   繁体   English

在PHP5中评论的有效和可读方法是什么?

[英]What are The Valid & Readable approaches to Commenting in PHP5?

In the past 2 months that I have been learning PHP, I have identified more than two styles people use to comment code! 在过去的两个月里,我一直在学习PHP,我发现有两种以上的样式用来评论代码! I haven't seen much consistency... which I think usually means artists at work . 我没有看到太多的一致性......我认为这通常意味着工作中的艺术家 So I wondered: what are the valid ways to comment which are still readable/practical? 所以我想知道:评论的有效方法是什么仍然可读/实用? Seeing all the valid possibilities in 1 place side by side will provide the overview that I am looking for to improve commenting 并排查看1个地方的所有有效可能性将提供我正在寻求改进评论的概述

/*
|  This is what I now use (5chars/3lines) I name it star*wars
\*

Quoting the Manual on Comments: 引用评论手册:

PHP supports 'C', 'C++' and Unix shell-style (Perl style) comments. PHP支持'C','C ++'和Unix shell风格(Perl风格)注释。 For example: 例如:

<?php
    echo 'This is a test'; // This is a one-line c++ style comment
    /* This is a multi line comment
       yet another line of comment */
    echo 'This is yet another test';
    echo 'One Final Test'; # This is a one-line shell-style comment
?>

In general, you will want to avoid using comments in your sourcecode . 通常,您需要避免在源代码中使用注释 To quote Martin Fowler: 引用Martin Fowler的话:

When you feel the need to write a comment, first try to refactor the code so that any comment becomes superfluous. 当您觉得需要编写注释时,首先尝试重构代码,以便任何注释都变得多余。

which means something like 这意味着什么

// check if date is in Summer period
if ($date->after(DATE::SUMMER_START) && $date->before(DATE::SUMMER_END)) {

should be rewritten to 应该改写成

if ($date->isInSummerPeriod()) { …

Another comment type you will sometimes encounter is the separator comment, eg something like 您有时会遇到的另一种评论类型是分隔符注释,例如

// --------------------------------------------

or 要么

################################################

Those are usually indicative that the code they are used in is doing too much. 这些通常表明他们使用的代码做得太多了。 If you find this in a class, check the responsibility of the class and see if some parts of it are better refactored into a standalone class. 如果你在一个类中找到它,检查类的职责,看看它的某些部分是否更好地重构为一个独立的类。

As for API docs, the common notation is PHPDoc , eg 至于API文档,常见的表示法是PHPDoc ,例如

/**
 * Short Desc
 *
 * Long Desc
 * 
 * @param  type $name description
 * @return type       description
 */
 public function methodName($name) { …

I would argue that you can omit Short and Long Desc if the remaining method signature clearly communicates what it does. 我认为如果剩下的方法签名清楚地传达它的作用,你可以省略短篇和长篇。 However, that requires a certain discipline and knowledge in how to actually write Clean Code . 但是,这需要一定的纪律和知识,如何实际编写清洁代码 For instance, the following is totally superfluous: 例如,以下内容完全是多余的:

/**
 * Get the timestamp property
 *
 * The method returns the {@link $timestamp} property as an integer.
 * 
 * @return integer the timestamp
 */
 public function getTimestamp() { …

and should be shortened to 并应缩短为

/**
 * @return integer
 */
 public function getTimestamp() { …

Needless to say, whether you go for full API docs or not also depends on the project. 毋庸置疑,无论您是否使用完整的API文档,还取决于项目。 I'd expect any framework I can download and use to have full API docs. 我希望我可以下载并使用任何框架来获得完整的API文档。 The important thing is just that whatever you decide to do, do it consistently. 重要的是,无论你决定做什么,都要始终如一地做到。

You should definitely use the phpdoc standards. 你绝对应该使用phpdoc标准。 Here's a quick start for beginners. 这是初学者的快速入门。

I'm sure you've seen comments like this: 我相信你看过这样的评论:

/**
 * example of basic @param usage
 * @param bool $baz 
 * @return mixed 
 */
function function1($baz)
{
   if ($baz)
   {
      $a = 5;
   } else
   {
      $a = array(1,4);
   }
   return $a;
}

Commenting this way makes it not only easy for most PHP-developers to read, but you can also generate nice documentations. 以这种方式进行评论使得大多数PHP开发人员不仅可以轻松阅读,而且还可以生成精美的文档。

To me every one of them looks equally readable. 对我来说,每个人看起来都同样可读。
I am using both one-liners and multi-line comments as well. 我也使用单行和多行注释。

Being highlighted in gray, they are always visible and distinct from other code. 以灰色突出显示,它们始终可见并且与其他代码不同。
I've seen not a single problem with comments readability before 我之前看过的评论可读性没有一个问题

It's quite common to use phpdoc guidelines for commenting. 使用phpdoc 指南进行评论是很常见的。 This includes annotations for generating a documentation. 这包括用于生成文档的注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM