mercredi 27 avril 2016

Passing single object vs passing multiple arguments

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

-Creates unnecessary dependency towards class A


Can anyone share additional insight towards the pros and cons of these two methods?

Aucun commentaire:

Enregistrer un commentaire