Too many arguments in method calls

Recently, I was torn up when I tried to write classes regarding the number of requested parameters.

An example of a simple simple constructor:

Burger(bun, meat, cheese, lettuce) this.bun = bun this.meat = meat ... 

Vs

 Burger(grocery) this.bun = grocery.bun this.meat = grocery.meat ... 

Both methods are valid approaches. The first method shows what exactly happens in a hamburger, breaks down the arguments into more general classes, so it reduces cohesion, and it seems to me easier to check, because the graph of objects is simpler.

But the second method is much simpler, cleaner, and perhaps a hamburger may require much more ingredients, then the arguments in the first case can be inflated without any problems.

I would like to know which method would be recommended in such a situation? Go for a cleaner, but more related code, or a more verbose way.

+4
source share
1 answer

passing 4+ parameters to any method or constructor is not a good idea or a good design.

I remember that Joshua Bloch ( Effective Java ) recommend Builder Pattern to this situation (point 2)

Point 2 : consider the constructor, faced with many parameters of the constructor

+5
source

Source: https://habr.com/ru/post/1334626/


All Articles