본문 바로가기
IT & 개발

6개월 후의 나를 위해 코드를 짜다 - 2026년 AI 시대에 더 중요해진 클린코드 5원칙

by 냉국이 2026. 3. 16.
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

댓글