Skip to the workspace
Pebellslearn by building
Clean Code Principles  ›  Lesson

Be Consistent — Names, Indentation and Style

Be consistent 12 minUsing the same words, shapes and formatting for the same ideas everywhere lets readers predict code instead of studying it.

software design architecture / Be Consistent — Names, Indentation and Style

Explore each scene at your own pace.

01 · The rule

The rule

Be Consistent — Names, Indentation and Style

Shelfwise has get_order, fetch_customer and retrieve_book. A new developer asks: do these three words mean three different things? Does fetch call the network while get reads a cache? They don't, but the reader cannot know that without opening all three. Inconsistency makes readers study code they should be able to predict.

The rule

Same idea, same word, same shape — everywhere.

Consistency works on three levels.

Words. Choose one verb per idea and stick to it. If lookups are get_, then every lookup is get_. If a customer is a customer in one file, it is not a client or user in the next.

Shapes. Similar things should be built the same way. If every service class takes its dependencies in the constructor, a new one should too. If errors are raised as exceptions, a new function should not suddenly return -1.

Formatting. Indentation, spacing, line length and quote style. In Python, indentation is not even optional decoration: it decides which block a line belongs to.

for order in unpaid_orders:
    send_reminder(order)
log("reminders sent")   # outside the loop, runs once

Move that last line four spaces to the right and it runs once per order. Most languages ignore indentation, but readers never do.

A style guide (such as PEP 8 for Python) settles the questions, and an automatic formatter applies the answers so nobody argues about spaces in code review. Reviews can then focus on behaviour. A linter goes one step further and flags inconsistencies a formatter cannot fix, such as unused variables or names that break the agreed convention.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.