Long method (ARCH2180)
Key python.quality.long-method · Severity info · Category quality
Overly long functions carry multiple responsibilities and become hard to read and test. Split the logic into small, named helper functions.
Incorrect
Section titled “Incorrect”def process_order(order): total = 0 for item in order.items: total += item.price * item.qty discount = total * 0.1 if total > 100 else 0 total -= discount tax = total * 0.18 total += tax send_email(order.user, total) log.info("processed") return totalCorrect
Section titled “Correct”def process_order(order): total = _subtotal(order) - _discount(order) total += total * 0.18 _notify(order.user, total) return total
def _subtotal(order): return sum(i.price * i.qty for i in order.items)Break the function into small single-responsibility helpers along its logical steps.