A piece of software is said to be encapsulated if someone can use it without knowing its inner workings. The software is a sort of black box. It has a well-defined interface to the outside world. “You give me input like this and I’ll produce output like that. Never mind how I do it. You don’t need to know.”
I think software development focuses too much on logical encapsulation. Code is logically encapsulated if, in theory, there is no logical necessity to look inside the black box.
Maybe there would be no need to look inside the code if everything were working correctly, but there’s a bug. Or maybe the code isn’t buggy so much as incompletely documented. Maybe your inputs push the box beyond its designed limits and you don’t know that until you use it. Or maybe the code is taking too long to execute.
Maybe there’s nothing wrong with the code, but you don’t trust it. In that case, the code is logically encapsulated but not psychologically encapsulated. That lack of trust negates the psychological benefits of encapsulation. This may be the most insidious breach of encapsulation. A failure of logical encapsulation is objective and may easily be fixed. A loss of confidence may be much harder to repair.
Developers may avoid using a component long after bugs have been fixed. Or they may use the component but be wary of it. They don’t concentrate their full mental energy on their own code because of a lack of trust in their dependencies. When a bug shows up in their code, they may waste time checking the untrusted component.
Psychological encapsulation might explain in part why people are more productive using tools they like. For example, Windows runs well for people who like Windows. Linux runs well for people who like Linux. Macs run well for people who like Macs. It’s as if the computer knows who its friends are.
Some of this is confirmation bias: we’re less likely to notice the flaws in things we like and more likely to notice the flaws in things we don’t like. But I think there’s more going on. If someone is using an operating system they distrust, they’re going to be less productive, even if the operating system performs flawlessly. Every time something goes wrong, they suspect it might be the operating system’s fault. Of course an error might be the operating system’s fault, but this is rare.
Related post: Opening black boxes