How do you write clean code?
Clean code is code a reader understands quickly. Use names that say what things are, keep functions and lines short, write comments that explain why, and stay consistent with the code around you. PEP 8 gives concrete limits for Python.
Last updated November 2026
What does "clean code" actually mean?
Clean code means the next reader can understand it without asking you. PEP 8 grounds this in one observation: code is read much more often than it is written [1]. Every choice below, from names to line length, serves that reader. Clean code is not clever code, and it is not code with no flaws.
Google's code review standard makes the same point from the other side: there is no perfect code, only better code [2]. Aim for clearer than before, not flawless.
What does clean code look like in practice?
A short example shows most of the ideas at once. The first version works. The second does the same thing, but a reader can see what it does at a glance. This is our own illustration, not code from a source.
Before:
def f(l):
t=0
for x in l:
t+=x
return t/len(l) if l else 0
After:
def average(prices):
"""Return the mean price, or 0 for an empty list."""
if not prices:
return 0
return sum(prices) / len(prices)
What changed: the names say what the data is, the indent is a steady 4 spaces with no tab, the empty case is handled first, and a one-line docstring says what the function returns. The before version also mixes a tab into a space-indented block, which Python does not allow [1]. Paste it into the PEP 8 snippet check to see the notes.
Which clean code tips have the biggest payoff?
Names, short units and consistency give the most for the least effort. The table separates PEP 8 rules from our own practice-based advice.
| Tip | What to do | Basis |
|---|---|---|
| Short lines | Keep lines to 79 characters | PEP 8 [1] |
| Short comments | Keep comments and docstrings to 72 characters | PEP 8 [1] |
| Steady indentation | 4 spaces per level, no tab mixing | PEP 8 [1] |
| Local consistency | Match the function and module first | PEP 8 [1] |
| Descriptive names | Name by meaning: prices, not l | Our advice |
| One job per function | If you need "and" to describe it, split it | Our advice |
| Early returns | Handle empty or invalid input first | Our advice |
| Why-comments | Explain intent; the code already shows what | Our advice |
How should you name things?
Name things by what they mean in the problem, not by their type or by a single letter. unpaid_invoices tells the reader more than list2. This is our advice; each language and project has its own case conventions, and PEP 8's consistency rule says to follow the code around you first [1].
- Variables: nouns that describe the content (
total_cost). - Functions: verbs that describe the action (
send_receipt). - Booleans: read like a yes or no question (
is_paid). - Avoid abbreviations your teammates would have to guess.
When should you write a comment?
Write a comment when the code cannot explain itself: why a choice was made, what a workaround protects against, or what a surprising value means. If a comment only repeats the code, rename things until the comment is unnecessary. In Python, keep each comment line within PEP 8's 72-character limit [1].
When is it fine to break the rules?
When following a rule would make the code harder to read. PEP 8 says so directly: know when to be inconsistent, and use judgment when a guideline does not apply [1]. A common case is old code that already uses a different style. Matching it beats a half-converted file.
Common mistakes
- Over-commenting. A comment on every line buries the few that matter.
- Clever one-liners. Packing logic into one line saves space and costs reading time.
- Renaming everything in an unrelated change. Reviewers cannot see the real change.
- Chasing perfect. Better is the standard, not perfect [2].
- Ignoring editor warnings. Our editor list helps you find one that supports your language.
Summary
- Write for the next reader.
- Name by meaning; describe actions with verbs.
- Keep Python lines to 79 characters and comments to 72.
- Comment the why, not the what.
- Stay consistent with nearby code, and use judgment.
Related: programming best practices and debugging tips.
Sources
- Python Software Foundation, PEP 8: Style Guide for Python Code (2001-07, viewed 2026-11)
- Google Engineering Practices, The Standard of Code Review (undated)