DEV Community

loading...
Cover image for Code Smell 10 - Too Many Arguments

Code Smell 10 - Too Many Arguments

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

Objects or Functions need too many arguments to work

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.

Exceptions

  • Operations in real world needing not cohesive collaborators.

Sample Code

Wrong

Right

Detection

Most linters warn when the arguments list is too large.

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

Photo by Tobias Tullius on Unsplash

Discussion (0)

pic
Editor guide