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


当前回答

在调试别人的JavaScript时,我看到了下面的评论:

// Notice: I feel so dirty doing this, but it's the only way to make it cross browser.

但在阅读Scott Hanselmen的一篇文章时,我看到了下面的引用,它与我在代码中发现的注释非常吻合:

Every line of code you write that you feel gross about will ultimately come back to haunt you. Therefore, avoid writing code that makes you feel dirty.

这很有趣。

其他回答

//Code sanitized to protect the foolish. using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.Web.UI; namespace Mobile.Web.Control { /// <summary> /// Class used to work around Richard being a fucking idiot /// </summary> /// <remarks> /// The point of this is to work around his poor design so that paging will /// work on a mobile control. The main problem is the BindCompany() method, /// which he hoped would be able to do everything. I hope he dies. /// </remarks> public abstract class RichardIsAFuckingIdiotControl : MobileBaseControl, ICompanyProfileControl { protected abstract Pager Pager { get; } public void BindCompany(int companyId) { } public RichardIsAFuckingIdiotControl() { MakeSureNobodyAccidentallyGetsBittenByRichardsStupidity(); } private void MakeSureNobodyAccidentallyGetsBittenByRichardsStupidity() { // Make sure nobody is actually using that fucking bindcompany method MethodInfo m = this.GetType().GetMethod("BindCompany", BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (m != null) { throw new RichardIsAFuckingIdiotException("No!! Don't use the fucking BindCompany method!!!"); } // P.S. this method is a joke ... the rest of the class is fucking serious } /// <summary> /// This returns true if this control is supposed to be doing anything /// at all for this request. Richard thought it was a good idea to load /// the entire website during every request and have things turn themselves /// off. He also thought bandanas and aviator sunglasses were "fuckin' /// gnarly, dude." /// </summary> protected bool IsThisTheRightPageImNotSureBecauseRichardIsDumb() { return Request.QueryString["Section"] == this.MenuItemKey; } protected override void OnLoad(EventArgs e) { if (IsThisTheRightPageImNotSureBecauseRichardIsDumb()) { Page.LoadComplete += new EventHandler(Page_LoadComplete); Pager.RowCount = GetRowCountBecauseRichardIsDumb(); } base.OnLoad(e); } protected abstract int GetRowCountBecauseRichardIsDumb(); protected abstract void BindDataBecauseRichardIsDumb(); void Page_LoadComplete(object sender, EventArgs e) { BindDataBecauseRichardIsDumb(); } // the rest of his reduh-ndant interface members public abstract string MenuItemName { get; set; } public abstract string MenuItemKey { get; set; } public abstract bool IsCapable(CapabilityCheck checker, int companyId); public abstract bool ShowInMenu { get; } public virtual Control CreateHeaderControl() { return null; } } }

更新:代码的原作者已经暴露了自己,所以我必须在适当的地方给予赞扬。Dan McKinley在我入职后不久就离开了我所在的公司,他讲述了更多关于代码的内容,解释了一些背景知识和“Richard”所写的一些“WTF”。

不是注释,而是属性

[ThereBeDragons]

还有一个是我在IHttpHandler的实现中看到的

//What is this?
public bool IsReusable
{
    get{return false;}
}

当被问及什么是“最好的评论”时,我们的回答都是我们能找到的最糟糕的评论,这充分说明了我们的职业……

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

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; }
    }
}

下面是一个必须声明的局部变量,只是为了将一个常量传递给库函数:

// This only exists because Scott doesn't know how to use const correctly