Skill: Clean Code
Write code that is easy to read, understand, and maintain.
Naming
Rules
- ✅ DO: Use descriptive, intention-revealing names
- ✅ DO: Use verbs for functions (
getUserById, calculateTotal)
- ✅ DO: Use nouns for classes and variables (
user, orderList)
- ✅ DO: Use consistent naming conventions (camelCase, PascalCase)
- ❌ DON'T: Use abbreviations (
btn, msg, usr, idx)
- ❌ DON'T: Use single letters except in short loops (
i, j ok in loops)
- ❌ DON'T: Use Hungarian notation (
strName, intCount)
- ❌ DON'T: Add unnecessary context (
userUserName → user.name)
Examples
// ❌ Bad
const d = new Date(); // What is d?
const ymd = formatDate(d); // Cryptic
function proc(u: any) {} // Unclear
// ✅ Good
const currentDate = new Date();
const formattedDate = formatDate(currentDate);
function processUser(user: User) {}
Functions
Rules
- ✅ DO: Keep functions small (under 20 lines ideally)
- ✅ DO: Do one thing (single responsibility)
- ✅ DO: Use early returns to reduce nesting
- ✅ DO: Limit parameters to 3 or fewer (use object for more)
- ❌ DON'T: Mix abstraction levels
- ❌ DON'T: Use flag arguments (split into two functions)
- ❌ DON'T: Have side effects that aren't obvious from the name
Examples
// ❌ Bad - does multiple things, deep nesting
function processOrder(order: Order) {
if (order) {
if (order.items.length > 0) {
if (order.user) {
// validate
// calculate
// save
// send email
}
}
}
}
// ✅ Good - single responsibility, early returns
function processOrder(order: Order) {
validateOrder(order);
const total = calculateTotal(order);
saveOrder(order, total);
notifyUser(order.user);
}
function validateOrder(order: Order) {
if (!order) throw new Error("Order is required");
if (order.items.length === 0) throw new Error("Order has no items");
if (!order.user) throw new Error("Order has no user");
}
Comments
Rules
- ✅ DO: Write self-documenting code instead of comments
- ✅ DO: Use comments for "why", not "what"
- ✅ DO: Document public APIs with JSDoc
- ✅ DO: Add TODO/FIXME with ticket numbers
- ❌ DON'T: Comment obvious code
- ❌ DON'T: Leave commented-out code
- ❌ DON'T: Write redundant comments
Examples
// ❌ Bad - obvious comment
// increment counter by 1
counter += 1;
// ❌ Bad - commented code
// const oldValue = calculateOldWay(x);
const value = calculateNewWay(x);
// ✅ Good - explains why
// Using floor to ensure integer for pagination offset
const offset = Math.floor(page * limit);
// ✅ Good - JSDoc for public API
/**
* Calculates the total price including tax.
* @param items - Array of items with price property
* @param taxRate - Tax rate as decimal (e.g., 0.08 for 8%)
* @returns Total price with tax applied
*/
function calculateTotal(items: Item[], taxRate: number): number {
// ...
}
Code Organization
Rules
- ✅ DO: Group related code together
- ✅ DO: Order: imports, types, constants, main code, helpers
- ✅ DO: Keep files focused (one component/class per file)
- ✅ DO: Use consistent file naming
- ❌ DON'T: Mix unrelated functionality
- ❌ DON'T: Create god files with thousands of lines
Magic Numbers & Strings
Rules
- ✅ DO: Extract magic numbers to named constants
- ✅ DO: Use enums for related constants
- ❌ DON'T: Hardcode values that have meaning
Examples
// ❌ Bad
if (user.age >= 18) {
}
if (status === 1) {
}
setTimeout(fn, 86400000);
// ✅ Good
const MINIMUM_AGE = 18;
if (user.age >= MINIMUM_AGE) {
}
enum OrderStatus {
Pending = 1,
Completed = 2,
}
if (status === OrderStatus.Pending) {
}
const * 60 * 60 * 1000;
setTimeout(fn, ONE_DAY_MS);
Simplicity (KISS)
Rules
- ✅ DO: Choose the simplest solution that works
- ✅ DO: Avoid premature optimization
- ✅ DO: Avoid premature abstraction
- ❌ DON'T: Over-engineer for hypothetical future needs
- ❌ DON'T: Add complexity without clear benefit
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: clean-code-133description: Principles for writing readable, maintainable, and simple code. Use when writing new functions, refactoring existing code, doing code reviews, or when user asks about "naming conventions", "code readability", "clean code", or "code quality". Use when this capability is needed.4---56# Skill: Clean Code78Write code that is easy to read, understand, and maintain.910## Naming1112### Rules1314- ✅ DO: Use descriptive, intention-revealing names15- ✅ DO: Use verbs for functions (`getUserById`, `calculateTotal`)16- ✅ DO: Use nouns for classes and variables (`user`, `orderList`)17- ✅ DO: Use consistent naming conventions (camelCase, PascalCase)18- ❌ DON'T: Use abbreviations (`btn`, `msg`, `usr`, `idx`)19- ❌ DON'T: Use single letters except in short loops (`i`, `j` ok in loops)20- ❌ DON'T: Use Hungarian notation (`strName`, `intCount`)21- ❌ DON'T: Add unnecessary context (`userUserName` → `user.name`)2223### Examples2425```typescript26// ❌ Bad27const d = new Date(); // What is d?28const ymd = formatDate(d); // Cryptic29function proc(u: any) {} // Unclear3031// ✅ Good32const currentDate = new Date();33const formattedDate = formatDate(currentDate);34function processUser(user: User) {}35```3637## Functions3839### Rules4041- ✅ DO: Keep functions small (under 20 lines ideally)42- ✅ DO: Do one thing (single responsibility)43- ✅ DO: Use early returns to reduce nesting44- ✅ DO: Limit parameters to 3 or fewer (use object for more)45- ❌ DON'T: Mix abstraction levels46- ❌ DON'T: Use flag arguments (split into two functions)47- ❌ DON'T: Have side effects that aren't obvious from the name4849### Examples5051```typescript52// ❌ Bad - does multiple things, deep nesting53function processOrder(order: Order) {54 if (order) {55 if (order.items.length > 0) {56 if (order.user) {57 // validate58 // calculate59 // save60 // send email61 }62 }63 }64}6566// ✅ Good - single responsibility, early returns67function processOrder(order: Order) {68 validateOrder(order);69 const total = calculateTotal(order);70 saveOrder(order, total);71 notifyUser(order.user);72}7374function validateOrder(order: Order) {75 if (!order) throw new Error("Order is required");76 if (order.items.length === 0) throw new Error("Order has no items");77 if (!order.user) throw new Error("Order has no user");78}79```8081## Comments8283### Rules8485- ✅ DO: Write self-documenting code instead of comments86- ✅ DO: Use comments for "why", not "what"87- ✅ DO: Document public APIs with JSDoc88- ✅ DO: Add TODO/FIXME with ticket numbers89- ❌ DON'T: Comment obvious code90- ❌ DON'T: Leave commented-out code91- ❌ DON'T: Write redundant comments9293### Examples9495```typescript96// ❌ Bad - obvious comment97// increment counter by 198counter += 1;99100// ❌ Bad - commented code101// const oldValue = calculateOldWay(x);102const value = calculateNewWay(x);103104// ✅ Good - explains why105// Using floor to ensure integer for pagination offset106const offset = Math.floor(page * limit);107108// ✅ Good - JSDoc for public API109/**110 * Calculates the total price including tax.111 * @param items - Array of items with price property112 * @param taxRate - Tax rate as decimal (e.g., 0.08 for 8%)113 * @returns Total price with tax applied114 */115function calculateTotal(items: Item[], taxRate: number): number {116 // ...117}118```119120## Code Organization121122### Rules123124- ✅ DO: Group related code together125- ✅ DO: Order: imports, types, constants, main code, helpers126- ✅ DO: Keep files focused (one component/class per file)127- ✅ DO: Use consistent file naming128- ❌ DON'T: Mix unrelated functionality129- ❌ DON'T: Create god files with thousands of lines130131## Magic Numbers & Strings132133### Rules134135- ✅ DO: Extract magic numbers to named constants136- ✅ DO: Use enums for related constants137- ❌ DON'T: Hardcode values that have meaning138139### Examples140141```typescript142// ❌ Bad143if (user.age >= 18) {144}145if (status === 1) {146}147setTimeout(fn, 86400000);148149// ✅ Good150const MINIMUM_AGE = 18;151if (user.age >= MINIMUM_AGE) {152}153154enum OrderStatus {155 Pending = 1,156 Completed = 2,157}158if (status === OrderStatus.Pending) {159}160161const ONE_DAY_MS = 24 * 60 * 60 * 1000;162setTimeout(fn, ONE_DAY_MS);163```164165## Simplicity (KISS)166167### Rules168169- ✅ DO: Choose the simplest solution that works170- ✅ DO: Avoid premature optimization171- ✅ DO: Avoid premature abstraction172- ❌ DON'T: Over-engineer for hypothetical future needs173- ❌ DON'T: Add complexity without clear benefit174175---176> Converted and distributed by [TomeVault](https://tomevault.io/claim/daniel-heydari-dev) — claim your Tome and manage your conversions.177<!-- tomevault:4.0:skill_md:2026-04-14 -->