Good Code Is Like A Good Joke: It Needs No Explanation
Learn why clean, self-explanatory code matters, and how to write code so clear it needs no comments, like a well-told joke.
• 6 min read
• 6 min read
Learn why clean, self-explanatory code matters, and how to write code so clear it needs no comments, like a well-told joke.
• 6 min read
• 6 min read
A few years ago, I was reviewing a teammate's pull request. The logic worked fine, but the code looked like a maze. I found myself scribbling notes, trying to follow variables that seemed to appear out of nowhere, and function names that sounded like inside jokes only the author understood.
When we spoke, he explained everything perfectly, step by step.
That's when it hit me: if a block of code needs a lengthy guided tour, then it isn't as good as it could be.
After our conversation, he refactored the entire thing. The new version? Crystal clear. No explanation needed. It was like watching someone rewrite a rambling story into a perfect one-liner. That moment changed how I think about code forever.
It reminded me of a line I once read:
Good code is like a good joke: it needs no explanation.
Think about it. The best jokes land instantly. You don't need to pause and say, "Wait, here's why it's funny." If you do, the magic is gone. Code works the same way.
When you write code, you're not just communicating with a machine. You're communicating with people, your future self, your teammates, maybe even strangers who'll maintain your project years from now.
Bad jokes make people groan. Bad code makes people frustrated, and costs real time and money. Every hour spent deciphering unclear code is an hour not spent building features, fixing bugs, or helping customers. New team members take twice as long to onboard. Production incidents happen because someone misunderstood the logic.
Good jokes bring people together. Good code makes teams faster, happier, and more confident.
If you need a comment to explain what a function does, rename the function.
❌ Bad:
# A bad function name example
def process_data(x)
total = 0
x.each do |item|
if item[:status] == "active"
total += item[:amount]
end
end
total
end
✅ Better:
# A good function name example
def calculate_active_customer_revenue(customers)
revenue = 0
customers.each do |customer|
revenue += customer[:amount] if customer[:status] == "active"
end
revenue
end
See the difference? The second version tells you exactly what it does. No detective work required.
A good joke isn't a 10-minute story. Break large methods into smaller, focused ones.
❌ Confusing:
# Confusing: You must keep function short
def handle_order(order)
# Validate order
raise "Empty order" if order[:items].nil? || order[:items].empty?
# Calculate total
total = 0
order[:items].each do |item|
total += item[:price] * item[:quantity]
total -= item[:discount] if item[:discount]
end
# Apply tax
tax = total * 0.08
total += tax
# Process payment
raise "Insufficient payment" if total > order[:payment][:amount]
# Send confirmation
send_email(order[:customer][:email], "Order confirmed: $#{total}")
{ success: true, total: total }
end
✅ Clear:
# This function is clear
def handle_order(order)
validate_order(order)
subtotal = calculate_subtotal(order[:items])
total = apply_tax(subtotal)
process_payment(order[:payment], total)
send_confirmation_email(order[:customer], total)
{ success: true, total: total }
end
Now you can understand the flow at a glance. Each function does one thing and does it well.
Comments should explain why you're doing something unusual, not what the code is doing. The code itself should already explain that.
When comments ARE needed:
# Finance dept requires 30-day grace period per 2024 policy
# Using binary search here because dataset can exceed 1M records
# Don't refactor: API returns nil for new users, false for deleted ones
# Implementation based on RFC 6749 section 4.1
When comments are NOT needed:
# This code already explains itself
# Loop through users X
users.each do |user|
# Check if user is active X
if user.active?
# Add to active list X
active_users << user
end
end
This code explains itself. The comments add nothing.
If a joke doesn't land, a comedian rewrites it. If your code feels clunky, rewrite it until it flows smoothly.
When you finish writing a function, read it out loud. Does it make sense? Would someone who's never seen this codebase understand it? If not, refactor.
Who will read this code next? Write as if you're telling the joke to a beginner. If they can follow it, you've nailed it.
Ask yourself:
If the answer is no, simplify.
When reviewing code that doesn't "land":
Do:
find_eligible_subscribers?"
Don't:
When receiving feedback:
The best developers I've worked with didn't just write code that worked; they wrote code that spoke clearly. Reading it felt like watching a master comedian deliver a one-liner: Sharp, Effortless, and Memorable.
So the next time you're writing a function or reviewing a pull request, ask yourself: Does this code explain itself, or will I have to explain it?
Because if it needs no explanation… you've just written something great.
Pick one function you wrote recently. Read it with fresh eyes.
Then ask: Would a stranger get the joke?
If not, refactor it. Make it clearer. Make it simpler. Make it land.
Your future self and your teammates will thank you.