Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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.

The shortcut

When you are unsure how to write something, look outward in rings: follow the file, then the project, then the language's guide. Your personal taste comes last.

Adding a book lookup to ShelfwiseStep 1 of 4
  1. Look at the file

    The module already has get_order(order_id) and get_customer(customer_id).

The trap is the "just this once" exception. One fetch_book among forty get_ functions costs every future reader a moment of doubt.

Drills

Answer fast — trust the rule.

quick drill

Shelfwise has get_order and get_customer. What should the new book lookup be called?

quick drill

What is the main benefit of an automatic formatter on a team?

quick drill

In Python, why does indentation matter more than in Java?

quick drill

You join a team whose style differs from your favourite. What do you do in your first change?

Remember

  • Same idea, same word, same shape — everywhere.
  • Follow the file, then the project, then the language guide; your taste comes last.
  • Trap: the "just this once" exception that makes readers doubt every name.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainJoining someone else's codebase

You join the Shelfwise team. Their code uses get_ for lookups, four-space indentation and an automatic formatter. You personally prefer fetch_ and two spaces. Explain how you would write your first new function, and how you would suggest a style change if you really wanted one.