3 ms·
Forgive my incompetence, but I have never gotten the point of private methods/variables. Could someone please explain why they exist? Edit: I know it's slightl
by lukeqsee 16y ago
Forgive my incompetence, but I have never gotten the point of private methods/variables. Could someone please explain why they exist?
Edit: I know it's slightly off-topic.
- ianp 16y agoPrivate members are generally just to protect your from yourself. If you have an integer that should /always/ be 1-10, it's easier to use a private variable and a setter that ensures the variable is always valid, than to hope all your code follows the rules. (Then also if the rule changes, e.g. to 5-100, you just need to change one function). There are some other uses and examples, but this is the simplest one i can think of off the top of my head.
- sam191 16y agoI may not be the most qualified person to answer this, but I always thought private methods and variables were for internal use inside of a class while the public methods were what one would actually call with their objects. For example your object had a method foo() which would be better defined as smaller methods which are combined together. Those smaller functions serve no other purpose and therefore do not need to be called by the object directly.
- sophacles 16y agoOne of the points not discussed in the other replies yet, is that public methods and variables are the interface, the well defined, relatively static way of using a class. The private members are internal implementation details, and may change frequently -- not to mention completely change in style, functionality and so on. The idea then, is that larger teams can be divided into sub-teams, and work in parallel because the interfaces between components don't change, even if the internals change drastically. Essentially it allows components to be somewhat independent. How this works in practice tho...
- epo 16y agoPrivate methods/variables etc are those which are not publicly available. Typically internal state for the object. Ideally, or at least for purity, all variables should be private and only accessible via get/set methods, thus encapsulation is enforced.
- rryyan 16y agoAccess specifiers (like "private") are language features used to implement the OOP notion of encapsulation: http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming) http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_.... Encapsulation is generally cited as one of the defining features of object oriented programming.
- silentbicycle 16y agoWhich is weird, because it's actually a defining feature of modular programming. Public/private access are used to specify an interface to arbitrary functionality. Other modules can call a public interface, but all private internals can be removed / modified freely.
- koenigdavidmj 16y agoYep, plenty of C modules do hacks to block access except through the defined API. Most are content simply to use void pointers in public header files, but some do nasty things like generating a random integer at library initialisation time and xor'ing all returned pointers by that number before returning it. (Eww.)
- ToastOpt 16y agoArguably, it's a matter of isolation from 'irrelevant' changes. In a function, one wishes to keep the ability to redefine their local variables (perhaps, to implement a faster/smaller-space algorithm) without otherwise affecting the overall program -- when they call other code, that other code should avoid accessing the caller's variables, lest the caller be stuck with precisely those variables and that implementation. If this isolation exists, I have more freedom to vary the implementation. Likewise, private members of a class are implementation details I may wish to change later -- but I would like some assurance that other components will be isolated from this change, so as not to break. Edit-add: oh, I of course forgot to mention -- for those not familiar, python permits both :-)