Timeline for Why do we need private variables?
Current License: CC BY-SA 3.0
20 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 1, 2024 at 1:07 | comment | added | NickJ | This is a problem both created and solved by object oriented code. If other classes are not reaching into your object and mutating things then you wouldn't have this problem in the first place. Having meangingful state shoved around into hidden nooks and crannies all over an app was the worst possible design of large scale programs from the beginning. Apps are not cells and programs are not collections of biological cells. An app is more like an army then a cell. The cells should not all be doing their own thing. Apps need to have exact results not race conditions and hidden state. | |
| May 3, 2021 at 16:41 | comment | added | Alberto Salvia Novella | I feel that enforcing it, as to have special words for it, was never necessary. | |
| Jan 26, 2020 at 10:31 | comment | added | Pratham Shah | Even in my college teachers teach that with c++ you can make banking application because it provides security and with c you cannot as it does not provide security. | |
| Mar 21, 2019 at 16:18 | comment | added | Curtis Yallop | We want to express code-logic in a way that reduces complexity. If we can look at code and say: this data can only be touched from this small scope, it is way easier to reason about the logic (to ensure correctness and troubleshoot bugs). We are basically setting up restricted-scope rules and telling the compiler to enforce them so we don't even need to think about the possibility of other code affecting these privates. Then we explicitly publish as public only what minimally should be public. And only pass the instance handle to places it should be used from. | |
| Mar 21, 2019 at 16:07 | comment | added | Curtis Yallop | Globals would also imply not having different object-instances of the same class. So actually, why-not-globals is a slightly different question that why-use-privates. | |
| Mar 21, 2019 at 15:52 | comment | added | Curtis Yallop | "Why do we need privates?" is almost the same question as "Why are globals bad?" Both are great questions to really understand. If another module wants to reach in a touch this one perfect thing that gets him what he wants, that's perfect! But it starts turning into a spaghetti mess that's extremely hard to follow and hard to troubleshoot bugs. Hacks are applied to fix complex bugs. After multiple layers of hacks, trying to figure out how a variable got set to a particular value can become almost impossible. The reasons described above answer why globals are bad as well - complexity, coupling. | |
| Jan 18, 2018 at 18:17 | comment | added | doubleYou | @AdamZerner see "Tell, don't ask" (martinfowler.com/bliki/TellDontAsk.html) about having getters/setters in the first place - but otherwise yes, because you should be free to change the internal representation of the value anytime you wish. As always, there are exceptions... (e.g. DTOs) | |
| May 11, 2017 at 3:47 | comment | added | Adam Zerner | So then, I suppose tutorials using nuclear_codes as an example of a private variable are being misleading. | |
| May 11, 2017 at 3:43 | comment | added | Adam Zerner | So then, are one line getters and setters like return this.name and this.name = name worthwhile? If you have a bug, I don't see how having such methods brings you closer to finding the bug versus if name was public. | |
| Jun 17, 2016 at 14:31 | comment | added | Wayne Werner | Exactly that. When people hear "information hiding", they think that they should use private variables for things like encryption keys, database credentials, etc. | |
| Jun 17, 2016 at 3:17 | comment | added | Andres F. | @WayneWerner What do you mean, "security"? I don't think I ever saw an example of a variable being private for "security" :/ Can you provide an example? Like this answer states, keeping internal state private is all about managing complexity and reducing the probability of bugs, and this is how it's explained everywhere. | |
| Feb 22, 2016 at 16:38 | history | rollback | gnat | Rollback to Revision 1 - reverted an invalid edit approved by careless reviewers: http://programmers.stackexchange.com/review/suggested-edits/131838 | |
| S Feb 22, 2016 at 13:40 | history | suggested | CommunityBot | CC BY-SA 3.0 | Added a great paragraph from Effective Java to elaborate more about the concept. |
| Feb 22, 2016 at 11:49 | review | Suggested edits | |||
| S Feb 22, 2016 at 13:40 | |||||
| Sep 22, 2015 at 23:37 | comment | added | Cort Ammon | I always like to say that part of debugging is not just driving directly towards "what went wrong," but taking the roundabout process of figuring out "what didn't go wrong," and focusing my investigation on what is left. If I can get the compiler to assist me with tools like private variables to define "what cannot possibly have occurred," that saves me a load of time. Only in the rare cases where I prove that what did go wrong cannot have occurred do I have to dig into the more frightening assumptions, like perhaps a buffer overrun overwriting my private data. | |
| Sep 22, 2015 at 21:31 | comment | added | Wayne Werner | I never realized it was used for managing complexity because in every case I've ever seen them used it was for security. Which is super funny because those same occasions used serializable classes which means there's no such thing as protection. Simply serialize, munge the data, and deserialize. | |
| Apr 10, 2012 at 17:15 | vote | accept | mwallace | ||
| Apr 10, 2012 at 9:58 | comment | added | user1249 | The holy grail of modern computing is keeping complexity down. | |
| Apr 10, 2012 at 5:57 | comment | added | Jeffrey Sweeney | +1 for getting at the heart of the issue, though I've personally found my code easier to maintain when I stopped going through hoops just to ensure that a frequently used variable stays private. Sure an error may come up in the future, but it's 60 less lines of code to sift through, and less variables and functions to boot ;) | |
| Apr 10, 2012 at 5:48 | history | answered | tdammers | CC BY-SA 3.0 |