你遇到过的源代码中最好的注释是什么?


当前回答

曾经有一个为我工作的程序员在他的代码中加入了“Style”注释,他在代码中编写了关于特定实现细节的内部争论,当他在某个特定的编码决策上被否决时,他就会离开。

例子:

的风格 '这是有争议的,但我传递图像句柄,而不是简单地 '传递缩放值,以保持调用代码更简单(通过a '一对声明语句)。或者,我可以传递这些数据 成员,但这将违反封装。

的风格 就像我在其他地方做过的一样,我将正式提出抗议 的表格),将注释序列化的实现视为 '一个属性,而不是一对Load/Save方法。这可能是 “风格的问题,值得商榷。

其他回答

struct core_unlocker
{
    core_unlocker(lock)
    {
        m_lock = lock
        unlock(lock)  //Abandon All Locks, Ye Who Enter Core!
    }
    ~core_unlocker()
    {
        lock(m_lock)
    }  
    private:
    Corelock m_lock;
}

从《雷神之锤III》的资料中,我在一些随机的帖子中偶然发现了这一点。该文件的完整源代码可以在这里找到。这是一种非常快速的求平方根倒数的方法。至于最好的评论呢?当然,这是一种常见的方法,但考虑到它附着在直线上,它的神奇之处在于它的伟大之处。

float Q_rsqrt( float number )
{
  long i;
  float x2, y;
  const float threehalfs = 1.5F;

  x2 = number * 0.5F;
  y  = number;
  i  = * ( long * ) &y;  // evil floating point bit level hacking
  i  = 0x5f3759df - ( i >> 1 ); // what the fuck?
  y  = * ( float * ) &i;
  y  = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
  // y  = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed

  #ifndef Q3_VM
  #ifdef __linux__
    assert( !isnan(y) ); // bk010122 - FPE?
  #endif
  #endif
  return y;
}

为什么你不应该将你的软件开发外包的一个经典案例:

public class Contact
{
    //...    

    /// <summary>
    /// Gets or sets the name of the first.
    /// </summary>
    /// <value>The name of the first.</value>
    public string FirstName
    {
        get { return _firstName; }
        set { _firstName = value; }
    }
}

//我穿上长袍,戴上巫师帽……

// BEGIN HACK
...
// END HACK: I feel dirty.