3 ms·
Interfaces in that case can be useful for unit tests since they enable mocking.
by sfblah 7y ago
Interfaces in that case can be useful for unit tests since they enable mocking.
- Quekid5 7y agoYou can do that with classes -- as long the dependents don't instantiate their dependencies directly. However, I personally like interfaces (well, traits, I'm doing scala), because it makes it much easier for readers to understand what bits are supposed to be part of the interface and which parts are supposed to be an internal implementation detail. Without interfaces it's really easy to accidentally change e.g. a return type into a more concrete one which accidentally leaks impl. details and suddenly other bits of code will come to rely on those impl. details. I work on rather large/complicated systems and I've seen this gradual spaghettification time and time again. Disclosure: I'm usually the person who gets called in to fix the mess -- so that might bias my perceptions here.
- tcoff91 7y agoWith Java you can mock concrete classes using mock libraries which can change a class to a mocked class at runtime.