News for hackers. Stuff that matters.
Re:Ask HN: Why do you make class members private? by xboxnolifes on Friday July 29, 2022 @04:06AM (#32274274)
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 std::vector::pop_back instead of trying to access the underlying container, grabbing the value, deleting the value, and setting the private variables to match the new state.

> 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? by robalni on Friday July 29, 2022 @08:26AM (#32276021)
> They will not be implementing unimplemented function signatures. In my mind, in my comments, they are library users.

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.