tags:

views:

412

answers:

8

Ok the great thing about programming to an interface is that it allows you to interchange specific classes as long as the new classes implement everything in that interface.

e.g. i program my dataSource object to an interface so i can change it between an xml reader and a sql database reader.

does this mean ideally every class should be programmed to an interface? when is it not a good idea to use an interface?

+6  A: 

When the YAGNI principle applies.

Interfaces are great but it's up to you to decide when the extra time it takes developing one is going to pay off. I've used interfaces plenty of times but there are far more situations where they are completely unnecessary.

Spencer Ruport
+1, it's very easy to extract an interface out of the class using the refactoring tools, so there's no reason to do it in advance unless you know for sure that you need an interface.
Steven Sudit
+2  A: 

Not every class needs to be flexibly interchanged with some other class. Your system design should identify the points where modules might be interchangeable, and use interfaces accordingly. It would be silly to pair every class with an additional interface file if there's no chance of that class ever being part of some functional group.

Every interface you add to your project adds complexity to the codebase. When you deal with interfaces, discoverability of how the program works is harder, because it's not always clear which IComponent is filling in for the job when consumer code is dealing with the interface explicitly.

womp
A: 

The real question is: what does your class DO? If you're writing a class that actually implements an interface somewhere in the .NET framework, declare it as such! Almost all simple library classes will fit that description.

If, instead, you're writing an esoteric class used only in your application and that cannot possibly take any other form, then it makes no sense to talk about what interfaces it implements.

Starting from the premise of, "should I be implementing an interface?" is flawed. You neither should be nor shouldn't be. You should simply be writing the classes you need, and declaring what they do as you go, including what interfaces they implement.

VoteyDisciple
A: 

Use an interface when you expect to need different behaviours used in the same context. I.e. if your system needs one customer class which is well defined, you probably don't need to use an ICustomer interface. But if you expect a class to comply to a certain behaviour s.a. "object can be saved" which applies to different knids of objects then you shoudl have the class implement an ISavable interface.

Another good reason to use an interface is if you expect different implementations of one kind of object. For example if ypu plan an SMS-Gateway which will route SMS's through several different third-party services, your classes should probably implent a common interface s.a. ISmsGatewayAdapter so your core system is independent from the specific implementation you use.

This also leads to 'dependecy injection' which is a technique to further decouple your classes and which is best implemented by using interfaces

Manu
A: 

I prefer to code as much as possible against an interface. I like it because I can use a tool like StructureMap to say "hey...get me an instance of IWidget" and it does the work for me. But by using a tool like this I can programatically or by configuration specify which instance is retrieved. This means that when I am testing I can load up a mock object that conforms to an interface, in my development environment I can load up a special local cache, when I am in production I can load up a caching farm layer, etc. Programming against an interface provides me a lot more power than not programming against an interface. Better to have and not need than need and not have applies here very well. And if you are into SOLID programming the easiest way to achieve many of those principles sort of begins by programming against an interface.

Andrew Siemer
+3  A: 

IMHO, you should try to use interfaces a lot. It's easier to be wrong by not using an interface than by using it.

My main argument on this is because interfaces help you make a more testable code. If a class constructor or a method has a concrete class as a parameter, it is harder (specially in c#, where no free mocking frameworks allow mocking non-virtual methods of concrete classes) for you to make your tests that are REAL unit tests.

I believe that if you have a DTO-like object, than it's overkill to use an interface, once mocking it may be maybe even harder than creating one.

If you're not testing, using dependency injection, inversion of control; and expect never to do any of these (please, avoid being there hehe), then I'd suggest interfaces to be used whenever you will really need to have different implementations, or you want to limit the visibility one class has over another.

Samuel Carrijo
It is recommended as a best practice to make methods virtual by default on "concrete" classes, which are fully mockable.
womp
There are no free mocking frameworks?
Steven Sudit
@Steven none that allows mocking non virtual methods
Samuel Carrijo
@womp I never heard of this recommendation. Could you give me some references?
Samuel Carrijo
http://www.scribd.com/doc/10731655/IDesign-C-Coding-Standard-232 is one. The mocking community also tends to espouse this view: http://stackoverflow.com/questions/124210/best-practices-of-test-driven-development-using-c-and-rhinomocks.
womp
A: 

As a general rule of thumb, I think you're better off overusing interfaces a bit than underusing them a bit. Err on the side of interface use.

Otherwise, YAGNI applies.

Ubiquitous Che
A: 

If you are using Visual Studio, it takes about two seconds to take your class and extract an interface (via the context menu). You can then code to that interface, and hardly any time was spent.

If you are just doing a simple project, then it may be overkill. But on medium+ size projects, I try to code to interfaces throughout the project, as it will make future development easier.

mc2thaH