关闭 x
IT技术网
    技 采 号
    ITJS.cn - 技术改变世界
    • 实用工具
    • 菜鸟教程
    IT采购网 中国存储网 科技号 CIO智库

    IT技术网

    IT采购网
    • 首页
    • 行业资讯
    • 系统运维
      • 操作系统
        • Windows
        • Linux
        • Mac OS
      • 数据库
        • MySQL
        • Oracle
        • SQL Server
      • 网站建设
    • 人工智能
    • 半导体芯片
    • 笔记本电脑
    • 智能手机
    • 智能汽车
    • 编程语言
    IT技术网 - ITJS.CN
    首页 » 程序人生 »程序员应该避免的5种代码注释

    程序员应该避免的5种代码注释

    2015-08-20 00:00:00 出处:foruok
    分享

    你有没有这样的经历:别人审查过你的代码之后给出的注释,你认为是没有必要的?注释代码是为了提高代码的可读性,目的是为了能让其他人更容易理解你的代码。

    我特别讨厌这5种注释类型以及制造它们的程序员。希望你不是其中之一。

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

    这个程序员自认为写了一段很了不得的代码,所以觉得有必要用自己的名字对每行代码进行标记。实施版本控制系统(VCS)能实现对代码变更的问责,但是也不会这么明显知道谁应对此负责。

    2.过时的程序员

    public class Program
    {
        static void Main(string[] args)
        {
            /* This block of code is no longer needed
             * because we found out that Y2K was a hoax
             * and our systems did not roll over to 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)
        {
            /* This is a for loop that prints the 
             * words "I Rule!" to the console screen 
             * 1 million times, each on its own line. It
             * accomplishes this by starting at 0 and 
             * incrementing by 1. If the value of the 
             * counter equals 1 million the for loop
             * stops executing.*/
            for (int i = 0; i < 1000000; i++)
            {
                Console.WriteLine("I Rule!");
            }
        }
    }

    我们都知道基础的编程逻辑是如何工作的——所以你不需要多此一举来解释这些显而易见的工作原理,虽然说你解释得很happy,但这只是在浪费时间和空间。

    4.爱讲故事的程序员

    public class Program
    {
        static void Main(string[] args)
        {
           /* I discussed with Jim from Sales over coffee 
            * at the Starbucks on main street one day and he
            * told me that Sales Reps receive commission 
            * based upon the following structure. 
            * Friday: 25%
            * Wednesday: 15%
            * All Other Days: 5%
            * Did I mention that I ordered the Caramel Latte with
            * a double shot of 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;
        }
    }

    要是你一定要在注释里提及需求,那么不要涉及别人的名字。销售部门的Jim可能会离开公司,而且很有可能大多数程序员根本不知道这是何许人也。不要在注释里提及不相干的事实。

    5.“以后再做”的程序员

    public class Program
    {
        static void Main(string[] args)
        {
           //TODO: I need to fix this someday - 07/24/1995 Bob
           /* I know this error message is hard coded and
            * I am relying on a Contains function, but 
            * someday I will make this code print a 
            * meaningful error message and exit gracefully.
            * I just don't have the time right now.
           */
           string message = "An error has occurred";
           if(message.Contains("error"))
           {
               throw new Exception(message);
           }
        }
    }

    这种类型的注释包含了上面所有其他类型。要是是在项目的初始开发阶段,这种待做注释是非常有用的,但要是是在几年后的产品代码——那就会出问题了。要是有什么需要修复的,立马解决,不要把它搁置一边,“以后再做”。

    要是你也常常犯这样的注释错误,要是你想了解注释的最佳做法,我建议你阅读类似于Steve McConnell写的《Code Complete》这样的好书。

    另外笔者也推荐你阅读以下内容:《9个最有趣的代码注释》、《代码注释中的5要与3不要》

    上一篇返回首页 下一篇

    声明: 此文观点不代表本站立场;转载务必保留本文链接;版权疑问请联系我们。

    别人在看

    我国成功实现磷化铟半导体激光器的异质集成

    存储如何推动澳大利亚的人工智能雄心

    优化数据密集型技术和应用的 TCO(总成本)

    Hyland 为企业开源 AI 就绪云存储库

    RealMan在北京启动机器人数据培训中心

    StorONE 和 Viking 企业解决方案统一存储以颠覆遗留系统

    《人工智能生成合成内容标识办法》2025年9月1日起施行

    美满科技股价暴跌,数据中心疲软加剧AI芯片担忧

    机器人投资在 2025 年 7 月突破43亿美元

    日本电产重返RoboBusiness,展示先进的机器人设备

    IT头条

    Synology 对 Office 套件进行重大 AI 更新,增强私有云的生产力和安全性

    01:43

    StorONE 的高效平台将 Storage Guardian 数据中心占用空间减少 80%

    11:03

    年赚千亿的印度能源巨头Nayara 云服务瘫痪,被微软卡了一下脖子

    12:54

    国产6nm GPU新突破!砺算科技官宣:自研TrueGPU架构7月26日发布

    01:57

    公安部:我国在售汽车搭载的“智驾”系统都不具备“自动驾驶”功能

    02:03

    技术分享

    如何删除自带的不常用应用为windows 7减负

    MySQL中多表删除方法

    改进的二值图像像素标记算法及程序实现

    windows 7 32位系统下手动修改磁盘属性例如M盘修改为F盘

    windows 7中怎么样在家庭组互传文件

    Linux应用集成MySQL数据库访问技巧

      友情链接:
    • IT采购网
    • 科技号
    • 中国存储网
    • 存储网
    • 半导体联盟
    • 医疗软件网
    • 软件中国
    • ITbrand
    • 采购中国
    • CIO智库
    • 考研题库
    • 法务网
    • AI工具网
    • 电子芯片网
    • 安全库
    • 隐私保护
    • 版权申明
    • 联系我们
    IT技术网 版权所有 © 2020-2025,京ICP备14047533号-20,Power by OK设计网

    在上方输入关键词后,回车键 开始搜索。Esc键 取消该搜索窗口。