Tuesday, June 21, 2011

The iddy biddy interface

Tools like resharper allow you to extract an interface from a class.

At a simple level you can just pull out all the public functions. Happy days, look Ma, I'm programming to an interface.

But what does that really buy you.

Lets say I have my AnalogTemperatureSensor class, and I extract an IAnalogTemperatureSensor interface.

So now I have a function that takes in readings, and gets some form of summary data, say an AverageWithOutliersRemoved .

Then when I want the same functionality for my WaterLevelSensor, I find that it does not share the Functions to choose Centigrade or Fahrenheit.... Opps, I can't reuse my code.

Also, there's no clear way to see what functions from IAnalogTemperatureSensor the AverageWithOutliersRemoved function will use.

IAnalogSensor 

On the other hand, If I had simply extracted an IAnalogSensor with functions like TakeReading(), IsReadingValid(), then my AnalogTemperatureSensor and my WaterLevelSensor could both implement this.

AnalogTemperatureSensor might also implement ITemperatureDevice, which allows me to choose C or F.

Several Small Specific interfaces are a whole better than one big nasty one.