Long Method — Code Smells Catalog Skip to content

Long Method

Also known as: Complex Method, God Method, Brain Method

Bloaters Measured Smells AntipatternCode SmellDesign SmellImplementation Smell Within Class

A method so long you scroll past the beginning before reaching the end. Every change requires re-reading the whole thing, every piece of logic is trapped inside, and the side effects could be...

2 min read 1 source

Overview

One of the most apparent complications developers can encounter in the code is the length of a method. The more lines of code a function has, the more the developer has to strain himself mentally to comprehend what the particular block of code does thoroughly. The longer a procedure is, the more difficult it is to understand it [1]. It is also harder to change or extend [2]. In addition, reading more lines requires more time, which quickly adds up because the code is read more than it is written [3]. Fowler strongly believes in short methods as a better option.

Causation

The author adds another code line rather than breaking the flow to identify the helper objects [4].

Problems

👁
Hard to Read and Reason About

  • Every time a developer wants to make any change to any part of it, he has to grasp the whole thing every time, which is time-consuming.

Low Reuse

  • Longer methods most likely have more functionalities, so developers cannot reuse them as easily as methods that are short and specific

💥
Side Effects

  • If a method is long, it is not very likely that it does only what the name of the method could indicate.

Refactoring

  • Extract Method
  • Replace Conditional with Polymorphism
  • Replace Method with Command
  • Introduce Parameter Object
  • Preserve the Whole Object
  • Split Loop

Sources

Browse All 56 Smells