Code Smell 03: Functions Are Too Long – Here’s How to Fix That

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


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri

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 ⚙️

https://maximilianocontieri.com/refactoring-010-extract-method-object?embedable=true

https://hackernoon.com/refactoring-025-decompose-regular-expressions?embedable=true

https://hackernoon.com/improving-the-code-one-line-at-a-time?embedable=true

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
}
<?

function setUpChessBoard() {
    $this->placeWhitePieces();
    $this->placeBlackPieces();
}

Detection 🔍

  • [x] Automatic

All linters can measure and warn when methods exceed a predefined threshold.

Tags 🏷️

  • Bloaters

Level 🔋

  • [x] Beginner

Why the Bijection Is Important 🗺️

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 Generation 🤖

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 Detection 🥃

AI tools can fix this smell with the right instructions to split code into small, focused functions.

Try Them! 🛠

Remember: AI Assistants make lots of mistakes

Suggested Prompt: Convert it to more declarative

| Without Proper Instructions | With Specific Instructions | |----|----| | ChatGPT | ChatGPT | | Claude | Claude | | Perplexity | Perplexity | | Copilot | Copilot | | You | You | | Gemini | Gemini | | DeepSeek | DeepSeek | | Meta AI | Meta AI | | Grok | Grok | | Qwen | Qwen |

Conclusion 🏁

Extract long methods into smaller pieces.

\ Break complex algorithms into parts.

\ You can also unit test these parts.

Relations 👩‍❤️‍💋‍👨

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

More Information 📕

https://refactoring.guru/es/smells/long-method?embedable=true

Also Known as

  • Long Method

Credits 🙏

Photo by Hari Panicker on Unsplash


Programs are meant to be read by humans and only incidentally for computers to execute.

Donald Knuth

https://hackernoon.com/400-thought-provoking-software-engineering-quotes?embedable=true


This article is part of the CodeSmell Series.

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-i-xqz3evd?embedable=true

\


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2025-08-20T13:00:08+00:00) Code Smell 03: Functions Are Too Long – Here’s How to Fix That. Retrieved from https://www.scien.cx/2025/08/20/code-smell-03-functions-are-too-long-heres-how-to-fix-that/

MLA
" » Code Smell 03: Functions Are Too Long – Here’s How to Fix That." Maximiliano Contieri | Sciencx - Wednesday August 20, 2025, https://www.scien.cx/2025/08/20/code-smell-03-functions-are-too-long-heres-how-to-fix-that/
HARVARD
Maximiliano Contieri | Sciencx Wednesday August 20, 2025 » Code Smell 03: Functions Are Too Long – Here’s How to Fix That., viewed ,<https://www.scien.cx/2025/08/20/code-smell-03-functions-are-too-long-heres-how-to-fix-that/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Code Smell 03: Functions Are Too Long – Here’s How to Fix That. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/08/20/code-smell-03-functions-are-too-long-heres-how-to-fix-that/
CHICAGO
" » Code Smell 03: Functions Are Too Long – Here’s How to Fix That." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2025/08/20/code-smell-03-functions-are-too-long-heres-how-to-fix-that/
IEEE
" » Code Smell 03: Functions Are Too Long – Here’s How to Fix That." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2025/08/20/code-smell-03-functions-are-too-long-heres-how-to-fix-that/. [Accessed: ]
rf:citation
» Code Smell 03: Functions Are Too Long – Here’s How to Fix That | Maximiliano Contieri | Sciencx | https://www.scien.cx/2025/08/20/code-smell-03-functions-are-too-long-heres-how-to-fix-that/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.