728x90
6개월 전에 내가 짠 코드를 다시 열어봤을 때, 나는 잠시 멈춰다. 변수명은 data, temp, result로 가듩했고, 200줄짜리 함수는 HTTP 요청 처리, DB 쿼리, 비즈니스 로직, 응답 포맷팅을 한 방에 다 했다. 그날 이후로 나는 코드를 "6개월 후의 나"를 위해 쓰기 시작했다.
클린 코드는 미래의 나에게 보내는 편지다
Robert C. Martin(Uncle Bob)은 클린 코드에서 이렇게 말한다. "코드를 읽는 시간 대 코드를 쓰는 시간의 비율은 10:1이다."
2026년, AI가 코드를 자동으로 생성하는 시대에 클린 코드는 더 중요해졌다. AI가 만든 스파게티 코드를 정리하는 능력이 오히려 핵심 역량이 두다.
원칙 1: 함수는 한 가지 일만 한다 (SRP)
// Bad: 하나의 함수가 너무 많은 일을 함
async function processUserOrder(userId, cartItems) {
const user = await db.query('SELECT * FROM users WHERE id = ?', [userId]);
if (user.balance < calculateTotal(cartItems)) throw new Error('잔액 부족');
await db.query('UPDATE users SET balance = balance - ?', [calculateTotal(cartItems)]);
await sendEmail(user.email, '주문 완료');
await updateInventory(cartItems);
return { success: true };
}
// Good: 각 책임을 분리
async function processUserOrder(userId, cartItems) {
const user = await getUserById(userId);
const total = calculateOrderTotal(cartItems);
await validateUserBalance(user, total);
await chargeUser(userId, total);
await notifyOrderCompletion(user.email);
await updateInventory(cartItems);
return createOrderResponse(cartItems);
}
원칙 2: 의미 있는 이름을 써라
# Bad
def calc(d, r):
return d * (1 - r/100)
# Good
def calculate_discounted_price(original_price, discount_percentage):
return original_price * (1 - discount_percentage / 100)
원칙 3: 주석보다 코드 자체를 읽기 쿠게
# Bad
if user.type == 2 and (datetime.now() - user.created_at).days > 30:
pass
# Good
PREMIUM_ACCOUNT_TYPE = 2
MINIMUM_ACCOUNT_AGE_DAYS = 30
def is_established_premium_user(user):
account_age = (datetime.now() - user.created_at).days
return user.type == PREMIUM_ACCOUNT_TYPE and account_age > MINIMUM_ACCOUNT_AGE_DAYS
원칙 4: 에러를 값처럼 다렸라
type Result<T, E> = { ok: true; value: T } | { ok: false; error: E };
function parseJsonSafely(input: string): Result<unknown, string> {
try {
return { ok: true, value: JSON.parse(input) };
} catch (e) {
return { ok: false, error: 'JSON 파싱 실패: ' + (e as Error).message };
}
}
원칙 5: 테스트 이름 자체가 명세다
class TestCalculateDiscountedPrice:
def test_10_percent_discount_reduces_price_by_tenth(self):
assert calculate_discounted_price(10000, 10) == 9000
def test_zero_discount_returns_original_price(self):
assert calculate_discounted_price(10000, 0) == 10000
def test_negative_discount_raises_value_error(self):
with pytest.raises(ValueError):
calculate_discounted_price(10000, -10)
클린 코드는 개발 속도를 늦춰지 않는다, 오히려 빠르게 만든다
클린 코드는 현재의 나를 위한 것이 아니라 팀의 미래를 위한 투자다.
함께 읽으면 좋은 글
300x250
'IT & 개발' 카테고리의 다른 글
| 벡터 데이터베이스 완전 정복 - RAG 구현을 위한 Pinecone vs Qdrant vs Milvus 실전 비교 (0) | 2026.03.16 |
|---|---|
| 267개 AI 모델의 충격 - 2026년 3월 에이전틱 AI가 IT 판세를 뒤흔다 (0) | 2026.03.16 |
| 주니어 개발자가 사라지는 시대 - AI와 경쟁 말고 협업하는 2026년 콜리어 전략 (0) | 2026.03.16 |
| React Server Components 완전 정복 - 기존 방식과 코드 비교로 배우는 RSC 실전 가이드 (0) | 2026.03.13 |
| GitOps with ArgoCD 실전 구축 가이드 - Kubernetes 배포 자동화로 월 장애 건수 80% 줄이기 (0) | 2026.03.13 |
댓글