Home › Learn › Shift left › Clean code
Shift left · page 7 of 13
Clean-code checks: early return and the rest
Clean-code linting for Python and SQL: early return instead of nested ifs, small functions, dead code, naming. Which Ruff rule sets to switch on.
In plain words
Early return beats nesting
Each level of nesting is one more condition a reader must hold in their head. Returning early for the simple cases keeps the main path flat.
# Nested: three levels, and the reader tracks every else
def discount(order):
if order is not None:
if order.total > 100:
if order.customer.is_member:
return 0.10
else:
return 0.05
else:
return 0
else:
return 0
# Early return: each rule is one line, and nothing is nested
def discount(order):
if order is None or order.total <= 100:
return 0
if order.customer.is_member:
return 0.10
return 0.05Checked on 2 Nov 2026: Ruff reports the else after return (RET505), and complexipy scores the nested version 9 and the early-return version 3.
Already built
Ruff has the clean-code rules; switch them on
Ruff is one fast tool that reimplements the main Python linters. You choose which rule sets to turn on.
| Clean-code habit | Ruff rules | Originally from |
|---|---|---|
| Exit early; no else after return | RET505 to RET508, SIM102 (collapsible if), PLR1702 (too many nested blocks) | flake8-return, flake8-simplify, pylint |
| Small functions | C901, PLR0911 (returns), PLR0912 (branches), PLR0913 (arguments), PLR0915 (statements) | mccabe, pylint |
| Simpler forms | SIM (ternary, any/all, context managers), FURB, UP, PERF | flake8-simplify, refurb, pyupgrade, perflint |
| Likely bugs | B (mutable default argument, loop variable captured late), E722 (bare except), TRY, BLE | bugbear, tryceratops |
| Dead weight | ARG (unused arguments), ERA (commented-out code), F401 (unused imports); vulture finds dead functions | flake8-unused-arguments, eradicate |
| Names and docs | N (PEP 8 naming), D (docstrings on public functions) | pep8-naming, pydocstyle |
| Readability score | complexipy cognitive complexity, which punishes nesting | |
| SQL clean code | SQLFluff: explicit join type, aliases, ambiguous columns, consistent case |
A stricter option exists, wemake-python-styleguide, but it is noisy, so it is not the default. Clean-code findings start as warnings, and the safe ones are fixed automatically with ruff check --fix.
FAQ
Questions people ask
Is there a linter that checks Python clean-code practices like early return?
Yes. Ruff includes flake8-return (RET505: else after return), flake8-simplify (SIM102: collapsible if) and pylint's refactor rules (too many branches, arguments, returns and nested blocks).
Ruff or pylint?
Ruff for the default: it is much faster and includes most of pylint's useful rules. Add pylint only for a specific check Ruff lacks.
Should clean-code findings block a merge?
Not at the start. Warn, fix the safe ones automatically, and tighten later. Blocking on style early makes teams switch the checks off.
Next
Where to go next
Next page: Security, secrets, vulnerable libraries and cost
↑ All of shift left · The repo on GitHub · Data project review