I often end up with methods with long number of arguments (up to 5 arguments). for ex -
public void doSomething(obj1, obj2, obj3, obj4, obj5)
obj1 to obj5 are unrelated to each other
I thought of creating a Bigger Object obj
containing obj1
to obj5
then pass only obj
object that is -
public void doSomething(obj)
But obj
is only a convenient object here and does not have to have obj1
to obj5
as it member variables.
Is it ok to use such convenient object or is there any other approach that I can leverage?
Updates
To answer about SRP, method doSomething
calls some other methods which are responsible for their operation. Hence doSomething
looks as -
public void doSomething(obj1, obj2, obj3, obj4, obj5) {
doSomethin1(obj1)
.doSomethin2(obj2)
.doSomethin3(obj3)
.doSomethin4(obj4)
.doSomethin5(obj5)
}
I was chaining and calling of these methods individually in tests but that seems code repetition to me and decided to create one method instead which takes all of the required arguments. But with this approach I was perplexed with having to pass to arguments to doSomething
method