Interfaces and why they are important

Nowadays most of us know that software interfaces (and I don’t mean GUIs) are important and we try to apply that as much as we can. But maybe the reason why interfaces are important and should be used is not that clear to everyone.

 

The main problem when designing and writing ever larger software is complexity. As more code is added to a project, the more complex the system becomes. That in turns correlates with more software defects, more development time (even for relatively small tasks, as integration takes a big chunk) and all because the more complex something it is, the more difficult it is for the human brain to understand it.

 

Most of the solutions (or patterns) in software engineering try to manage this problem of complexity. More specifically, they try to reduce the complexity of the code which in turns makes the code easier to understand, read, decipher. As a result, the code becomes easier to maintain, improve, refactor, add new functionality to, teach to others. It all translates in cheaper costs for software and better return on investment. It’s a win-win that even CEOs can understand.

 

The ubiquitous solution to fight complexity in software is by introducing another level of indirection. Thus, the famous statement: You can solve any computing problem by adding another layer of indirection.

The vast majority of design patterns in software engineering use this simple technique to solve common problems.

 

Interfaces are just part of this solution. An interface offers you a clear contract between the writer of the implementation and the writer of the client user. The interfaces reduce the complexity of the implementation by hiding all that behind a clear contract. So instead of having to understand the full details of the implementation, all you have to understand is how to use the interface. That’s a severe reduction in complexity. Think about the many libraries that you use but don’t fully understand in the minutest details. They simplify your life.

 

Interfaces are also a natural part of the software design. If you ever designed a new piece of software from scratch, they you know that the first step is to translate the business requirements into a set of functionalities desired. A functional spec addressing the question – what should the software do? (not how). Then the software designer gets the functional spec and translates that into a design spec. In the process, it identifies sets of functionalities and responsibilities that are related. Each such a set can become a class in programming lingo. But that’s not enough. The design has to determine how these sets/classes relate to each other: how do they communicate to each other, what are the dependencies between them, how do they get created and when, what is their lifetime, etc.

All these questions must be answered by keeping in mind that the ultimate goal is to minimize the complexity of the overall system as much as possible. Remember, that in turn translates into maintainability, extensibility, flexibility and other good ilities.

That being said, interfaces are the best candidate to address these questions as interfaces are ultimately contracts of interaction. So a good design always defines the interfaces first, before any code is written (of course, some of them will change as the picture becomes clearer later, when more information if available).

 

Reducing dependencies is another big plus provided by interfaces, especially when used in libraries (or any code meant to be reusable). Anyone who ever tried to reuse some badly written C++ code for instance, knows how frustrating the include hell and the unresolved externals hell can be. Cyclic dependencies are especially evil in this context. Mind you, this is a known short-coming of C++ language.

The solution is to hide all the complexity and extra fluff behind an interface and a factory method that creates the implementation. This hides all the implementation details and dependencies that you shouldn’t care about.

 

Advertisements

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: