Objects or Functions need too many arguments to work

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

Problems πŸ˜”

Solutions πŸ˜ƒ

  1. Find cohesive relations among arguments
  2. Create a "context".
  3. Consider using a Method Object Pattern.
  4. Avoid "basic" Types: strings, arrays, integers, etc. Think on objects.

Refactorings βš™οΈ

Read the articles below for refactorings.



Context πŸ’¬

Sample Code πŸ“–

Wrong 🚫

public class Printer {   
  void print(String documentToPrint, 
           String papersize,
           String orientation, 
           boolean grayscales,
           int pagefrom,
           int pageTo,
           int copies,
           float marginLeft,
           float marginRight,
           float marginTop,
           float marginBottom         
        ) {
    }
}

Right πŸ‘‰

final public class PaperSize { }
final public class Document { }
final public class PrintMargins { }
final public class PrintRange { }  
final public class ColorConfiguration { }
final public class PrintOrientation { }
// Class definition with methods and properties omitted for simplicity

final public class PrintSetup {
    public PrintSetup(PaperSize papersize,
           PrintOrientation orientation, 
           ColorConfiguration color,
           PrintRange range,
           int copiesCount,
           PrintMargins margins
           ) {}
}

final public class Printer {   
  void print(
         Document documentToPrint, 
         PrintSetup setup        
        ) {
    }
}

Detection πŸ”

Most linters warn when the arguments list is too large.

You can also detect this smell when a function signature grows over time.

Exceptions πŸ›‘

Operations in real-world needing not cohesive collaborators.

Some low-level functions mirror external APIs or system calls.

In those cases, argument lists reflect constraints you cannot control.

Tags 🏷️

Level πŸ”‹

[X] Beginner

Why the Bijection Is Important πŸ—ΊοΈ

Good design keeps a clear bijection between concepts in the program and concepts in the MAPPER.

When you spread a concept across many arguments, you break that mapping.

You force callers to assemble meaning manually, and the model stops representing the domain.

AI Generation πŸ€–

AI generators often create this smell.

They optimize for quick success and keep adding parameters instead of creating new abstractions.

AI Detection 🧲

AI generators can fix this smell when you ask for value objects or domain concepts explicitly.


Try Them! πŸ› 

Remember: AI Assistants make lots of mistakes

Suggested Prompt: Refactor this function by grouping related parameters into meaningful domain objects and reduce the argument list to one parameter


Steal My Prompts (Without Proper Instructions)πŸ“΅

Steal My Prompts (With Specific Instructions) πŸ‘©β€πŸ«

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.

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-vii-8dk31x0

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-iii-t7h3zkv

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xviii


Lead photo by Tobias Tullius on Unsplash

This article is part of the CodeSmell Series.