Tech »  Topic »  Code Smell 03: Functions Are Too Long - Here's How to Fix That

Code Smell 03: Functions Are Too Long - Here's How to Fix That


by Maximiliano Contieri August 20th, 2025

Refactor and extract functions longer than 5 lines. Here's how.

Humans get bored after line five.

TL;DR: Refactor and extract functions longer than 5 lines.

Problems 😔

  • Low cohesion
  • High coupling
  • Hard to read
  • Low reusability

Solutions 😃

  1. Refactor
  2. Create small objects to handle specific tasks. Unit-test them.
  3. Compose methods
  4. Divide and conquer

Refactorings ⚙️

Examples

  • Libraries

Context 💬

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.

Sample Code 📖

Wrong 🚫



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
}

Right 👉



function setUpChessBoard ...

Copyright of this story solely belongs to hackernoon.com . To see the full text click HERE