Code Smell 03 - Functions Are Too Long

Code Smell 03 - Functions Are Too Long

Leader posted 6 min read

Humans get bored after line five.

TL;DR: Refactor and extract functions longer than five 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

https://maximilianocontieri.com/refactoring-025-decompose-regular-expressions

https://maximilianocontieri.com/refactoring-002-extract-method

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

[Gist Url]: # (https://gist.github.com/mcsee/1f12fb2d0cb9f8eea202526597cf4b83)

<?

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
}

[Gist Url]: # (https://gist.github.com/mcsee/0f66ce8c2bba8990e44a36495fa4c3e1)

<?

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: Break long functions using extract method refactoring

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://maximilianocontieri.com/code-smell-75-comments-inside-a-method

https://maximilianocontieri.com/code-smell-102-arrow-code

https://maximilianocontieri.com/code-smell-206-long-ternaries

https://maximilianocontieri.com/code-smell-107-variables-reuse

https://maximilianocontieri.com/code-smell-74-empty-lines

https://maximilianocontieri.com/code-smell-154-too-many-variables

https://maximilianocontieri.com/code-smell-83-variables-reassignment

More Information

https://refactoring.guru/es/smells/long-method

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://maximilianocontieri.com/software-engineering-great-quotes


This article is part of the CodeSmell Series.

https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code

1 Comment

1 vote

More Posts

Code Smell 10 - Too Many Arguments

Maxi Contieri - Dec 20, 2025

Code Smell 06 - Too Clever Programmer

Maxi Contieri - Dec 5, 2025

Code Smell 17 - Global Functions

Maxi Contieri - Feb 28

Code Smell 319 - Hardcoded Stateless Properties

Maxi Contieri - Apr 9

Code Smell 16 - Ripple Effect

Maxi Contieri - Feb 19
chevron_left

Related Jobs

Commenters (This Week)

4 comments
2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!