vendredi 6 mars 2015

Is it good to pass minimal paramters?

Assume I have an object Car, with five parameters, { numwheels, color, mileage, horsepower, maxSpeed }. I have a method that needs 3 of these values. Which of the 2 options is said to be best practice ? Is it better to pass enclosing object and reduce the number of parameters, OR just pass in bare-minimal data to a method (eg: numwheels and color will not be accessed in method 2) ?




  1. Option 1 pass whole oject:


    void compute(Car c) { return c.mileage + c.horsepower + c.maxSpeed; }




  2. Option 2: pass in just the method values.


    void compute(int mileage, int horsepower, int maxSpeed) { return mileage + horsepower + maxSpeed; }




Aucun commentaire:

Enregistrer un commentaire