Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should you have an interface for all objects/classes

As part of process improvements we are trying to ensure that all our projects have suitable unit tests, as there is a bit of education required in house I am trying ascertain what exactly is the best way to ensure we are making our classes as 'testable' as possible.

I suspect that we will start moving down the Mock object route and with most examples I have seen they interfaces to mock the implementations of the objects. So my question is should we ensure that ALL classes have an interface from which they derive?

If not what would be the process you would suggest in identifying classes that should have interface to allow them to be mocked?

like image 941
Dean Avatar asked Dec 18 '22 05:12

Dean


1 Answers

No. Having interfaces on all classes / objects will only create an unnecessary overhead without producing any extra benefit.

The general rule of thumb is that your classes should depend on abstractions rather than concrete implementations, so I would suggest using "dependencies" as a starting point and any class that is an external dependency to another class should implement an interface.

like image 156
lomaxx Avatar answered Jan 07 '23 08:01

lomaxx