Why Clean Code?
iCode is read far more than it is written
Studies show developers spend 10x more time reading code than writing it. Clean code reduces cognitive load, makes onboarding faster, and prevents bugs born from unclear logic.
Write
→
Read
→
Maintain
→
Scale
Naming Conventions
Names are the primary documentation of your code. A well-named variable or function eliminates the need for a comment entirely. Avoid abbreviations, single letters, and vague nouns.
| Bad | Good |
|---|---|
| d | daysSinceLastLogin |
| getInfo() | getUserProfileData() |
| flag | isEmailVerified |
| tmp | pendingTransactions |
| Controller2 | PaymentController |
naming.ts
// Bad
const d = calc(u.ts);
const flag = getInfo();
// Good
const daysSinceLastLogin = calculateDaysSince(user.lastLoginTimestamp);
const isEmailVerified = getUserVerificationStatus();Readability Impact by Practice
Good naming90 %
Small functions85 %
No deep nesting80 %
Comments40 %
Function Design
Well-designed functions are the atoms of clean code. Each one should be small, focused, and named so clearly that its body reads like a confirmation of the name.
1
Single Responsibility
A function should do one thing and do it well. If you need 'and' to describe it, split it into two.
2
Keep It Small
Aim for functions under 20 lines. Smaller functions are easier to name, test, and reuse.
3
Limit Parameters
Prefer 2 or fewer parameters. When more are needed, group them into a typed options object.
Code Smells
Code smells are surface signals of deeper design problems. Recognizing them early lets you refactor before technical debt compounds.
God Class
One class that knows and does too much. Break it into focused, single-purpose classes.
Long Method
Methods over 30 lines are a sign of decomposition debt. Extract helpers with clear names.
Magic Numbers
Unexplained literals like `86400` buried in logic. Replace with named constants.
Deep Nesting
More than 3 indent levels signals complex logic. Use early returns or extract sub-functions.
Refactoring Tips
Refactoring is the discipline of improving code structure without changing behavior. Small, committed improvements compound into a significantly cleaner codebase over time.
Refactoring Workflow
1
Identify Smell
2
Write Test
3
Refactor
4
Verify
5
Commit
refactor-example.ts
// Before: hard to follow
function p(u: any) {
if (u && u.a === true && u.age > 18) {
return u.n.split(" ")[0];
}
return null;
}
// After: self-documenting
const MIN_ADULT_AGE = 18;
function getFirstName(user: User): string | null {
const isEligible = user.isActive && user.age > MIN_ADULT_AGE;
if (!isEligible) return null;
return user.fullName.split(" ")[0];
}✓Refactor safely
Always have tests before refactoring. Rename first, then extract, then simplify. Commit after each small improvement so you can roll back with confidence.