douxia5179 2010-08-11 19:33
浏览 103
已采纳

PHP:评论代码

Good Day All,

Got theoretical question.

I'm creating a simple application using KohanaPHP framework, just fyi. This is my first time with framework and have one - maybe stupid for some of you - question.

While developing classes or functions I'm commeting my code using DocBlock. Just wondering how should I comment my code while using framework? I meant to code some parts of the code, not whole controllers.

Basically, I'm using following methods:

// Check if variable_name is greater than zero
if($this->var > 0) {
    // do something
} else {
    // do something
}

if( $result ) {
    // display confirmation message
} else {
    // display errors
}

Am I doing it right way? Is there any standard for inserting comments in the code?

Edit: Let me explain, I'm not using comments like "check if variable is greater than zero". I'm just wondering if is it good practice to put comments into the code.

Regards, Tom

  • 写回答

6条回答 默认 最新

  • dsvtnz6350 2010-08-11 19:52
    关注

    Comments are liars! The problem with comment is that you have to update them as you update your code. If you don't, you end up with code looking like this :

    // sum $a and $b
    $x = $a * $a - $b;
    

    So the best way to documente your code is to make it really clear! I would write your code like this :

    if ( isPositive(3) ) {
        doA();
    } else {
        doB();
    }
    
    if( $result ) {
        displayConfirmationMsg();
    } else {
        displayErrors();
    }
    

    This code doesn't need comments at all because it's very simple to understand it!

    Well, anyway, when I do have to write comments (almost never), I go with the // notation but I think it doesn't really matter.

    By the way, check out this awesome video of uncle bob http://bit.ly/AYqFT

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题