Article 4: Clean Code Principles

Introduction

Clean code is code that's easy to read, understand, and modify. Through years of maintaining codebasesβ€”some clean, some notβ€”I've learned that the time saved by writing "quick and dirty" code is always repaid with interest during maintenance.

This article covers the fundamental principles of writing clean code, including naming conventions, function design, and introductory design patterns that make code self-documenting and maintainable.

What Makes Code Clean?

spinner

Clean code:

  • Reads like prose - Intent is immediately clear

  • Does one thing well - Single responsibility

  • Has no surprises - Behaves as expected

  • Is easily testable - Clear inputs and outputs

  • Minimizes dependencies - Low coupling

Meaningful Names

Variables

# Bad: Cryptic or misleading names
d = 86400  # What is this?
lst = []   # List of what?
temp = get_user()  # Not temporary!

# Good: Intention-revealing names
seconds_per_day = 86400
active_users: list[User] = []
current_user = get_user()

Naming Conventions

Type
Convention
Example

Variables

snake_case

user_count, is_active

Constants

UPPER_SNAKE_CASE

MAX_RETRIES, API_URL

Functions

snake_case (verb)

get_user(), calculate_total()

Classes

PascalCase (noun)

UserAccount, TaskManager

Modules

snake_case

user_service.py, data_models.py

Private

leading underscore

_internal_method(), _cache

Functions

Classes

Boolean Naming

Function Design

Small Functions

Functions should do one thing and do it well:

Function Arguments

Avoid Flag Arguments

Command-Query Separation

Functions should either do something (command) or return something (query), not both:

Code Organization

Vertical Formatting

Module Organization

Comments and Documentation

When to Comment

Self-Documenting Code

Docstrings

Introduction to Design Patterns

Design patterns are reusable solutions to common problems. Here are foundational patterns you'll use frequently.

Factory Pattern

Creates objects without exposing creation logic:

Strategy Pattern

Enables selecting algorithms at runtime:

Repository Pattern

Abstracts data access logic:

Code Smells and Refactoring

Long Method

Duplicated Code

Magic Numbers

Practical Exercise

Exercise 1: Refactor Messy Code

Exercise 2: Apply Factory Pattern

Clean Code Checklist

Use this checklist during code reviews:

Key Takeaways

  1. Names matter - Spend time choosing clear, intention-revealing names

  2. Keep functions small - One function, one responsibility

  3. Avoid comments by writing clearer code - Comments are often a code smell

  4. Design patterns solve recurring problems - Learn Factory, Strategy, Repository

  5. Refactor continuously - Clean code is a practice, not a destination

What's Next?

Clean code principles give you the foundation. In Article 5: SOLID Principles, we'll dive deeper into object-oriented design principles and advanced patterns that help build flexible, maintainable systems.


This article is part of the Software Engineering 101 series.

Last updated