4 ms·
> 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 u
by robalni 4y ago
> 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.
- Jtsummers 4y agoInvalid state means invalid state. Suppose (going with my bounded stack example from another reply to you) you initialize the stack with: a_bounded_stack = new BoundedStack(100); There are various actions applied, correctly, through the interface (push, pop, and peek). Somewhere, you decide to manually alter the size of the stack but not through its interface (resize) a_bounded_stack.limit = 50; But you forget (someone came by with cookies) to actually shrink the backing array and also to change "top". So now you end up with this state: a_bounded_stack limit = 50 array.size = 100 top = 60 Oops, top is not allowed to be larger than limit, and array.size is supposed to be equal to limit. That is an invalid state, people can still use the public interface (push, pop, peek), but who knows what they'll get back.
- robalni 4y ago> But you forget (someone came by with cookies) to actually shrink the backing array and also to change "top". Someone could come by with cookies while I'm writing a private function too. This is more of a reason for having a function for doing something. 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?
- deleted 4y ago[deleted]
- xboxnolifes 4y agoIf 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 the post death animation stuff could also be handled as a check in that function call.
- robalni 4y agoOk, 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?
- xboxnolifes 4y agoFrom 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 write to. Or more accurately, it's a variable that directly writing to could invalidate the state of the object. They are intermediate values or implementation details that define the state of the object. Reading them should be fine, but allowing unrestricted writing is dangerous. If there is reason to directly change the value, there should be a public method that includes validation to prevent the user from invalidating the object. On the other hand, private methods exist for code re-use convenience purposes. You could just as easily only have public methods and write all logic inside of them. But a lot of the time you have pieces of code that could be reused between methods, but on its own doesn't guarantee the object to be in a valid state at the beginning or end of the call. So, you make it private. It only exists as a means of avoiding code duplication and possibly improving readability. Private methods may also have value in inheritance / abstract classes, but I'm focusing on invalid state.