Home Projects Blog About Contact
Download CV
Back to Blog

Clean Code Principles Every Developer Should Know

Practical principles for writing code that is readable, maintainable, and elegant — applicable to any programming language.

Clean Code Principles Every Developer Should Know

Writing code is easy. Writing clean code is an art. After years of reading and writing code, here are the principles I follow in every project.

1. Meaningful Names

The name of a variable, function, or class should tell you why it exists, what it does, and how it’s used.

# ❌ Bad
d = 86400

# ✅ Good
SECONDS_IN_A_DAY = 86400
// ❌ Bad
function calc(a, b) {
  return a * b * 0.21;
}

// ✅ Good
function calculateTax(price, quantity) {
  const TAX_RATE = 0.21;
  return price * quantity * TAX_RATE;
}

2. Functions Should Do One Thing

A function should do one thing, do it well, and do it only.

# ❌ Does too many things
def process_user(user):
    validate_email(user.email)
    hash_password(user.password)
    save_to_database(user)
    send_welcome_email(user.email)

# ✅ Each function has one job
def register_user(user):
    validated = validate_user(user)
    saved = save_user(validated)
    notify_user(saved)

3. Avoid Comments — Write Self-Documenting Code

If your code needs comments to explain what it does, it needs to be rewritten. Comments should explain why, not what.

// ❌ Unnecessary comment
// Check if user is over 18
if (user.age > 18) { ... }

// ✅ Self-documenting
const isAdult = user.age > 18;
if (isAdult) { ... }

// ✅ Good comment — explains WHY
// Tax exemption applies to non-profit organizations per regulation 2024/15
if (organization.isNonProfit) { ... }

4. DRY — Don’t Repeat Yourself

Every piece of knowledge should have a single, unambiguous representation within a system.

// ❌ Repetition
function getAdminUsers() {
  return users.filter(u => u.role === 'admin' && u.active);
}
function getActiveAdmins() {
  return users.filter(u => u.role === 'admin' && u.active);
}

// ✅ Single source of truth
const isActiveAdmin = (user: User) =>
  user.role === 'admin' && user.active;

function getAdminUsers() {
  return users.filter(isActiveAdmin);
}

5. The Boy Scout Rule

“Leave the campground cleaner than you found it.”

Every time you touch a file, leave it a little better than you found it. Rename a confusing variable. Extract a helper function. Remove dead code.

6. Error Handling

Don’t ignore errors. Don’t use exceptions for control flow. Be specific.

# ❌ Bad — catches everything silently
try:
    process(data)
except:
    pass

# ✅ Good — specific and informative
try:
    result = process(data)
except ValidationError as e:
    logger.warning(f"Invalid data: {e}")
    return default_result()
except ConnectionError as e:
    logger.error(f"Service unavailable: {e}")
    raise

7. Keep It Simple

“Simplicity is the ultimate sophistication.” — Leonardo da Vinci

The simplest solution that works is usually the best one. Don’t over-engineer.

ApproachLinesBugsMaintenance
Over-engineered500ManyHard
Simple50FewEasy
Too simple10SomeRisky

Aim for the sweet spot — simple but not simplistic.

Conclusion

Clean code isn’t about following rules blindly. It’s about empathy — writing code that your future self (and your teammates) will thank you for.

Start with one principle this week. Apply it consistently. Then add another. Over time, clean code becomes second nature.