DEV Community

Cover image for Code Smell 03 - Functions Are Too Long

Code Smell 03 - Functions Are Too Long

Maxi Contieri
Learn something new every day. - I am a senior software engineer working in industry, teaching and writing on software design, SOLID principles, DDD and TDD.
Originally published at Updated on ・1 min read

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


1) Refactor

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

3) Compose methods


  • Libraries

Sample Code



function setUpChessBoard() {

    //A lot of lines
Enter fullscreen mode Exit fullscreen mode



function setUpChessBoard() {
Enter fullscreen mode Exit fullscreen mode


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


Also Known as

  • Long Method

More info


  • Complexity


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


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

Discussion (2)

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."

eduluz1976 profile image
Eduardo Luz

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