Comments on: What is an Interface? https://simpleprogrammer.com/back-to-basics-what-is-an-interface/ Fri, 12 Jan 2018 17:36:58 +0000 hourly 1 https://wordpress.org/?v=7.0 By: David Sackstein https://simpleprogrammer.com/back-to-basics-what-is-an-interface/#comment-458 Fri, 10 Jun 2016 22:23:00 +0000 https://simpleprogrammer.com/?p=1152#comment-458 Hi John,
You claim that defining an interface just to gain the ability to provide a mock for tests is not justified.
I disagree.
For instance, take the email server example that was mentioned. (You need to mock the email server so that “the unit tests do not need to hit a real SMTP server”).
The need for the interface is not BECAUSE you need to test. Rather, the test demonstrates that you needed the interface all along.
The test demonstrates that your class under test DOES NOT depend on the ability to send SMTP messages. It only depends on the behavior of one that can.
It can function very well without SMTP indeed without a connection to the internet.
Without the need to test you might not have discovered that.
Having discovered that you can now simplify your class because it needs to make fewer assumptions about the email server it is using than before.
The power of depending on less is not only because it allows you to work with other implementations, but also because it simplifies your code.

]]>
By: jsonmez https://simpleprogrammer.com/back-to-basics-what-is-an-interface/#comment-457 Fri, 06 Dec 2013 19:36:00 +0000 https://simpleprogrammer.com/?p=1152#comment-457 In reply to Róbert Papp.

That would be work as well.

]]>
By: Róbert Papp https://simpleprogrammer.com/back-to-basics-what-is-an-interface/#comment-456 Thu, 05 Dec 2013 23:38:00 +0000 https://simpleprogrammer.com/?p=1152#comment-456 In reply to jsonmez.

How about using Java’s Powermock/Easymock classextension (which is part of the core since v3)/or alike to mock a drive method on a Car, wich gives the same flexibility of having that indermediary interface just to restrict which method can be called.

]]>
By: jsonmez https://simpleprogrammer.com/back-to-basics-what-is-an-interface/#comment-455 Wed, 31 Jul 2013 15:55:00 +0000 https://simpleprogrammer.com/?p=1152#comment-455 In reply to Stephen Byrne.

Thanks for the comment, I do see your point and the way you suggest makes sense.
I think it is a perfectly reasonable conclusion. We both can probably agree that interface abuse is bad and is prevalent today. Which is the real problem that must be tackled. Thanks for the insights.

]]>
By: Stephen Byrne https://simpleprogrammer.com/back-to-basics-what-is-an-interface/#comment-454 Wed, 31 Jul 2013 14:08:00 +0000 https://simpleprogrammer.com/?p=1152#comment-454 Very nice article, and although I agree with you in general, I’d be inclined to say that 1:1 interface=>implementation is not *necessarily* a sign of bad design…I take it to mean that in the following example:

public class Car:IDrivable
{
public void Drive(){Console.WriteLine(“Vroom!Beep!”);};
public decimal Fuel{get;set;}
public void OpenBoot(); //and many other badly designed methods/properties, etc.
}

public interface IDrivable
{
void Drive();
}

….all we’re doing here is using the language conttructs available to us in C# to declare to a consumer “look, the only thing I can guarantee about that IDrivable Car I gave you is that it has a Drive() method.” – i.e the “Contract” aspect of an interface.
Now if there were some way to take your concrete implementation of Car and specify the contract inline on the actual class members , then that’d be just fine – but pointless unless you absolutely only ever needed a single concrete implementation of that behaviour, because otherwise you would need some way to share this definition of contract in a central place…bringing you right back to the interface keyword!

Also I would disagree that using interfaces for Unit testing is in some way an artificial and unnatural thing – in fact it’s more natural to me if I have a class “GroceryShopper” and I want to test that it goes to the store and buys milk given a means of transport:
Surely a properly isolated unit test says “Drive that IDrivable thing I gave you to the store and get some milk” because we really only care about the milk, not the vehicle, as opposed to “Drive this Car to the store…” where we are now relying on having a working and callable Drive method that won’t throw, etc…and even if you use a stub Car object, how do you guarantee that the stub’s Drive method won’t drift in signature from the concrete’s method without some kind of…contract…to enforce it, thus rendering your tests meaningless unless you test objects by contract only.

Just my 2c 🙂

]]>
By: jsonmez https://simpleprogrammer.com/back-to-basics-what-is-an-interface/#comment-453 Wed, 24 Jul 2013 04:03:00 +0000 https://simpleprogrammer.com/?p=1152#comment-453 In reply to John Giannetti.

Thanks great comment. I agree about integration testing. Typemock is very nice, works like magic! 🙂

]]>
By: John Giannetti https://simpleprogrammer.com/back-to-basics-what-is-an-interface/#comment-452 Wed, 24 Jul 2013 02:57:00 +0000 https://simpleprogrammer.com/?p=1152#comment-452 Great post. I see interface abuse quite extensively in C# code these days. You even see ridiculous interfaces for what are really glorified data structures (example: ICustomer). When interfaces are used correctly, I think they express behavior not data. And, they don’t really have many methods. IEnumerable has only one method in its signature and IEumerator has three. And some think that reset was a mistake! This shows that interfaces are extremely powerful when used correctly, but used incorrectly they mostly get in the way.

I not am not real fan of unit testing since I develop data-base oriented applications. I just never seen a need there, but I have found integration testing to be useful. Apparently, typemock is a framework that allows you to test dependencies without using the standard dependency injection techniques. Here is a link that discusses this: http://www.infoq.com/news/2007/12/does-di-pay-off

]]>
By: Suhas Chatekar https://simpleprogrammer.com/back-to-basics-what-is-an-interface/#comment-451 Fri, 21 Jun 2013 20:03:00 +0000 https://simpleprogrammer.com/?p=1152#comment-451 In reply to jsonmez.

Ruling our interfaces completely is equally wrong as ruling out unit testing or dependency injection for that matter. All three have their strengths and bad spots if used in a wrong way. They all need to be balanced to achieve SOLID. It is not very difficult to come up with example to support any argument in the world of programming.

]]>
By: jsonmez https://simpleprogrammer.com/back-to-basics-what-is-an-interface/#comment-450 Fri, 21 Jun 2013 20:00:00 +0000 https://simpleprogrammer.com/?p=1152#comment-450 In reply to Suhas Chatekar.

Yes, there is certainly still a place for unit testing, but much of the unit testing I see today with heavy dependency injection and mocking really doesn’t test much at all.

]]>
By: Suhas Chatekar https://simpleprogrammer.com/back-to-basics-what-is-an-interface/#comment-449 Fri, 21 Jun 2013 19:58:00 +0000 https://simpleprogrammer.com/?p=1152#comment-449 In reply to jsonmez.

Are you serious about moving away from unit testing? Unit testing are most useful because quick feedback loop. Integration/Acceptance tests do not give you quick and pin-pointed feedback. I would rather review every usage of interface and make sure interfaces are not abused instead of abandoning unit testing altogether.

]]>