Oct 20, 2008 / .net
Interfaces are IFriendly

I used to find interfaces a pain in the rear, I couldn't see why they added value.

These days I like them. Here's why.

Interfaces communicate responsibilities and collaborations

Q1. What does this class do?

public class Document 
{ 
   // insert lots of code here 
}

Q2. What does this class do?

public class Document : IPersistent, IVersionable, IReleasable, ITaggable
{
   // insert lots of code here
}

The first one is anyones guess. With the second one, you can get a good idea of the classes collaborations and responsibilities in seconds.

The reason I used to not like interfaces was for this exact same reason. When learning a new library/API, interfaces instantly reveal responsibilities that you don't understand. They're a reminder of how much learning you have to do. That is painful.

In reality the pain is worth it. You will probably have to understand those responsibilities and collaborations anyway. Even if they're not explicitly stated in interfaces. The interfaces just help group them up.

This reason alone makes single use interfaces worth while.

Interfaces reduce potential complexity

Understanding this...

public void Release(IReleasable r){ ... }

...is far easier than understanding this...

public void Release(Document d){...}

The reason is that the possible complexity of Release(...) is greatly reduced. We know it can only work on a subset of a Document, therefore there's less to understand and keep in mind at any given time.


You may also like...