views:

242

answers:

7

I often found myself when writing a new class that inside a class I pass a parameter inside of many private methods. On the other side I also create sometime private members and just use them in one method.

So my question is "After which rules do you create a private member and when you don't and pass the variable from private method to private method"?

Can you give me some simple tips or hints, so that the design will get better?

+15  A: 

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

kgiannakakis
A: 

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

DanDan
+1  A: 

Creating private data just to avoid having to pass a round data as function arguments seems like global data in disguise to me. Data should be part of the class only if it is part of the class' state (i.e. it needs to be kept between invocations of the class' member function).

sbi
+1  A: 

Classes are supposed to be self contained, self sustained objects. If the data being used is part of the state of the class, you should use an internal variable. If the data is only going to be used temporarily for a calculation or something, pass it in as a parameter.

Additionally, you should be using private class members, using accessors and mutators to access and modify them. Don't allow external code to get it's hands on the internals of your class.

Mike Trpcic
A: 

Do what the Python community does.

Tip 1: Don't waste time on privacy.

Tip 2: Privacy is often more easily handled by delegation to a Strategy plug-in (or "injection") than by complex private attributes and methods.

Tip 3: Privacy makes unit testing more complex than it needs to be. It's easier to simply make things public.

Tip 4: Only use private when your code will be managed by lawyers who need to protect intellectual property by concealing an algorithm. This does happen, and when it does, it will be clear that you must declare things as private to prevent disclosure.

S.Lott
Surely declaring things as private doesn't actually 'hide' your algorithm - it's still right there in the header for all to see. You should use pImpl instead in those cases.
bdonlan
@bdonlan: I agree, private doesn't hide much. Python doesn't have it, and no one misses it.
S.Lott
This is a joke post, isn't it
DanDan
@DanDan: Nope. Python has only a minimal private construct -- not widely used. Very few complaints that it's missing. And very few struggles with the subtleties of privacy. The `private` keyword is approximately worthless. The downvotes come from an assumption that -- since it's in the language -- there must be a purpose. I've yet to see any good purpose for "private" declarations.
S.Lott
@S.Lott: "since it's in the language" -- Which of the many languages having `private` declarations are you talking about? "I've yet to see any good purpose for `private` declarations" -- encapsulation and information hiding are fundamental principles of CS. Do you really expect someone to step up here to defend them in front of you?
sbi
@sbi: many languages have `private` - Java, C++ are two I know. Python does not have it and doesn't need it. `private` does not enhance encapsulation that I can see. If you have counter-examples, that's good. After doing Java and Python since the '90's I've never seen value added via `private`. I've seen much value in encapsulation. None in `private`.
S.Lott
is not about if private is necessary or not, is about if its useful or not, by having private members/methods you can hide the implementation of your class, making its interface easier to understand for other programmers to use
Alberto Gutierrez
@Alberto Gutierrez: All the Python folks do is use a naming convention to separate the "approved" interface methods from the other methods. They don't spend time wondering about questions like this one. The time spent on formalizing privacy doesn't appear to create any value. Informal privacy (via a naming convention) seems to work as well and is much less complex.
S.Lott
-5 is too harsh...
Stefano Borini
@Stefano Borini: There is a "privacy fetish". Everyone goes through a phase where they think `private` is *really important*. Later, after trying to unittest or maintain programs with `private` declarations, they become more pragmatic. Until then, they must downvote pragmatism in favor of dogmatism.
S.Lott
+1  A: 

I agree with "If a parameter is part of the 'state' of the class, then use a private member", but that immediately brings a new question: when a parameter is consider part of the state of the class?

Just to compliment what has been said, I would add:

A parameter is part of the state of the class if after the call to the method we need to keep the value.

Example 1 The parameter is not part of the state of the class.

The class Car doesn't have any reason to remember what 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 the parameter is part of the state of the class.

The class Car needs to know how much fuel it has.

Class Car{
    Fuel fuel;        

    [...]

    public void putSomeFuel (Fuel fuelToAdd){
        this.fuel.add(fuelToAdd);
    }

    [...]
}
Alberto Gutierrez
A: 

Should this entity has the same lifetime as the object?

Of course, it's not a strict crterion which gives right answer in all cases, but it works good quite often and it's very simple...

maxim1000