cover-img

Code Smell 10 - Too Many Arguments

Objects or Functions need too many arguments to work.

29 September, 2022

6

6

0

Objects or Functions need too many arguments to work.

TL;DR: Don't pass more than three arguments to your functions.

Problems

Low maintainability

Low Reuse

Coupling

Solutions

Find cohesive relations among arguments

Create a "context".

Consider using a Method Object Pattern.

Avoid "basic" Types: strings, arrays, integers, etc. Think on objects.

Sample Code

Wrong

Right

Detection

Most linters warn when the arguments list is too large.

Exceptions

Operations in real world needing not cohesive collaborators.

Tags

primitive

Conclusion

Relate arguments and group them. Always favor real world mappings. Find in real world how to group the arguments in cohesive objects.
If a function gets too many arguments, some of them might be related to the class construction. This is a design smell too.

Relations

Credits

This article is part of the CodeSmell Series.

programming

refactoring

code

cleancode

smells

6

6

0

programming

refactoring

code

cleancode

smells

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.