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

Break the method

Refactorings


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Maxi Contieri

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

Right


calculatePrimeFactors();

removeDuplicates();

printNumbers();

// Three diferent methods

// We can test them and reuse them

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.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Maxi Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maxi Contieri | Sciencx (2022-10-15T01:46:12+00:00) Code Smell 169 – Glued Methods. Retrieved from https://www.scien.cx/2022/10/15/code-smell-169-glued-methods/

MLA
" » Code Smell 169 – Glued Methods." Maxi Contieri | Sciencx - Saturday October 15, 2022, https://www.scien.cx/2022/10/15/code-smell-169-glued-methods/
HARVARD
Maxi Contieri | Sciencx Saturday October 15, 2022 » Code Smell 169 – Glued Methods., viewed ,<https://www.scien.cx/2022/10/15/code-smell-169-glued-methods/>
VANCOUVER
Maxi Contieri | Sciencx - » Code Smell 169 – Glued Methods. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/15/code-smell-169-glued-methods/
CHICAGO
" » Code Smell 169 – Glued Methods." Maxi Contieri | Sciencx - Accessed . https://www.scien.cx/2022/10/15/code-smell-169-glued-methods/
IEEE
" » Code Smell 169 – Glued Methods." Maxi Contieri | Sciencx [Online]. Available: https://www.scien.cx/2022/10/15/code-smell-169-glued-methods/. [Accessed: ]
rf:citation
» Code Smell 169 – Glued Methods | Maxi Contieri | Sciencx | https://www.scien.cx/2022/10/15/code-smell-169-glued-methods/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.