You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/blog/2025-08-15-comments-in-code.md
+14-11Lines changed: 14 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
title: 'Code Comments: The Good, The Bad, and The Hilarious'
2
+
title: "Code Comments: The Good, The Bad, and The Hilarious"
3
3
description: A guide to writing comments that help (and avoiding the ones that hurt), with a collection of the funniest gems from real codebases.
4
4
slug: code-comments
5
5
authors: ozgur
@@ -28,7 +28,6 @@ hide_table_of_contents: false
28
28
29
29
---
30
30
31
-
32
31
# Code Comments: The Good, The Bad, and The Hilarious
33
32
34
33
Every developer has been there. It’s 2 PM, you're deep into a feature, and you stumble upon a function named `handleData`. It takes three arguments: `x`, `y`, and `flag`. It returns `0` or `1`. There are no comments. You are now an unwilling digital archaeologist, and your afternoon is officially ruined.
@@ -46,14 +45,15 @@ Good comments don't explain **what** the code is doing; they explain **why**. If
46
45
Here's where comments truly shine:
47
46
48
47
#### 1. Explaining the "Why" (The Business and Product Logic)
48
+
49
49
Code is excellent at showing the implementation, but it's terrible at capturing external context. This is the most valuable role a comment can play: bridging the gap between a business decision and a line of code.
50
50
51
51
```javascript
52
52
// Apply a 10% holiday discount for all premium users.
53
53
// This is for the Q4 campaign and must be removed after Jan 31st.
54
54
// See ticket JIRA-512 for the official request from Marketing.
55
55
if (user.isPremium) {
56
-
price *= 0.90;
56
+
price *=0.9;
57
57
}
58
58
```
59
59
@@ -107,7 +107,7 @@ A good comment can act as a crucial warning sign about critical, non-obvious con
107
107
API_TIMEOUT=2.5
108
108
```
109
109
110
-
-----
110
+
---
111
111
112
112
## The Bad: When Comments Are a Liability
113
113
@@ -148,7 +148,7 @@ Developers sometimes write confusing, poorly named code and then use a comment a
148
148
```javascript
149
149
// This function gets the items from the database (d) and filters them
150
150
// based on the user's permissions (p).
151
-
function getFltItems(d, p) {
151
+
functiongetFltItems(d, p) {
152
152
// ...
153
153
}
154
154
```
@@ -166,9 +166,10 @@ function filterItemsByUserPermissions(items, permissions) {
166
166
In the age of version control systems like Git, there is no reason to leave huge blocks of commented-out code in the codebase. It’s digital hoarding. It confuses search tools, clutters the file, and makes other developers wonder if it's important, disabled, or just forgotten. **Just delete it.** If you ever need it back, your Git history is there for you.
167
167
168
168
### 5\. The Rise of the AI Commentator 🤖
169
+
169
170
You've probably noticed that modern AI coding assistants (like Copilot or Cursor) love to add comments to almost everything they write. This isn't because they're trying to be helpful in a nuanced way; it's because they are trained on billions of lines of public code, where they've learned to associate a specific code block with a specific explanatory comment. The result is that they often produce perfectly redundant comments that explain what the code is doing, not why. The AI provides a verbose first draft, but it's still the developer's job to be the editor—to delete the noise and preserve only the comments that provide genuine insight.
170
171
171
-
-----
172
+
---
172
173
173
174
## The Hilarious: Dispatches from the Codebase Trenches
174
175
@@ -184,12 +185,12 @@ Every experienced developer has stumbled upon comments that are less about docum
184
185
185
186
```javascript
186
187
// Dear maintainer:
187
-
//
188
+
//
188
189
// Once you are done trying to 'optimize' this routine,
189
190
// and have realized what a terrible mistake that was,
190
191
// please increment the following counter as a warning
191
192
// to the next guy:
192
-
//
193
+
//
193
194
// total_hours_wasted_here = 42
194
195
```
195
196
@@ -239,21 +240,23 @@ It's bloated, confusing, and pretty awful by necessity(for the most part).
239
240
*/
240
241
```
241
242
242
-
*** And of course, when you feel too guilty ***
243
+
**_ And of course, when you feel too guilty _**
244
+
243
245
```javascript
244
246
// I'm sorry.
245
247
```
246
248
247
249
### Linus Torvalds
248
250
249
-
Before finishing this blog post, I just wanted to mention Linus Torvalds, the creator of both the Linux kernel and the version control system Git. I won’t dive deep into those, since that’s not the focus here, but their rants on both reviews, and comments to code are legendarily known among the community.
251
+
Before finishing this blog post, I just wanted to mention Linus Torvalds, the creator of both the Linux kernel and the version control system Git. I won’t dive deep into those, since that’s not the focus here, but their rants on both reviews, and comments to code are legendarily known among the community.
250
252
251
253
```javascript
252
254
// Wirzenius wrote this portably, Torvalds fucked it up.
253
255
```
256
+
254
257
If you don't know about them, I recommend googling them and their comments. Just for fun, if nothing else.
255
258
256
-
### Conclusion
259
+
### Conclusion
257
260
258
261
These gems serve as a crucial reminder: code is written by people. It can be a place of pristine logic and structure, but it's also one of chaos, humor, and shared struggle.
0 commit comments