Earlier quoted context omitted.
Corollary: If your programming language supports reflection, people will depend on private properties and methods regardless of what you do.
People who can't accomplish something with an API because things are kept private won't be users.
Ask HN: Why do you make class members private?
111–117 of 117 posts
Re: Ask HN: Why do you make class members private?
#112Earlier quoted context omitted.
Ok, let's assume we only care about states that's always invalid, like negative health. So then my question is the same as for Jtsummers: is private a way to tell the programmer to look at all member functions to see if there is already a function that does what you need?
From your other replay: > Is the reason to make those variables private then to tell the programmer that there is a function that changes them and that you have to look through all the functions of the class before you touch them to see if there is a function that does what you need? I'm not sure I understand the question. A private property is a property that a class user should have no reason to want to directly wr…
What the user of a class has reason to do depends on what they need to get done. Maybe there is a function that already does what they need to do or maybe there isn't. So the choice is: call a function or write code that directly accesses the variable. This is a choice you would have both for private and public variables. But people say that they make things private because the class user should use already existing functions instead of touching the variable directly if there is already a function that does it. So does that mean that private is a way to tell the class user that there are functions that touch this variable, to increase the chances that the class user finds and uses them?
Re: Ask HN: Why do you make class members private?
#113Earlier quoted context omitted.
From your other replay: > Is the reason to make those variables private then to tell the programmer that there is a function that changes them and that you have to look through all the functions of the class before you touch them to see if there is a function that does what you need? I'm not sure I understand the question. A private property is a property that a class user should have no reason to want to directly wr…
> A private property is a property that a class user should have no reason to want to directly write to. What the user of a class has reason to do depends on what they need to get done. Maybe there is a function that already does what they need to do or maybe there isn't. So the choice is: call a function or write code that directly accesses the variable. This is a choice you would have both for private and public va…
I would say private is a way to tell the user to not touch the variable, or to use an accessor function that has validation to prevent entering invalid state.
At this point I feel like I'm just repeating myself, as I can't imagine a concrete example of what you're asking. Could you give a hypothetical, or better yet real, example of what you're thinking of here? I can't think of any at all. I felt that the example by Jtsummers was a great illustration.
Re: Ask HN: Why do you make class members private?
#114Earlier quoted context omitted.
If it's intended to be in that state, then it's valid. But following the example: if you made the player's health a public property, code could straight up just set it to a negative number. The game engine this is running in may not properly support a negative health value. If instead it was set using a public method, there can be added checks to lower bound health to zero. And depending on the architecture, all of t…
Ok, let's assume we only care about states that's always invalid, like negative health. So then my question is the same as for Jtsummers: is private a way to tell the programmer to look at all member functions to see if there is already a function that does what you need?
Private is a way of excluding the member so designated from any consideration at all by a consumer of the class. Assuming that they see it at all (which if it is in a compiled library using normal tooling they might well not at all), they are effectively told to treat it as if it does not exist.
There may exist mechanism to bypass this (especially in dynamic languages and/or those with robust introspection/metaprogramming), but that's breaking the warranty seal on the class.
All the functionality that a class provides to consumers is provided by its public members. That's what public members are.
Re: Ask HN: Why do you make class members private?
#115Earlier quoted context omitted.
> A private property is a property that a class user should have no reason to want to directly write to. What the user of a class has reason to do depends on what they need to get done. Maybe there is a function that already does what they need to do or maybe there isn't. So the choice is: call a function or write code that directly accesses the variable. This is a choice you would have both for private and public va…
> So does that mean that private is a way to tell the class user that there are functions that touch this variable, to increase the chances that the class user finds and uses them? I would say private is a way to tell the user to not touch the variable, or to use an accessor function that has validation to prevent entering invalid state. At this point I feel like I'm just repeating myself, as I can't imagine a concre…
That's what I meant, that it's a way to tell the programmer that you may not need to touch this variable directly because there are already functions that do that so please look at those functions to see if there is one that does what you need. Private then makes sure that all functions that touch the variable are added to the list because you can't use a private variable in a function not in the list. The list I'm talking about is the function declarations/definitions in the class.
This C++ code doesn't compile because `number` is private by default:
class A {
int number;
};
void zero(A &a) {
a.number = 0;
}
But this works because now `zero` is added to a list of functions inside the class and that way the function is easier to find for someone who wants to modify `number` (assuming the function and the class were not already defined right next to each other): class A {
int number;
void zero();
};
void A::zero() {
number = 0;
}
So the idea is that private can help with reusing code because it makes sure that all functions that modify the variable are listed in the class, which increases the chance of them being used.Otherwise, what's the difference between my two examples that makes the second one better than the first one (other than that the first one doesn't compile)? The only difference is that the function has been added to a list inside the class and got a slightly different name.
Re: Ask HN: Why do you make class members private?
#116Earlier quoted context omitted.
> So does that mean that private is a way to tell the class user that there are functions that touch this variable, to increase the chances that the class user finds and uses them? I would say private is a way to tell the user to not touch the variable, or to use an accessor function that has validation to prevent entering invalid state. At this point I feel like I'm just repeating myself, as I can't imagine a concre…
> I would say private is a way to tell the user to not touch the variable, or to use an accessor function that has validation to prevent entering invalid state. That's what I meant, that it's a way to tell the programmer that you may not need to touch this variable directly because there are already functions that do that so please look at those functions to see if there is one that does what you need. Private then m…
> Otherwise, what's the difference between my two examples that makes the second one better than the first one (other than that the first one doesn't compile)? The only difference is that the function has been added to a list inside the class and got a slightly different name.
The second one is not better than the first one. I would say it's a completely pointless approach to a problem, and would be better served by just making number public. Leaving an empty zero function signature around so someone can come by and fill it in to do what they want serves absolutely no useful purpose at all here. If zeroing out the number property of class A is useful for A, then it should be an already implemented function.
That being said, the example given, in my eyes, is not useful. What is "number"? Why is it private? What does "A" do? What are it's expected states? What are it's failure conditions? If A cannot enter an invalid state by changing number, there is no reason for it to be private.
As I previously said, private exists to prevent objects from entering invalid states from external influence. If A#number has no invalid state, then it need not be private.
Re: Ask HN: Why do you make class members private?
#117Earlier quoted context omitted.
> I would say private is a way to tell the user to not touch the variable, or to use an accessor function that has validation to prevent entering invalid state. That's what I meant, that it's a way to tell the programmer that you may not need to touch this variable directly because there are already functions that do that so please look at those functions to see if there is one that does what you need. Private then m…
I think the disconnect between me and you here is that you're looking at the the "user" ("programmer" in your comment) in our hypotheticals as a person who is going to implement the class. In my mind, they are the person who is using the class. They will not be implementing unimplemented function signatures. In my mind, in my comments, they are library users. I'm thinking of someone who is using std::vector and using…
I clarified in the question that I'm only asking about situations where everyone has access to all of the code. So are we assuming that the organization is structured so that only one person/team works on a class and that no-one else is supposed to touch it? Or why would the class user not write a new function that accesses the member variables? Is it because the variables are private, which tells them to look at existing functions, just like I described?
> That being said, the example given, in my eyes, is not useful.
Ok, let's do a more useful example, a string class, where we assume that `len` may never be negative (and we assume that there are other string functions defined in the same way as `truncate` to make the class more useful; I just didn't want to write them in this example).
Code 1:
class Str {
public:
char *data;
int len;
};
void truncate(Str *s) {
s->len = 0;
}
Code 2: class Str {
char *data;
int len;
public:
void truncate();
};
void Str::truncate() {
len = 0;
}
What's the difference between these two and why is code 2 better than code 1? As I currently understand it, the difference is that in code 2 the truncate function has been forced (because of private member variables) to be added to a list of functions inside the class, which makes it easier to find the function for someone who wants to truncate the string and therefore it reduces the risk that someone implements that function again when it already exists.