Wednesday, January 04, 2006

Interfaces are not contracts

One part of OO programming that I have little agreement with is the idea that interfaces are contracts. The only thing that an interface specfies is how to call the particular method. However it makes no gaurantees whatsover as to what the method does. Every implementation of an interface could do a separate and totally unrelated action.

My favorite example of this are properties/methods that expose file names. A lot of interfaces (or just properties on abstract/parent classes) have a property that exposes information about an underlying or related file. Typically this is called FileName and is a string.

But what does that really mean? Is it the


  • The name of the actual file: foo.txt

  • The full path of the file in normal notation: c:\bar\foo.txt

  • The relative path of the file: bar\foo.txt

  • The full path of the file in URI format: file://c:/bar/foo.txt

I've played with interfaces where different implementations have returned any number of the above. It's quite frustrating.

But it goes to the point that interfaces by themselves are not contracts.

0 Comments:

Post a Comment

<< Home