Humans get bored after line five.
TL;DR: Refactor and extract functions longer than 5 lines.
When you write a long function, you hide too many details in one place.
You force the reader to hold multiple concepts in mind.
You mix unrelated responsibilities and make the code hard to test.
You create a rigid block that breaks easily when you change it.
Short, focused functions let you read, test, and modify code faster.
<?
function setUpChessBoard() {
$this->placeOnBoard($this->whiteTower);
$this->placeOnBoard($this->whiteKnight);
// A lot more lines
// Empty space to pause definition
$this->placeOnBoard($this->blackTower);
$this->placeOnBoard($this->blackKnight);
// A lot more lines
}
<?
function setUpChessBoard() {
$this->placeWhitePieces();
$this->placeBlackPieces();
}
All linters can measure and warn when methods exceed a predefined threshold.
A real-world action should map to a clear, concise function.
When you pack many actions into one function, you lose that mapping.
Developers must mentally reconstruct the steps, which slows comprehension and increases errors.
AI generators often create long functions if you give them vague prompts.
They tend to cram all logic into one place unless you explicitly request modular code.
AI tools can fix this smell with the right instructions to split code into small, focused functions.
Remember: AI Assistants make lots of mistakes
Suggested Prompt: Convert it to more declarative
Without Proper Instructions |
With Specific Instructions |
---|---|
Extract long methods into smaller pieces.
Break complex algorithms into parts.
You can also unit test these parts.
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xv
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxi
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xlii
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxii
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xv
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxi
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xvii
Photo by Hari Panicker on Unsplash
Programs are meant to be read by humans and only incidentally for computers to execute.
Donald Knuth
This article is part of the CodeSmell Series.