Tuesday, September 1, 2009

Emphasizing Encapsulation

Encapsulation in software design refers to hiding the details of an implementation behind a set of provided interfaces. This definition isn't exclusive to object-oriented software design. Even traditional functions provide an interface while the implementation of that function is hidden. Object-oriented design simply places more emphasis on encapsulation. This is import when designing software simply because it imposes limits on what the client can do with the implementation. This isn't to say that the client invoking the code cannot misuse it. Chances are that a developer using even a well-written piece of code is going to misuse it at first. However, if the implementation is hidden, and the provided interfaces are well-designed and well-documented, than it doesn't take very long to figure out the intended use. Contrast this with code that isn't encapsulated. Even if well documented, the developer can never fully conceptualize the intent of the code.

The provided interfaces lie at the core of encapsulation because without them, all you have is an opaque piece of code that one cannot interact with. This obviously isn't desired and doesn't really exist in practice. Encapsulating messy code is a good thing initially. Emphasis should be placed on the quality of the provided interfaces. This doesn't exactly act as an excuse for writing terrible code. It merely states that if one thing needs to been done well and on time, it should be the provided interfaces. In placing the the quality requirements in the provided interfaces, this provides a nice sandbox for refactoring the encapsulated code.