Live data from Hacker News

Ask HN: Why do you make class members private?

news.ycombinator.com

71–80 of 117 posts

Re: Ask HN: Why do you make class members private?

#71
post #34

> You have been taught to do it so you just do it without thinking. Ouch, that's a pretty careless reason to start with. Why does insulting your audience seem like the right first move? In much of the classist (classy? You decide) code I've written, the entire reason that I've chosen to encapsulate the data into a class is that maintaining a consistent data structure requires book-keeping. The fields and methods asso…

> Ouch, that's a pretty careless reason to start with

I think it's fine. This is probably the reason that I used to have for using private until I started thinking about it.

> there should never be a reason for consumers to handle that stuff directly

Are we assuming here that the organization is structured so that only one team works on a class, so that that's the team that knows how the bookkeeping is done and no other team should touch it?

Re: Ask HN: Why do you make class members private?

#72

To hide implementation details from class users. You ideally only want to have a documented interface and all implementation details should be opaque to class users. This can be quite tricky and usually classes will export some state, typically done through getters and setters. Then one day you decide you want to aggressively refactor that class. Now you can because the interface can stay the same even though you cou…

> To hide implementation details from class users.

Yes, that's what private does. My question was more why you should do that.

> the interface can stay the same even though you could completely re-do the guts.

So is the reason to use private that it's a way to tell programmers to not use something in too many places because that will make it hard to change in all those places if you need to change the private member?

Re: Ask HN: Why do you make class members private?

#73

Earlier quoted context omitted.

Python would like a word...

The language is not relevant to the question. Making variables and methods private means making them inaccessible outside the class and I gave the typical reasons/best practices for that.

> Making variables and methods private means making them inaccessible outside the class and I gave the typical reasons/best practices for that.

The point they are making is in python there's no private method, everything is public. There's only a convention of "avoid using anything starting with "_"

Re: Ask HN: Why do you make class members private?

#74
post #72

To hide implementation details from class users. You ideally only want to have a documented interface and all implementation details should be opaque to class users. This can be quite tricky and usually classes will export some state, typically done through getters and setters. Then one day you decide you want to aggressively refactor that class. Now you can because the interface can stay the same even though you cou…

> To hide implementation details from class users. Yes, that's what private does. My question was more why you should do that. > the interface can stay the same even though you could completely re-do the guts. So is the reason to use private that it's a way to tell programmers to not use something in too many places because that will make it hard to change in all those places if you need to change the private member?

No, it says 'this is not meant for you to change from the outside'. It's an implementation detail, not part of the interface and any references to it are bound to break without notice, warning or acceptance of the consequences.

Think of the interface to your class as a contract: this is how this class works, from now until eternity (or until the next breaking change ;) ). Relying on implementation details breaks that contract, the contract is on the interface not on the guts.

It won't be 'hard to change' it may be impossible to change. Because someone that relies on your class may not have access to the code of the other class!

Re: Ask HN: Why do you make class members private?

#75
post #62

Earlier quoted context omitted.

Re 1: The reason is not just to hide access, but also to permit change. Suppose someone implements a data structure using a backing array. Later on they change it to use a b-tree, but users have started directly accessing the backing array (because it's faster than the presented interface, even if just by a function call) and passing around references/pointers to array elements. Oops. You can't make changes anymore,…

1. So the reason here to use private is as a way to tell programmers to not use a thing in too many places because then it will be hard to change in all those places if you need to change the private member? 2. First, remember that I am only interested in code that everyone has write access to so even if something is private, everyone can still change it. "This makes the class/module author responsible for controllin…

1. It's private, only internal routines can use it. You aren't telling anyone to avoid using it, you are constraining where it can be used. You can use it directly in every internal routine if you want, that's not a problem. But no one outside the class/module can directly access it so if you change it (in whatever fashion) then those outside users will not be impacted because they are only dependent on the public interface. Now if you change the public interface, then they are impacted but that's often rare after the initial development effort, in my experience.

2. Forget about people, it's about place. I don't care how many people alter a particular class or module. What I care about is how many places some information has to exist within the code and has to be maintained and synchronized as a result.

As a mostly-useless-after-CS101 example, consider a bounded stack. An implementation might have a number (perhaps variable) which describes its limit, an array backing it, and another number indicating where the current "top" of the stack is (in languages where arrays carry their size you don't necessarily need that limit number as a separate thing).

If you leave everything public then every user of this bounded stack could directly alter the backing array and change the "top" of the stack, artificially indicating that something had been popped off or incorrectly incrementing beyond the limit. The limit itself could be altered without actually changing the backing array. The backing array could be made smaller or larger without correspondingly changing the limit. All of that would make this data structure useless, because it would be in an arbitrary, likely invalid, state.

In order to preserve the invariants of the system (limit == array size, 0 correctly use it they have to preserve all these invariants everywhere they use the data structure. The code is now scattered and contains many duplications. Again, this isn't about people, it's about places and the number of them.

If you want to change the internal structure of this bounded stack, you have to change every place that currently accesses the public fields. Or you can be a sane developer, use private fields and public routines that manipulate the state so that it's always in a valid state. Now when you change the internal structure you only have to change those public routines and the private fields, no other place has to be altered. Every use of this bounded stack will look exactly the same as before, just push and pop and some error handling for when the stack is full or empty.

Re: Ask HN: Why do you make class members private?

#77
post #71
post #34

> You have been taught to do it so you just do it without thinking. Ouch, that's a pretty careless reason to start with. Why does insulting your audience seem like the right first move? In much of the classist (classy? You decide) code I've written, the entire reason that I've chosen to encapsulate the data into a class is that maintaining a consistent data structure requires book-keeping. The fields and methods asso…

> Ouch, that's a pretty careless reason to start with I think it's fine. This is probably the reason that I used to have for using private until I started thinking about it. > there should never be a reason for consumers to handle that stuff directly Are we assuming here that the organization is structured so that only one team works on a class, so that that's the team that knows how the bookkeeping is done and no ot…

> Are we assuming here that the organization is structured so that only one team works on a class, so that that's the team that knows how the bookkeeping is done and no other team should touch it?

No. If you're in such an organization, anybody on any team should be able to read the code and discover how the bookkeeping is done and locate bugs therein. A good API will obscure the necessity of bookkeeping from consumers in order to reduce bug-ridden boilerplate. Said in more highfalutin' language, privacy should be used to prevent leaky abstractions.

Re: Ask HN: Why do you make class members private?

#78
post #34

> You have been taught to do it so you just do it without thinking. Ouch, that's a pretty careless reason to start with. Why does insulting your audience seem like the right first move? In much of the classist (classy? You decide) code I've written, the entire reason that I've chosen to encapsulate the data into a class is that maintaining a consistent data structure requires book-keeping. The fields and methods asso…

> A dedicated user can gain access to, and modify, everything. That may be so, but it is akin to using an undocumented system call. And good luck if that class ever gets refactored or turned into a service.

Indeed. My point was that "private" has all the strength of a "warranty void if removed" sticker.

Re: Ask HN: Why do you make class members private?

#79

The reason is simple: to prevent objects from getting to an invalid state. The set of property values of an object is its state, and you can model every possible valid state and state transition. Not every state is valid. If you allow all variables to be touched willy-nilly, you allow an object to be put into an invalid state. The only variables and functions that should be public are those that cannot put an object…

> The only variables and functions that should be public are those that cannot put an object into an invalid state.

That's an interesting one. So a reason to use private is to tell programmers that calling this function or changing this variable can leave the object in an invalid state? I guess invalid state then means that you can't call a public function on that object.

It might not be clear what an invalid state is though. I mean, every state has its place. Example, after I decrease the health of the player to 0 but before the death animation is played, is that a valid state? I should not be able to play in that state but it's a state that the player might be in for a frame and it will be visible on the screen.

Re: Ask HN: Why do you make class members private?

#80
post #51

Earlier quoted context omitted.

Can we acknowledge yet that the concept of classes as tiny programs with tiny APIs has failed? [...] mainly based around working mostly in C++ Speaking as a C# developer, I think the concept of classes as tiny programs with tiny APIs works very well.

It's been a decade since I used C# but the corporate design pattern culture of that language back then turned me off of it forever. Everything looked like this: https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpris... Maybe it's better now but the Java/C# practice of shoveling largely empty classes around with an IDE isn't something I'd point to as a good example.

Wow, what a repo. I can't even tell if the author is serious or this is some tinderbox-dry satire, bravo either way.

I'm sad that I've worked with plenty of codebases that look exactly like that and I twitched a little bit browsing around. :)

Post reply on HN