Comment Only What the Code Cannot Say

Comment Only What the Code Cannot Say

Kevlin Henney

THE DiFFERENCE BETWEEN THEORY AND PRACTiCE is greater in practice than it is in theory—an observation that certainly applies to comments. In theory, the general idea of commenting code sounds like a worthy one: offer the reader detail, an explanation of what’s going on. What could be more help- ful than being helpful? In practice, however, comments often become a blight. As with any other form of writing, there is a skill to writing good comments. Much of the skill is in knowing when not to write them.

When code is ill-formed, compilers, interpreters, and other tools will be sure to object. If the code is in some way functionally incorrect, reviews, static analysis, tests, and day-to-day use in a production environment will flush most bugs out. But what about comments? In The Elements of Programming Style (Computing McGraw-Hill), Kernighan and Plauger note that “a com- ment is of zero (or negative) value if it is wrong.” And yet such comments often litter and survive in a codebase in a way that coding errors never could. They provide a constant source of distraction and misinformation, a subtle but constant drag on a programmer’s thinking.

What of comments that are not technically wrong, but add no value to the code? Such comments are noise. Comments that parrot the code offer noth- ing extra to the reader—stating something once in code and again in natural language does not make it any truer or more real. Commented-out code is not executable code, so it has no useful effect for either reader or runtime. It also becomes stale very quickly. Version-related comments and commented-out code try to address questions of versioning and history. These questions have already been answered (far more effectively) by version control tools.

??34 97 Things Every Programmer Should Know

?

???????????????A prevalence of noisy comments and incorrect comments in a codebase encourages programmers to ignore all comments, either by skipping past them or by taking active measures to hide them. Programmers are resourceful and will route around anything perceived to be damage: folding comments up; switching coloring scheme so that comments and the background are the same color; scripting to filter out comments. To save a codebase from such misapplications of programmer ingenuity, and to reduce the risk of overlook- ing any comments of genuine value, comments should be treated as though they were code. Each comment should add some value for the reader, otherwise it is waste that should be removed or rewritten.

What then qualifies as value? Comments should say something code does not and cannot say. A comment explaining what a piece of code should already say is an invitation to change code structure or coding conventions so the code speaks for itself. Instead of compensating for poor method or class names, rename them. Instead of commenting sections in long functions, extract smaller functions whose names capture the former sections’ intent. Try to express as much as possible through code. Any shortfall between what you can express in code and what you would like to express in total becomes a plausible candidate for a useful comment. Comment what the code cannot say, not simply what it does not say.

时间: 2024-10-17 21:36:39

Comment Only What the Code Cannot Say的相关文章

[转]Clean Code Principles: Be a Better Programmer

原文:https://www.webcodegeeks.com/web-development/clean-code-principles-better-programmer/ ----------------------------------------------------------------- "My code is working well, the website I built is looking great, and my client is happy. So why

能力库开发思路整理

能力库界面如下: 相关数据库表: 1 CREATE TABLE `base_ability` ( 2 `abillty_id` varchar(36) NOT NULL DEFAULT '' COMMENT '主键', 3 `ability_code` varchar(20) DEFAULT NULL, 4 `ability_name` varchar(20) DEFAULT NULL COMMENT '能力编号', 5 `ability_name_desc` varchar(255) DEFA

CSS制作水平垂直居中对齐 多种方式各有千秋

作为前端攻城师,在制作Web页面时都有碰到CSS制作水平垂直居中,我想大家都有研究过或者写过,特别的其中的垂直居中,更是让人烦恼.这段时间,我收 集了几种不同的方式制作垂直居中方法,但每种方法各有千秋呀,要正确的选择也是一件不容易的事情.我会将这几种方法一一介绍给大家,以供大家参考.或许对 于我这样的初学者有一定的帮助. 用CSS来实现元素的垂直居中效果是件苦差事,虽然说实现方法有多种,但有很多方式在某些浏览器下可能无法正常的工作.接下来我们就一起来看看这些不同方法实现垂直居中的各自优点和其不足

python练习题-20170916

周末开始第一次尝试对着书写代码练习题 <笨办法学python>--作者Zed A.Shaw,翻译Wang Dingwei ex1.pyprint('hello world')---------------------------------------ex2.py #A comment, this is so you can read your program later.#Anything after the # is ignored by python. print('i could ha

CCNET+MSBuild+SVN实时构建的优化总结

本文不是介绍如何使用CCNET+MSBuild+SVN构建自动编译系统,相关的内容可以从很多地方获取,可以再园子里搜一下. 随着我们的SVN库日益壮大,容量达到10G,几十G 甚至更大时,我们发现自动构建速度越来越慢,直到有一天你发现入了很小一段代码却不得不等待几小时构建完成,程序员的忍受是有极限的,因此我们决定采取措施实施优化. 首先,我们必须分析哪些因素导致了我们构建速度的减慢,罗列一下,大概如下几个方面: 1. SVN库太大,使得构建服务器在更新SVN代码时花费大量时间. 2. SVN库里

全面解析《嵌入式程序员应该知道的16个问题》

文章为转载文章,写的很好,和大家分享下,原文连接如下: ----Sailor_forever分析整理,[email protected] http://blog.csdn.net/sailor_8318/archive/2008/03/25/2215041.aspx 1.预处理器(Preprocessor) 2.如何定义宏 3.预处理器标识#error的目的是什么? 4.死循环(Infinite loops) 5.数据声明(Data declarations) 6.关键字static的作用是什么

【转】嵌入式程序员应该知道的16个问题

全面解析<嵌入式程序员应该知道的16个问题> ----Sailor_forever分析整理,[email protected] http://blog.csdn.net/sailor_8318/archive/2008/03/25/2215041.aspx 1.预处理器(Preprocessor) 2.如何定义宏 3.预处理器标识#error的目的是什么? 4.死循环(Infinite loops) 5.数据声明(Data declarations) 6.关键字static的作用是什么? 7.

LintCode-Longest Common Subsequence

Given two strings, find the longest comment subsequence (LCS). Your code should return the length of LCS. Example For "ABCD" and "EDCA", the LCS is "A" (or D or C), return 1 For "ABCD" and "EACB", the LCS

ex2-注释和井号

代码: print("I could have code like this.") # and the commnt after is ignored.# You can also use a comment to "disable" or comment a piece of code:# print("This won't run.")print("This will run.")