DEV Community

Timevolt
Timevolt

Posted on

The Dark Knight of Clean Code: Writing Code That Doesn't Need Comments

The Quest Begins (The “Why”)

I still remember the first time I opened a legacy module that looked like a cryptic spell book. Variables named tmp, data, and x littered the file, and every few lines there was a comment that read “// hack to make it work – TODO: fix later”. I spent an entire afternoon tracing a single bug, only to discover that the comment was outright wrong – the code had changed, but the comment stayed behind like a stubborn side‑kick refusing to leave the battlefield.

That experience taught me something painful: comments can become liabilities. When they’re out of sync, they mislead more than they help. I started wondering: What if we could write code so clear that comments were unnecessary?

The Revelation (The Insight)

The best practice that changed everything for me is intention‑revealing functions – breaking down a block of logic into tiny, well‑named functions whose names are the documentation.

Think of it like giving each step of a quest a clear title: instead of scribbling notes on a map (“go left after the scary tree”), you name the path itself (“Enter the Enchanted Forest”). When the function name tells you exactly what it does, there’s no need for a comment to explain it.

Why does this work?

  1. Names are checked by the compiler/interpreter – if you rename a function, every call site updates automatically. Comments stay static and can drift.
  2. Small functions are easier to test – you can unit‑test each intention in isolation.
  3. Reading the code becomes a narrative – you follow the story from high‑level to low‑level without pausing to decipher side notes.

Wielding the Power (Code & Examples)

Before: The “comment‑heavy” struggle

// Calculate the total price for a cart
function calculateCartTotal(cart) {
  let total = 0; // start at zero
  for (let i = 0; i < cart.length; i++) { // iterate each item
    const item = cart[i];
    let price = item.price; // base price
    if (item.discount > 0) { // apply discount if any
      price = price * (1 - item.discount/100 - item.discount / 100); // WRONG! should be (1 - discount/100)
    }
    total += price * item.quantity; // add line‑item subtotal
  }
  // add tax (8.5%)
  total = total * 1.085;
  return total; // final amount
}
Enter fullscreen mode Exit fullscreen mode

See the problems?

  • The comment “// apply discount if any” is outright misleading – the math is wrong.
  • The tax comment is fine, but if the tax rate ever changes, the comment will lag behind.
  • A future maintainer has to mentally parse the loop, the discount logic, and the tax line – all while hoping the comments aren’t lying.

After: Intention‑revealing functions

function calculateCartTotal(cart) {
  const subtotal = sumLineItems(cart);
  return applyTax(subtotal);
}

function sumLineItems(cart) {
  return cart.reduce((sum, item) => sum + itemLineTotal(item), 0);
}

function itemLineTotal(item) {
  const discountedPrice = applyDiscount(item.price, item.discount);
  return discountedPrice * item.quantity;
}

function applyDiscount(price, discountPercent) {
  return price * (1 - discountPercent / 100);
}

function applyTax(amount) {
  // Tax rate is stored centrally; if it changes, we update it here once.
  const TAX_RATE = 0.085;
  return amount * (1 + TAX_RATE);
}
Enter fullscreen mode Exit fullscreen mode

What changed?

  • Each function name tells you exactly what it does – no comment needed.
  • The discount bug is impossible now because the formula lives in applyDiscount, a single place that’s easy to unit‑test.
  • If the tax rate changes, you edit one constant in applyTax; every caller gets the fix automatically.
  • Reading calculateCartTotal reads like a high‑level story: “Get the subtotal, then apply tax.”

The trap to avoid

Don’t fall into the “over‑extracting” pitfall where you create a function for every single line, like addTwoNumbers(a, b). That just adds noise. Keep the extraction intentional – each function should representational question: How do we get the line‑item total? How do we apply a discount?

Why This New Power Matters

When you write code that speaks for itself, you gain three super‑powers:

  1. Speed – New teammates (or future you) can grasp the flow in minutes, not hours.
  2. Safety – Fewer places for bugs to hide; logic lives in one spot, making refactors less scary.
  3. Joy – There’s a genuine thrill when you look at a function and think, “Yep, that does exactly what it says.”

It’s like finally seeing the Matrix code behind the rain – you understand the system instead of being blinded by symbols.

Your Turn

Pick a chunk of code you’ve been avoiding because it’s “too messy.” Spend ten minutes extracting one intention‑revealing function. Give it a name that reads like a sentence. Then step back and ask: Did I just make a comment obsolete?

If you answered yes, you’ve leveled up. Now go find the next block and repeat. Your future self (and your teammates) will thank you.

Happy coding, and may your functions always be as clear as a hero’s quest! 🚀

Top comments (0)