【发布时间】:2018-03-26 22:04:53
【问题描述】:
在我看来,sample #2 似乎是更易读的评论方式。
但是如果我将 PSR-2 应用于两个样本,sample #1 不会改变,但 sample #2 的结果会发生如下变化,这不是正确的评论。
在这种情况下最好的评论方式是什么?
样品 #1/* Read cached data */
if ($useCache == true){
// do something
/* Download and cache data */
} else {
// do something
}
样品 #2
/* Read cached data */
if ($useCache == true){
// do something
}
/* Download and cache data */
else {
// do something
}
样品 #2 的 PSR-2 结果
/* Read cached data */
if ($useCache == true){
// do something
} /* Download and cache data */
else {
// do something
}
结论 2017/12/13
到目前为止,最好的方法似乎如下: 标记它们在括号内
if ($useCache == true){
/* Read cached data */
// do something
}
else {
/* Download and cache data */
// do something
}
【问题讨论】:
-
我认为评论会在 else 括号内。
-
在我看来,您应该尝试将变量和函数命名为足够好的名称,而不必使用 cmets。通常,当代码更新时,cmets 不会更新。例如,您是否记得在修复拼写错误并将代码从
$useCash更改为$useCache时更新 cmets? -
>
For instance would you remember to update the comments when you fix the spelling error and change the code from "$useCash" to "$useCache"?@rypskar Gee ...感谢您指出这一点!我已经修好了。正如您所提到的,变量和函数的命名确实足够好,因此不使用 cmets,但我必须在重构之前将前人留下的代码去意大利面,然后学习正确的方法。