Tidy code is pleasant to look at, but that's only the beginning. Have you ever read code that looked neat and was still hard to understand?
- Maybe the variable names are too abstract.
- Maybe one function takes on too many responsibilities.
- Or maybe the same business logic is scattered across the application.
That's where clean code becomes more than aesthetics. Clean code helps us write software that is easier to:
Read
Understood without guesswork.
Change
Modified without unnecessary side effects.
Test
Verified in a more focused way.
Maintain
Looked after over the long term.
Clean code ≠ pretty code
Many developers think clean code means:
- Neat indentation.
- Consistent formatting.
- No overly long lines of code.
Those things do help, but formatting alone doesn't make code clean. Code can look neat and still be hard to understand, full of duplication, and risky to change.
Code is read more often than it is written
Developers don't only write code. They also have to read it, understand it, debug it, and change existing code.
const d = 86400000;
const x = users.filter(
u => u.a && u.l < Date.now() - d
);const ONE_DAY_MS = 86_400_000;
const dormantUsers = users.filter(
user =>
user.isActive &&
user.lastLoginAt <
Date.now() - ONE_DAY_MS
);Good names reduce mental effort
Compare the two examples below.
function calc(p: number, q: number) {
return p * q;
}function calculateTotalPrice(
unitPrice: number,
quantity: number
) {
return unitPrice * quantity;
}The goal is not longer names. It's clearer intent.
A good name explains what the code is meant to do, not just its data type or the steps it takes.
One function, one clear responsibility
A function that does too many things is harder to understand, test, and fix.
async function registerUser(input: RegisterInput) {
if (!input.email.includes("@")) throw new Error();
const user = await db.users.insert(input);
await mailer.send(user.email, "Welcome!");
return user;
}async function registerUser(input: RegisterInput) {
const validatedInput = validateInput(input);
const user = await createUser(validatedInput);
await sendWelcomeEmail(user);
return user;
}Splitting responsibilities into meaningful functions can make the flow clearer. But small functions aren't a goal in themselves: every extra abstraction must genuinely improve clarity.
Duplication makes change expensive
When the same business logic is spread across many places, a small change can require many fixes. Say the premium-customer discount rule is copied into three different functions. When the rule changes, one implementation can get left behind and produce inconsistent behavior.
Principles to keep in mind:
- Avoid duplicating the same business logic.
- Extract a shared function when there really is a shared rule or behavior.
- Don't create an abstraction just because two pieces of code look alike.
Good abstraction reduces duplication without hiding intent.
Clean code makes change safer
Code that's easy to understand is easier to evolve as requirements change. Some of its traits:
- Names and structure reflect the business logic.
- Every function has a clear purpose.
- Errors are handled explicitly.
- Tests verify the important behavior.
- Abstractions are no more complicated than the problem.
Clean code doesn't mean code that never changes. It helps changes happen with more controlled risk.
Clean code is about clarity, not cosmetics.
Good code isn't just code that looks neat. Good code helps the next developer understand:
- What does this code do?
- Why does it exist?
- How can I change it safely?
Write code for the next person who has to read it.