DEV Community

loading...
Cover image for Code Smell 03 - Functions Are Too Long

Code Smell 03 - Functions Are Too Long

mcsee profile image Maxi Contieri Originally published at maximilianocontieri.com Updated on ・1 min read

Humans get bored beyond line 10.

Problems

  • Low Cohesion
  • High coupling
  • Difficult to read

Solutions

1) Refactor

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

3) Compose methods

Examples

  • Libraries

Sample Code

Wrong

Right

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

Credits

Photo by Hari Panicker on Unsplash


This article is part of the CodeSmell Series.

Discussion (2)

pic
Editor guide
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.