惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Securelist
C
CERT Recently Published Vulnerability Notes
Forbes - Security
Forbes - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
L
LINUX DO - 最新话题
The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
SecWiki News
SecWiki News
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The Last Watchdog
The Last Watchdog
S
Schneier on Security
T
Troy Hunt's Blog
N
News | PayPal Newsroom
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Schneier on Security
Schneier on Security
P
Privacy & Cybersecurity Law Blog
T
Tor Project blog
T
Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
A
Arctic Wolf
S
Secure Thoughts
P
Proofpoint News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Security Latest
Security Latest
Scott Helme
Scott Helme
Security Archives - TechRepublic
Security Archives - TechRepublic
Latest news
Latest news
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
T
Threat Research - Cisco Blogs
Webroot Blog
Webroot Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
大猫的无限游戏
大猫的无限游戏
T
Tenable Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
H
Hacker News: Front Page
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog

Kent C. Dodds Blog

Implementing Hybrid Semantic + Lexical Search Simplifying Containers with Cloudflare Sandboxes Migrating to Workspaces and Nx Offloading FFmpeg with Cloudflare Building Semantic Search on my Content Helping YOU ask ME questions with AI How I used Cursor to Migrate Frameworks The Dow's Start on the Covenant Path 2025 in Review The next chapter: EpicAI.pro AI is taking your job How I increased my visibility Launching Epic Web 2023 in Review Stop Being a Junior RSC with Dan Abramov and Joe Savona Live Stream Fixing a Memory Leak in a Production Node.js App 2022 in Review My Car Accident I Migrated from a Postgres Cluster to Distributed SQLite with LiteFS I'm building EpicWeb.dev A review of my time at Remix Remix: The Yang to React's Yin How I help you build better websites Why I Love Remix The State Initializer Pattern How to React ⚛️ Get a catch block error message with TypeScript Building an awesome image loading experience How Remix makes CSS clashes predictable Introducing the new kentcdodds.com How I built a modern website in 2021 How to use React Context effectively Static vs Unit vs Integration vs E2E Testing for Frontend Apps The Testing Trophy and Testing Classifications Array reduce vs chaining vs for loop Don't Solve Problems, Eliminate Them Super Simple Start to Remix Super Simple Start to ESModules in Node.js JavaScript Pass By Value Function Parameters How to write a Constrained Identity Function (CIF) in TypeScript How to optimize your context value How to write a React Component in TypeScript TypeScript Function Syntaxes Listify a JavaScript Array Build vs Buy: Component Libraries edition Using fetch with TypeScript Wrapping React.useState with TypeScript Define function overload types with TypeScript 2020 in Review Business and Engineering alignment Hi, thanks for reaching out to me 👋 useEffect vs useLayoutEffect Super simple start to Firebase functions Super simple start to Netlify functions Super Simple Start to css variables Favor Progress Over Pride in Open Source Testing Implementation Details How getting into Open Source has been awesome for me useState lazy initialization and function updates Use ternaries rather than && in JSX Application State Management with React Use react-error-boundary to handle errors in React JavaScript to Know for React How I structure Express apps What open source project should I contribute to? When I follow TDD AHA Programming 💡 How I Record Educational Videos Should I write a test or fix a bug? Stop mocking fetch Intentional Career Building Improve test error messages of your abstractions Tracing user interactions with React Eliminate an entire category of bugs with a few simple tools Common mistakes with React Testing Library Super Simple Start to React Stop using client-side route redirects The State Reducer Pattern with React Hooks Function forms Replace axios with a simple custom fetch wrapper How to test custom React hooks React Production Performance Monitoring Should I useState or useReducer? Stop using isLoading booleans Make Your Test Fail Make your own DevTools An Argument for Automation Fix the "not wrapped in act(...)" warning Super Simple Start to ESModules in the Browser Implementing a simple state machine library in JavaScript 2010s Decade in Review Why users care about how you write code Why I avoid nesting closures Don't call a React function component Why your team needs TestingJavaScript.com Inversion of Control Understanding React's key prop How to Enable React Concurrent Mode Profile a React App for Performance
Please, don't commit commented out code
2015-10-27 · via Kent C. Dodds Blog

I often find stuff like this in codebases I review:

function foo(bar) {
	const baz = bar(false)
	// we no longer do this for some good reason
	// if (baz === 'foobar') {
	// return baz
	// } else {
	// return bar.foobar()
	// }
	return baz
}

This function should look like this:

function foo(bar) {
	return bar(false)
}

You might be thinking: "But Kent! What if that 'good reason' is no longer true, and we need to do it the old way again later?" The answer, my dear reader, is git diff:

There's the precious code. We can grab those changes and boom! We're back in business!

Now you might be thinking: "Ok, cool, but what's the problem with leaving the commented code in there? It's easier for people to see how things used to be, it might be helpful to them, and it's a way for me to leave my mark on the codebase forever!!"

Here are a few of the reasons you should probably not commit commented out code:

Focus and cognitive load

For me, this is the biggest reason and reason enough to avoid doing it altogether. I don't know about you, but when I come to commented out code, I'll often stop what I'm doing to read it. I think "maybe it's important" or I may simply be curious. Either way, my workflow has been derailed.

Hides what's important

I have seen stuff like this:

// dozens
//
// of
//
// lines
//
// of
//
// commented
//
// code
someImportantCode()
// dozens
//
// of
//
// more
//
// lines
//
// of
//
// commented
//
// code

It can be possible to skip over someImportantCode() when scanning over a file. This is less likely with the right syntax highlighting but it can happen, and the comments are simply not worth keeping around.

Out of date

I've long held the opinion that the only thing that can tell you the truth about the code is the code. The instant you add a comment, it's out of date. Documentation comments are beneficial enough to justify their existence (though you should try to make your code self-documenting for people other than yourself).

However commented out code does not justify its existence. It wont take long before that commented code is out of context, no longer tested, linted, or run, the APIs it was using have changed or been removed, and now it's just in the way.

Conclusion

Again, the main problem is commented code adds confusion with no real benefit. Just rely on your version control system to keep track of the code that once was. Let it go.

If you're an eslint person, you might be interested in Gleb Bahmutov's eslint-rules (specifically his no-commented-out-code rule). If you're not an eslint person, listen to Jamund Ferguson on JavaScript Jabber. His enthusiasm for it will convince you that you should try it out :-)


QYMAM

_k-why-mahm _ —  Questions You Might Ask Me:

Q: Are there exceptions to this rule? A: Yes. But they're rare.

Q: What about stuff I'm working on but isn't yet working? I don't want to wait until it's all done because my computer might get stolen or catch fire! A: Put that in a branch. There's no problem committing totally broken stuff to a branch. Once it's done, you can merge the working stuff into master.Personally I recommend that you squash commits into atomically working commits before you put those into master, but as long as it's working when it gets into master then you're good.

Q: What about when we absolutely know the code is done, but some third party integration is required before it can be committed? I don't want to lose that knowledge. Can't I just say: “// uncomment this code when foo is done” ?? A: In my opinion, that TODO should live in a story and the commented out code should live in a development branch. What if the third party integration fails or is dropped? You now have to remember to remove the comment and the commented out code.

Q: What about examples for third party integrators? A: To me, that's not commented out code, that's documentation. I'm totally cool with that. Though, to be honest, it sort of drives me nutso that the documentation in the AngularJS 1 project is in the form of comments in the files. The first line of runnable code in compile.js is line 737! But that's another blogpost (and I'm not saying they're wrong to do that… There are tradeoffs for sure).

Q: git history isn't very discoverable… How do I find the commit that removed the code I want back? A: A very valid question. Removing commented code not only makes it harder to find later, but it also makes it so people in the future don't know it existed before. For finding the code, there are tools and git commands to help you look at the history of a file. I've had to do this before, going back months (even years) in a file was actually quite trivial. As for knowing of its existence, if it's that important, you could add a comment that explains briefly that something important existed there before and people can find it in the git history. I believe this is an extremely rare case and can't think of personally ever needing to do this.

Do you have more questions? Add a comment here or ping me on twitter.


Thank you Matt Zabriskie, Lin Clark, Kyle, Jamund Ferguson, and Gleb Bahmutov for reviewing this blog post.