<< back to blog

Interfaces are IFriendly

.net

20 Oct, 2008

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...
.NET Gem Fun
Running FubuMVC on Mono (OSX)
Really Easy Way To Partition an ASP.NET MVC With An Admin Area
Automating .NET development (and NHibernate) with IronRuby + Rake
Get a Rails-Stylee Interactive Shell For Your NHibernate Backed Domain Model
In Memory SQLite Testing with NHibernate
NHibernate Trick
OSX MonoDevelop MVC?
NHibernate: Calling Update Unnecessarily
.NET Config for Multiple Developers

kick it on DotNetKicks.com
blog comments powered by Disqus