When to use private members - when to pass a function / method as a parameter

I often met when writing a new class, which inside a class passed a parameter inside many private methods. On the other hand, I also create some private members and just use them in one way.

So my question is: “After what rules do you create a private member, and when not, and pass the variable from the private method to the private method”?

Can you give me some simple tips or tricks to make the design better?

+4
source share
7 answers

If the parameter is part of the "state" of the class, use the private element. If, on the other hand, the parameter is just a temporary object, just pass it as a parameter.

+17
source

I agree with "If the parameter is part of the class" state ", then use the private member", but this immediately raises a new question: when is the parameter considered as part of the class state?

Just to compliment what I said, I would add:

The parameter is part of the state of the class if, after calling the method, we need to save the value.

Example 1 A parameter is not part of the state of a class.

In the Car class, there is no reason to remember which key was used to start the car.

Class Car{ Lock lock; [...] public boolean startCar (Key keyUsedToStart){ return (canStartCarWithThisKey (keyUsedToStart)); } private boolean canStartCarWithThisKey (Key keyUsedToStart){ return (lock.canStartCarWithThisKey(keyUsedToStart)); } [...] } 

Example 2: a parameter is part of the state of a class.

Car class needs to know how much fuel it has.

 Class Car{ Fuel fuel; [...] public void putSomeFuel (Fuel fuelToAdd){ this.fuel.add(fuelToAdd); } [...] } 
+4
source

Creating private data is only to avoid the need to pass round data, since the arguments to the function seem to be global data that mask me. Data should be part of the class only if it is part of the state of the class (i.e., it must be stored between calls to a member function of the class).

+1
source

Classes must be self-contained, self-sustaining objects. If the data used is part of the state of the class, you must use an internal variable. If the data will be used only temporarily for calculation or something else, pass it as a parameter.

In addition, you must use private members of the class , using accessors and mutators to access and modify them. Do not allow external code to access the internal components of your class.

+1
source

I always pass variables to functions. You never know when you will need to use this function in a different context, and the parameters will change.

0
source

Should this object have the same lifetime as the object?

Of course, this is not a strict crterion that gives the correct answer in all cases, but it works well quite often, and it is very simple ...

0
source

Do what the Python community does.

Tip 1: Don't waste your time on privacy.

Tip 2: Privacy is often easier to handle by delegating the Strategy (or “injection”) to the plugin than using complex private attributes and methods.

Tip 3: Confidentiality makes unit testing more complex than it should be. It’s easier to just make things public.

Tip 4: use only private ones when your code is managed by lawyers who must protect intellectual property by hiding the algorithm. This happens, and when that happens, it will be clear that you must declare things confidential to prevent disclosure.

-6
source

All Articles