Suppose I have the following
Class A { Foo getFoo(); Bar getBar(); Baz getBaz(); }
And I need to define a function doStuff
that uses Foo
, Bar
, Baz
of one object and does some stuff
I'm struggling between which method of implementing doStuff
is better (suppose it would be undesirable to place doStuff
inside class A
)
Method A
void doStuff(Foo foo, Bar bar, Baz baz) { //some operation }
or
Method B
void doStuff(A a) { Foo foo = a.getFoo(); Bar bar = a.getBar(); Baz baz = a.getBaz(); //some operation }
To my limited knowledge, (+ pros, - cons)
Method A
+It is clear exactly what parameters doStuff()
operates on
-Susceptible to long parameter lists and more susceptible to user mistakes
Method B
+Simple, easy to use method
+Seems more extensible (?)
-Creates unnecessary dependency towards class A
Can anyone share additional insight towards the pros and cons of these two methods?
The * symbol is used to pass a variable number of arguments to a function. Typically, this syntax is used to avoid the code failing when we don't know how many arguments will be sent to the function.
Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
Java always passes parameter variables by value. Object variables in Java always point to the real object in the memory heap. A mutable object's value can be changed when it is passed to a method. An immutable object's value cannot be changed, even if it is passed a new value.
Method A (naked parameters) always has the advantages that
Method B (Parameter Object) has advantages when
That the Parameter Object introduces a new dependency on which caller and callee depend is not much of a disadvantage, since it is a simple class with no dependencies of its own.
So, Parameter Object is
Parameter Object
s do provide a nice approach to encapsulate related
parameters to reduce the total parameter count to any method or constructor. One should be very careful to make sure that the parameter objects do actually contain truly related parameters.
Actually there are multiple ways of approaching this problem depending on the parameter types
you are dealing with. If you are dealing with parameters that are general types like more than one String
s or Int
s and there is a potential for a client to actually pass in the wrong sequence of arguments ,it often makes more sense to create custom types
ie. create enum
with possible values. That can provide good compile time check for your arguments. Another good use of them is you can use them to return
complex values from functions. See here.
Another approach i take a lot of times is to check and see if the work done by the doStuff
method be broken down into simpler methods with less dependencies.
Principally i try to follow Bob Martin's recommendation of a maximum of three parameters. Well he actually say it should be mostly not more than one ! Any increase should have justified reasons. Refer this excellent book : Clean Code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With