Comments are like apologies to the next programmer

Comments are like apologies to the next programmer.
— Unknown

I saw this quote today in an online forum. I love it!

I have long reeled against comments in code. That’s not to say they’re never useful. It’s just that 95% of code comments are worthless, or even worse.

But if this is true, then what are comments apologizing for?

Hard-to-read code.

// Determine the shipping rate
var s = 15;
if ( p >= 100 || q >= 5 ) {
    s = 0;
}

A little refactoring can avoid the need to apologize with a comment:

const shippingPrice = 15;
const freeShippingPrice = 100;
const freeShippingQuantity = 5;

function calculateShipping(price, quantity) {
    if ( price >= freeShippingPrice || quantity >= freeShippingQuantity ) {
        return 0;
    }
    return shippingPrice;
}
Share this