3 ms·
> What guarantees that when I send a message to an object, that object won't decide to change itself in response? I don't think that's what the other commenter
by DonaldPShimoda 8y ago
> What guarantees that when I send a message to an object, that object won't decide to change itself in response?
I don't think that's what the other commenter is saying.
They're saying that internal state is not directly manipulable from external sources. So an object `x` cannot have a method that takes in another object `y` and does `y.foo += 3`. Instead, it must do something like `y.addFoo(3)` to accomplish the goal.
This is superficial in some sense, but then every distinction between various programming languages is superficial if you look at it right. :)
The idea is that the object itself is the only thing which can manipulate its internal state. Therefore, to use these objects, the programmer must have imbued them with the necessary methods. This is restrictive (you have to implement the methods before you can manipulate the state), but this is the core design principle I think the other commenter is trying to tell you about. Objects cannot be manipulated willy-nilly; they must have implemented some method which can be called from the outside. This restriction creates a much more solid barrier of distinction between the responsibilities of the various objects at play.
- galaxyLogic 8y agoRight and that is a subtle notion. You may not need that level of encapsulation the same day you are programming it, but as the system grows it gives you benefits in maintainability including observability. That may be why benefits of Object Orientation may not be so obvious to understand on first look, you need to consider the future evolution of the system. We need to see the forest from the trees.