DEV Community

Cover image for Code Smell 03 - Functions Are Too Long
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

4

Code Smell 03 - Functions Are Too Long

Humans get bored beyond line 10.

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

Problems

  • Low Cohesion
  • High coupling
  • Difficult to read
  • Low Reuse

Solutions

1) Refactor

2) Create small objects dealing with some tasks. Unit test them.

3) Compose methods

Examples

  • Libraries

Sample Code

Wrong

<?

function setUpChessBoard() {

    $this->placeOnBoard($this->whiteTower);
    $this->placeOnBoard($this->whiteKnight);
    //A lot of lines
    //.....
    $this->placeOnBoard($this->blackTower);
}
Enter fullscreen mode Exit fullscreen mode

Right

<?

function setUpChessBoard() {
    $this->placeWhitePieces();
    $this->placeBlackPieces();
}
Enter fullscreen mode Exit fullscreen mode

Detection

All linters can measure and warn when methods are larger than a predefined threshold.

Relations

Also Known as

  • Long Method

More info

Tags

  • Complexity

Conclusion

Extract long method into smaller pieces. Break complex algorithms in parts. You can also unit test these parts.

Credits

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.

Last update: 2021/06/01

Dynatrace image

Observability should elevate – not hinder – the developer experience.

Is your troubleshooting toolset diminishing code output? With Dynatrace, developers stay in flow while debugging – reducing downtime and getting back to building faster.

Explore Observability for Developers

Top comments (4)

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

The funny thing is that placeWhitePieces() and placeBlackPieces() will need to call each placement inside which then we'll need to separate into single functions placeKing($this) and get $this->color inside to keep them atomic.
So it's just a matter of ordering the code at the end.

Collapse
 
mcsee profile image
Maxi Contieri

as always :)

Collapse
 
winstonpuckett profile image
Winston Puckett

A developer I really respect said, "a developer can only hold one screen's worth of code at one time." And sets the function limit according to how big the average developer's screens are. I also like the way you said it, "humans get bored beyond line 10."

Collapse
 
eduluz1976 profile image
Eduardo Luz

Sorry, but at first glance I didn't see any difference among the right and wrong code.

Redis image

Short-term memory for faster
AI agents

AI agents struggle with latency and context switching. Redis fixes it with a fast, in-memory layer for short-term context—plus native support for vectors and semi-structured data to keep real-time workflows on track.

Start building

👋 Kindness is contagious

Sign in to DEV to enjoy its full potential—unlock a customized interface with dark mode, personal reading preferences, and more.

Okay