DEV Community

Cover image for Code Smell 169 - Glued Methods
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

4

Code Smell 169 - Glued Methods

Don't make two or more things at once.

TL;DR: Try to be as atomic as possible in your methods

Problems

  • Coupled Code

  • Harder to test

  • Harder to read

Solutions

  1. Break the method

Refactorings

Context

If you name a method with 'And' you are probably missing an extract-and-break method opportunity.

Sample Code

Wrong


calculatePrimeFactorsRemoveDuplicatesAndPrintThem()

// Three responsibilities

Enter fullscreen mode Exit fullscreen mode

Right


calculatePrimeFactors();

removeDuplicates();

printNumbers();

// Three diferent methods

// We can test them and reuse them

Enter fullscreen mode Exit fullscreen mode

Detection

[X] Semi-Automatic

Some linters can warn us about methods including the term 'and'.

Tags

  • Coupling

Conclusion

When making methods, it is very important to play some rubber duck story and tell ourselves if we are making things right.

Relations

Disclaimer

Code Smells are just my opinion.

Credits

Photo by Scott Sanker on Unsplash


Learning the art of programming, like most other disciplines, consists of first learning the rules and then learning when to break them.

Joshua Bloch


This article is part of the CodeSmell Series.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay