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

简介:

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

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

1.自以为很了不得的程序员


 
 
  1. public class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. string message = "Hello World!"; // 07/24/2010 Bob
  6. Console.WriteLine(message); // 07/24/2010 Bob
  7. message = "I am so proud of this code!"; // 07/24/2010 Bob
  8. Console.WriteLine(message); // 07/24/2010 Bob
  9. }
  10. }

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

2.过时的程序员


 
 
  1. public class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. /* This block of code is no longer needed
  6. * because we found out that Y2K was a hoax
  7. * and our systems did not roll over to 1/1/1900 */
  8. //DateTime today = DateTime.Today;
  9. //if (today == new DateTime(1900, 1, 1))
  10. //{
  11. // today = today.AddYears(100);
  12. // string message = "The date has been fixed for Y2K.";
  13. // Console.WriteLine(message);
  14. //}
  15. }
  16. }

如果一段代码已不再使用(即过时),那就删除它——不要浪费时间给这些代码写注释。此外,如果你需要复制这段被删除的代码,别忘了还有版本控制系统,你完全可以从早期的版本中恢复代码。

3.多此一举的程序员


 
 
  1. public class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. /* This is a for loop that prints the
  6. * words "I Rule!" to the console screen
  7. * 1 million times, each on its own line. It
  8. * accomplishes this by starting at 0 and
  9. * incrementing by 1. If the value of the
  10. * counter equals 1 million the for loop
  11. * stops executing.*/
  12. for (int i = 0; i < 1000000; i++)
  13. {
  14. Console.WriteLine("I Rule!");
  15. }
  16. }
  17. }

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

4.爱讲故事的程序员


 
 
  1. public class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. /* I discussed with Jim from Sales over coffee
  6. * at the Starbucks on main street one day and he
  7. * told me that Sales Reps receive commission
  8. * based upon the following structure.
  9. * Friday: 25%
  10. * Wednesday: 15%
  11. * All Other Days: 5%
  12. * Did I mention that I ordered the Caramel Latte with
  13. * a double shot of Espresso?
  14. */
  15. double price = 5.00;
  16. double commissionRate;
  17. double commission;
  18. if (DateTime.Today.DayOfWeek == DayOfWeek.Friday)
  19. {
  20. commissionRate = .25;
  21. }
  22. else if (DateTime.Today.DayOfWeek == DayOfWeek.Wednesday)
  23. {
  24. commissionRate = .15;
  25. }
  26. else
  27. {
  28. commissionRate = .05;
  29. }
  30. commission = price * commissionRate;
  31. }
  32. }

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

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


 
 
  1. public class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. //TODO: I need to fix this someday - 07/24/1995 Bob
  6. /* I know this error message is hard coded and
  7. * I am relying on a Contains function, but
  8. * someday I will make this code print a
  9. * meaningful error message and exit gracefully.
  10. * I just don't have the time right now.
  11. */
  12. string message = "An error has occurred";
  13. if(message.Contains("error"))
  14. {
  15. throw new Exception(message);
  16. }
  17. }
  18. }

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

如果你也常常犯这样的注释错误,如果你想了解注释的最佳做法,我建议你阅读类似于Steve McConnell写的《Code Complete》这样的好书。本文来自云栖

社区合作伙伴“Linux中国”,原文发布日期:2015-10-02   

目录
相关文章
|
10月前
|
存储 安全 iOS开发
内存卡怎么格式化?6个格式化方法供你选
随着使用时间的增加,内存卡可能会因为数据积累、兼容性或是文件系统损坏等原因需要进行格式化。那么怎样正确格式化内存卡呢?格式化内存卡的时候需要注意什么呢?本文会给大家提供详细的步骤,帮助大家轻松完成格式化内存卡的操作。
|
10月前
|
人工智能 搜索推荐 前端开发
MiniPerplx:基于 Grok 2.0 的开源 AI 搜索引擎,支持网页、学术、视频搜索
MiniPerplx 是一款基于 Grok 2.0 模型的开源 AI 搜索引擎,支持网页、学术论文、YouTube 视频等多种内容搜索,提供代码解释、天气预报等功能。
417 17
MiniPerplx:基于 Grok 2.0 的开源 AI 搜索引擎,支持网页、学术、视频搜索
|
存储 Kubernetes 虚拟化
计算机必背单词——云计算和虚拟化
本文介绍了程序员需要掌握的云计算和虚拟化相关技术词汇,包括AWS、Azure、GCP等云服务平台,Docker、Kubernetes等容器技术,以及IaaS、PaaS、SaaS等云服务模型。通过学习这些词汇,希望能拓宽读者的知识面并加深对相关概念的理解。
228 0
|
自动驾驶 物联网 5G
波束成形技术在5G中的关键作用
波束成形技术在5G中的关键作用
409 0
|
网络协议 网络架构
|
人工智能 自动驾驶 搜索推荐
人工智能的发展方向有哪些?
【5月更文挑战第17天】人工智能的发展方向有哪些?
754 3
|
数据采集 Prometheus 监控
prometheus的查询接口Instant queries 的缺点
Prometheus Instant queries 是一种实时的查询接口,它允许你在 Prometheus 中立即查询时间序列数据,而不需要像使用 PromQL 表达式一样定义监控规则。虽然 Instant queries 提供了方便的实时数据查询功能,但也有一些缺点需要考虑: 性能影响:Instant queries 可能会对 Prometheus 服务器的性能产生影响,特别是在处理大量的并发查询时。因为实时查询需要即时计算时间序列数据,可能会增加服务器的负载,导致性能下降。 资源消耗:Instant queries 需要消耗服务器的资源,包括 CPU 和内存等。如果频繁进行大量的实时
254 3
|
机器学习/深度学习
基于DSP的音频信号处理
基于DSP的音频信号处理
786 2
|
安全 开发工具 Android开发
说一说你对移动应用中的社交分享功能的实现。
移动应用中的社交分享功能增强互动性与传播力,提升用户体验。开发者通过集成社交媒体SDK(如微信SDK)实现分享,使用OAuth进行授权。定制分享内容样式,选择合适平台,如针对年轻人选择抖音、快手。统计分享数据评估效果,优化用户体验,遵守法规,全面测试并根据用户反馈持续优化。此功能对应用成功至关重要。
258 0
|
Python
python中删除含有缺失值的行
python中删除含有缺失值的行
765 2