Humans get bored beyond line 10.
TL;DR: Refactor and extract functions longer than 5 lines.
- Low Cohesion
- High coupling
- Difficult to read
- Low Reuse
-
Create small objects dealing with some tasks. Unit-test them.
-
Compose methods
Refactoring 010 - Extract Method Object
- Libraries
<?
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 are larger than a predefined threshold.
Code Smell 75 - Comments Inside a Method
Code Smell 206 - Long Ternaries
- Complexity
Extract the long method into smaller pieces. Break complex algorithms into parts. You can also unit-test these parts.
- Long Method
Photo by Hari Panicker on Unsplash
Programs are meant to be read by humans and only incidentally for computers to execute.
Donald Knuth
Software Engineering Great Quotes
This article is part of the CodeSmell Series.