Anti-Patterns

Dirty Code Anti-Patterns: What Bad Code Actually Looks Like

Exposing the most common dirty code anti-patterns — cryptic names, bloated functions, hidden side effects, and copy-paste disasters. Learn what not to do.

Writing clean code isn’t just about making your code work—it’s about making it understandable, maintainable, and a joy to work with. Here are the essential principles every developer should internalize.

Why Clean Code Matters

Clean code is the foundation of sustainable software development. When code is clean, it’s:

  • Easier to understand - New team members can onboard faster
  • Easier to modify - Changes require less effort and introduce fewer bugs
  • Easier to test - Well-structured code is naturally more testable
  • Less costly - Technical debt accumulates slower

The Core Principles

1. Meaningful Names

Names should reveal intent. A variable name should tell you why it exists, what it does, and how it’s used.

# Bad
d = 86400  # elapsed time in seconds

# Good
SECONDS_PER_DAY = 86400

Choose names that are:

  • Pronounceable - You should be able to discuss them in conversation
  • Searchable - Single-letter names are hard to find
  • Consistent - Use the same concept throughout the codebase

2. Functions Should Do One Thing

The Single Responsibility Principle (SRP) states that a function should have one, and only one, reason to change.

# Bad - does multiple things
def process_user_data(user):
    validate_email(user.email)
    hash_password(user.password)
    save_to_database(user)
    send_welcome_email(user)
    log_user_creation(user)

# Good - each function has one responsibility
def create_user(user_data):
    user = validate_and_build_user(user_data)
    save_user(user)
    notify_user_created(user)

3. Keep Functions Small

Functions should be small. How small? Smaller than that.

A good rule of thumb:

  • 20 lines or less is a good target
  • One level of abstraction per function
  • One level of indentation where possible

4. Avoid Side Effects

Pure functions are easier to test and reason about. They take inputs and return outputs without modifying external state.

# Bad - side effect hidden in function name
def check_password(password):
    if is_valid(password):
        initialize_session()  # Unexpected side effect!
        return True
    return False

# Good - explicit about what it does
def validate_password(password):
    return is_valid(password)

def create_authenticated_session(user):
    if validate_password(user.password):
        return Session(user)
    raise AuthenticationError()

5. Don’t Repeat Yourself (DRY)

Duplication is the root of all evil in software. Every piece of knowledge should have a single, unambiguous representation.

However, be careful not to DRY prematurely—wait until you see the pattern three times before abstracting.

6. Comments Are a Last Resort

Good code is self-documenting. Comments should explain why, not what.

# Bad - explains what the code does
# Increment i by 1
i += 1

# Good - explains why
# Compensate for zero-based indexing in user-facing display
display_index = internal_index + 1

Valid reasons for comments:

  • Legal requirements
  • Explaining intent behind non-obvious decisions
  • Warnings about consequences
  • TODO notes (but address them!)

Practical Tips for Writing Cleaner Code

  1. Write tests first - TDD naturally leads to cleaner, more modular code
  2. Refactor continuously - Clean up as you go, not in big batches
  3. Read more code - Study well-written open source projects
  4. Code review - Fresh eyes catch issues you’ve become blind to
  5. Take breaks - Tired developers write messy code

The Boy Scout Rule

“Leave the campground cleaner than you found it.”

Every time you touch code, leave it a little better than you found it. Small improvements compound over time.

Conclusion

Clean code is not about perfection—it’s about professionalism. It’s about caring enough to do your job well and respecting your future self and teammates who will maintain this code.

Start applying these principles today, one small improvement at a time. Your future self will thank you.


What clean code principles have had the biggest impact on your work? Share your thoughts in the comments below.