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.

↑ All of shift leftShift left, explainedHow software solved itSyntax treesPure functionsTests, coverage, complexityTesting data logic offlineClean codeSecurity and costWhat is missingThe bundleHow it is builtAll the checksOpen source and ours

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.05

Checked 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 habitRuff rulesOriginally from
Exit early; no else after returnRET505 to RET508, SIM102 (collapsible if), PLR1702 (too many nested blocks)flake8-return, flake8-simplify, pylint
Small functionsC901, PLR0911 (returns), PLR0912 (branches), PLR0913 (arguments), PLR0915 (statements)mccabe, pylint
Simpler formsSIM (ternary, any/all, context managers), FURB, UP, PERFflake8-simplify, refurb, pyupgrade, perflint
Likely bugsB (mutable default argument, loop variable captured late), E722 (bare except), TRY, BLEbugbear, tryceratops
Dead weightARG (unused arguments), ERA (commented-out code), F401 (unused imports); vulture finds dead functionsflake8-unused-arguments, eradicate
Names and docsN (PEP 8 naming), D (docstrings on public functions)pep8-naming, pydocstyle
Readability scorecomplexipy cognitive complexity, which punishes nesting
SQL clean codeSQLFluff: 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