Reg Braithwaite has got me thinking about information hiding. It’s a tenet of modular programming, including OO. But why is information hiding worth bothering with at all?
- It makes the public interface easier to understand, and therefore the code more readable.
- It prevents clients of the class from accessing the physical layout of an implementation which is likely to change. By restricting client access to a well-defined and immutable interface, we have some assurance that changing the implementation is less likely to break external code.
- It prevents clients of the class from breaking the implementation by altering the state of an instance in a way not anticipated or allowed by the class’s designer.
- For security reasons, some things can’t be shown to the outside world.
For some reason, many discussions of information hiding, such as this Wikipedia article, seem to focus mostly on the second point (allowing change to the implementation without breaking external code). They’re all important issues, although information hiding is not always the best solution to each problem.
Information hiding is not the only way to make a class’s interface more readable, but it is very effective at this. I believe that readability is so important that this reason alone is justification for using information hiding. However, the for hiding information, we would do well to consider whether or not the information should exist in the first place. It’s probably worse to hide cruft than to expose it. Before hiding information for readability, consider whether the information to be hidden is a buried treasure or dust about to be swept under the rug.
The flipside of the second point, preventing the outside world from having dependencies on the internal details of your class, is that the public interface of a class should be, as much as possible, immutable. Hiding the parts of the class that are likely to change is just another way to say that the parts of the class which are not hidden should not change.
The third point, preventing clients from changing the state of a class, seems like a good idea, but it implicitly presumes that the class has mutable state to begin with. It is a non-issue in a stateless class, or a stateful but immutable class.
Security seems like a good reason to hide information, but the hiding is often done poorly, resulting in leaks. Of course, the only foolproof security is not to store this information at all. But if you absolutely must store it, you need to hide it. In this case, the cost of hiding the information well can be quite high.
Funny, I started by talking about information hiding, but with every detail I explore I find myself talking about reducing state (possibly to statelessness) or making it immutable.
Conclusion?
{ 5 } Comments
I think one of the other problems of hiding information is hiding too much of it, and creating wrong ownerships. It is important to identify which information hide, or to hide the information that is unnecessary to others.
State (if there is any) is part of the interface, but a part that tends to be poorly documented. Most languages don’t have support for annotating state-change in the type-system, so it’s hard to rigorously specify. Functional programmers have known for years that eliminating unnecessary state reduces coupling and leads to clean and robust modularity.
3x + y = 9 + y
- subtract out y
3x = 9
Isn’t information hiding partly about reducing the number of things to consider? I’ve heard David Ungar quoted as saying, "Interfaces are about psychological chunking."
Greg: Yes, exactly.
Dan: Yes, this is essentially point 1 in my post, expressed a bit differently.
I meant something else, actually…that information hiding, by making state inaccessible to clients, shrinks the amount the programmer has to consider.
It’s like a corollary to point 3: clients can’t touch the state, so the designer can ignore both the client’s impact on state, and variables in the client’s space.
While information hiding makes the thing easier to use from the outside, it also makes it easier to change from the inside.
{ 1 } Trackback
[...] year, I asked the question, "Why hide information?" Without needing a definitive answer for that question, let’s agree that, given a certain use [...]
Post a Comment