Don't Write Comments
Summary
TLDR这段视频脚本讨论了代码注释的价值和局限性。作者认为,在大多数情况下,应该避免编写注释,而是通过简化和重构代码来提高可读性。他强调,代码的复杂性应该通过变量命名和类型系统来解决,而不是依赖注释。注释可能会过时,而且没有工具来确保它们的准确性。相反,高质量的代码文档对于描述系统架构和公共API至关重要,应该与代码保持紧密联系。作者还提到,尽管有些情况下注释是必要的,但应该谨慎使用,并寻找其他方法来表达代码的意图。
Takeaways
- 📝 代码注释大多数时候是不必要的,应该通过改进代码来提高可读性。
- 🔍 如果代码复杂到需要注释解释,应该考虑重构或简化代码。
- 📌 使用常量代替硬编码值,如将数字5替换为有描述性的常量。
- 📋 通过变量命名来简化复杂的条件表达式,减少对注释的依赖。
- 🔄 将复杂条件移动到单独的函数中,提高代码的可读性。
- 🔧 类型系统可以减少注释的需要,如C++中的unique_ptr明确了资源所有权。
- 🚫 注释可能会过时,而代码有工具可以检查错误,注释没有这样的系统。
- 📖 阅读代码而不是注释来理解代码的功能。
- 📚 重视代码文档,它描述了系统的高层架构和公共API。
- 🔗 使用工具如Doxygen、pydoc和JavaDoc从代码文件生成文档。
- 📝 在某些情况下,注释是必要的,如性能优化的非显而易见的原因。
- 🔗 如果代码使用了特定的数学原理或算法,可以考虑添加源链接。
Q & A
为什么作者认为大多数时候不应该在代码中写注释?
-作者认为,如果代码需要注释来解释,那么更应该考虑简化或重构代码,使其更易于理解。
在代码中使用常量有什么好处?
-使用常量可以提高代码的可读性,使得代码中的数值更有意义,而不是仅仅作为一个数字存在。
如何通过变量命名来简化复杂的条件表达式?
-通过为表达式中的部分命名,可以使条件表达式更易于阅读,减少对注释的依赖。
将复杂条件移动到单独的函数中有什么优势?
-这样做可以使得代码更加模块化,易于理解和维护,同时减少对注释的需求。
在C++中,unique_ptr类型如何减少对注释的依赖?
-unique_ptr类型明确表示了对象的所有权,使得代码的意图更加清晰,减少了对注释的需要。
为什么说注释可能会带来问题?
-注释可能会随着代码的修改而变得过时,而且没有像代码那样的工具来检查注释的准确性。
作者为什么更倾向于阅读代码而不是注释?
-因为代码是精确的,而注释可能会出错或过时。作者认为代码本身就是最好的文档。
代码文档与注释有何不同?
-代码文档描述了系统的高层架构和公共API,而注释则描述了代码的内部工作方式。
为什么说高质量的文档对于API的使用很重要?
-高质量的文档可以帮助API的用户了解如何使用它,以及任何接口定义的期望,如线程安全、可能的状态或错误条件。
在什么情况下应该考虑使用注释?
-当代码为了性能原因做了一些不明显的操作,或者使用了特定的数学原理或算法时,可以考虑使用注释来解释。
作者如何看待未来编程语言的发展?
-作者认为代码是表达意图的更好方式,即使未来可能不需要编写代码,但目前来说,代码比注释更能准确表达开发者的意图。
Outlines
📝 代码注释的争议
本段落讨论了代码注释的使用,作者认为大多数情况下不应该在代码中添加注释。通过示例说明,如果代码复杂到需要注释来解释,那么应该考虑简化或重构代码。作者提倡使用常量、类型和函数来减少注释的需要,因为这些方法可以使代码更加清晰且易于维护。同时,作者强调了代码文档的重要性,它与注释不同,更侧重于描述系统的高层架构和公共API,有助于用户了解如何使用代码。
🤖 代码与注释的未来
这一段落通过一个关于未来可能不需要编码的幽默漫画,引出了代码比注释更能精确表达意图的观点。作者认为,尽管人工智能可能会影响到程序员的工作,但代码仍然是表达意图的最佳方式。最后,作者提出了一个问题,询问观众在哪些情况下认为注释是必要的,暗示了注释在某些特定情况下仍然有其价值。
Mindmap
Keywords
💡代码注释
💡代码可读性
💡代码重构
💡类型系统
💡文档
💡静态分析工具
💡代码意图
💡性能优化
💡数学原理
💡API
💡社会政治动荡
Highlights
大多数时候,你不应该在代码中写注释。
如果代码需要注释来解释,那么应该考虑简化或重构代码。
通过使用变量命名表达式的一部分,可以使条件语句更易读。
当条件足够复杂时,可以考虑将整个条件移动到自己的函数中。
类型可以消除注释的需要,例如C++中的unique_ptr类型。
注释可能会像代码一样出现错误,而且通常不会被更新。
代码文档与注释不同,它描述了系统的高层架构和公共API。
使用工具如Doxygen、pydoc和JavaDoc可以直接从代码文件生成文档。
文档应该描述类或API代表的内容以及接口定义的任何期望。
在某些情况下,例如为了性能原因,应该考虑使用注释。
如果代码利用了特定的数学原理或算法,可以考虑链接到源以帮助未来的维护者。
代码是一种比注释更精确的表达意图的方式。
如果你觉得需要用人类语言来描述你的代码,试着让你的代码更人性化。
注释可能会撒谎,但代码不会。
代码文档应该尽可能与代码保持同步。
在代码中使用注释的少数情况包括性能优化和特定算法的使用。
代码是更精确的规范表达方式。
Transcripts
It's time to get a bit controversial.
I don't think you should write comments in your code
pretty much most of the time.
Here,
we have some code where we expect the value to be 5
Looking at this code, it's not obvious what five signals.
We could add a comment explaining what five is,
but even better we can create a constant representing the variable instead.
The if statement now reads like a comment:
that we want status to be message sent.
If your code is complex enough that it warrants a comment, you should instead
see if you can simplify or refactor the code to make it better instead.
Right now, this condition is complex enough
that we add a comment explaining it, but we can simplify this
by using variables to name parts of the expression.
Now the condition reads like the comment does.
So the comment is basically redundant and can be removed.
When conditions are complex enough like this,
you could also consider moving the whole condition to its own function.
Now you don't need to decipher at all.
Types can also make comments redundant.
In C++, there's no built in memory management.
You often have a function like this where you get back a thing,
but we need to make clear who will take ownership of the thing.
Ownership, meaning the responsibility
to release the memory after everyone is done with it.
In older C++, you pretty much had to rely on comments to do this.
If you were given ownership of an object, you'd find out by reading
the comments of the function.
But C++ added a new type called unique_ptr.
The type represents a unique reference to the object.
So if you get one, congratulations!
It's now your responsibility.
The type tells you explicitly
that you now own it without the need for a comment.
And even better, the type makes it so you get a compile error
if you do bad things with it.
A comment doesn't do that.
Likewise, if we have a function that returns an int,
but that int is optional.
We could add a comment saying that -1 means we're not actually
returning a value.
But even better, we could use a type.
If we return an optional int instead, it's
now obvious that we might not give a value.
We can't miss the comment and not realize that we might get
an invalid timestamp back.
The user needs to handle a missing value or they'll get a compiler error.
You might
wonder why don't we just make our code high quality and add comments anyways?
Isn't more comments just better?
That ignores the problems with comments.
Comments get bugs like code.
When people make changes to code,
they often don't update the comments to match.
But unlike comments, we have tools to stop bugs from entering code.
We have tests, compiler checks and limiting.
We don't have any system like that for comments.
Maybe one day we can have some static analysis tool that uses AI to determine
if the code matches the comments and flags any discrepancies -
there's a start up idea - but because of this I don't trust the comments
even when they exist.
Comments can lie, but code cannot.
So when trying to understand what a piece of code does, I read the code.
I never read the comments.
Maybe it's just me.
Do you guys find yourself reading comments to understand code?
What I do read is code documentation.
Code documentation describes the high level
architecture and public APIs of a system.
Code documentation differs from comments
where comments describe the internals of how your code works.
Documentation describes how you use the code.
The world needs more high quality documentation,
and while we could write the documentation for our code
completely separated from our code, it makes sense to keep our documentation
as close to the code as possible in order to try to help them stay in sync.
Tools like Doxygen, pydoc and JavaDoc generate documents
directly from code files and therefore change alongside our code.
It's useful to document what a class or API represents
and any expectations for interface definitions
like thread safety, possible states or error conditions.
This helps the consumers of the API know how to use it and also helps
any new implementers of the interface
know how they're supposed to operate and behave.
The guidance I've given about comments still applies for documentation.
If we write better APIs, our documentation will be more concise
and less prone to errors.
But I concede that you'll never be able to make all of the parts
of the system obvious with pure code.
I do think there's a few cases where you should consider comments:
if the code does something non-obvious for performance reasons,
a comment can help explain why the code looks weird.
If the code is utilizing a specific
mathematical principle or an algorithm from a particular source
you might consider linking to the source to help future maintainers.
There's this comic called comic strip,
and one of the comics is about a guy
telling a developer that one day we won't need code in the future
because you'll be able to
simply write a spec and the program will just write itself.
The developers are casting retorts that code is just a more precise way
of writing a spec.
I mean, I think it's wrong. And A.I.
is coming for all our jobs, and we should be afraid
of the impending sociopolitical turmoil that comes when our best tool
for distributing wealth, the job disappears.
But alas, it still helps my point.
Code is a much better way to express intent than comments about code.
So in general, if you feel like you need human language to describe your code,
see if you could make your code more human.
What other cases do you think comments are needed?
5.0 / 5 (0 votes)
Risk-Based Alerting (RBA) for Splunk Enterprise Security Explained—Bite-Size Webinar Series (Part 3)
Roadmap for Learning SQL
How to Optimize Performance in Unreal Engine 5
Breaking down Scarlett Johansson's dispute with OpenAI
Microsoft's New PHI-3 AI Turns Your iPhone Into an AI Superpower! (Game Changer!)
Why You Shouldn't Nest Your Code