不要这样注释你的代码

释不能毫无用处,程序注释就是为了提高代码的可读性,为了让原作者以外的其他开发人员更容易理解这段程序,本文所列举的注释一定要避免~

1.自傲

public class Program
{
    static void Main(string[] args)
    {
        string message = Hello World!”;            // 07/24/2010 Bob
        Console.WriteLine(message);                   // 07/24/2010 Bob
        message = I am so proud of this code!”;    // 07/24/2010 Bob
        Console.WriteLine(message);                  // 07/24/2010 Bob
    }
}

2.过时

public class Program
{
    static void Main(string[] args)
    {
        /* 这段程序已经不再有用
         * 因为我们发现千年虫问题只是一场虚惊
         * 我们的系统不会恢复到1/1/1900 
         */
        //DateTime today = DateTime.Today;
        //if (today == new DateTime(1900, 1, 1))
        //{
        //    today = today.AddYears(100);
        //    string message = “The date has been fixed for Y2K.”;
        //    Console.WriteLine(message);
        //}
    }
}

3.太显而易见

public class Program
{
    static void Main(string[] args)
    {
        /* 这个程序是用来在屏幕上
         * 循环打印1百万次”I Rule!”
         * 每次输出一行。循环计数
         * 从0开始,每次加1。
         * 当计数器等于1百万时,
         * 循环就会停止运行
        */ 
        for (int i = 0; i < 1000000; i++)
        {
            Console.WriteLine(“Rule!”);
        }
    }
}

4. 与内容无关

public class Program
{
    static void Main(string[] args)
    {
       /* 有一天我在大街上的一家星巴克里
        * 和销售部的Jim讨论问题,他告诉我
        * 销售代表是依据以下的比例提取佣金的。
        * 周五: 25%
        * 周三: 15%
        * 其它日期: 5%
        * 我是否告诉你过我点了一个卡拉梅
        * 铁咖啡和两份的Espresso?
       */
        double price = 5.00;
        double commissionRate;
        double commission;
        if (DateTime.Today.DayOfWeek == DayOfWeek.Friday)
        {
            commissionRate = .25;
        }
        else if (DateTime.Today.DayOfWeek == DayOfWeek.Wednesday)
        {
            commissionRate = .15;
        }
        else
        {
            commissionRate = .05;
        }
        commission = price * commissionRate;
    }
}

5. 拖到日后

public class Program
{
    static void Main(string[] args)
    {
       //TODO: 将来我会修复这个问题 – 07/24/1995 Bob
       /* 我知道这个问题很难解决而且
        * 我现在依赖于这个Contains函数,但
        * 我以后会用一种更有意义,更
        * 优雅的方式打印这段代码。
        * 我只是现在没时间。
       */
       string message = An error has occurred”;
       if(message.Contains(“error”))
       {
           throw new Exception(message);
       }
    }
}

原文:http://repeatgeek.com/career/5-types-of-comments-to-avoid-making-in-your-code/

posted @ 2014-11-11 13:43  甜甜酱  阅读(200)  评论(3编辑  收藏  举报