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.
Refactorings βοΈ
Read the articles below for refactorings.
Context π¬
- When you add arguments to make a function work, you encode knowledge in position and order.
- You force your callers to remember rules that belong to the domain.
- When you do this, you move behavior away from meaningful objects, and you replace intent with mechanics.
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 π·οΈ
- Bloaters
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.
Related Reading
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.
