7

I can see how interfaces are very useful to force different classes to have the same functionality.

But there are also actions that are bound to a specific class (like updating an unique variable that is used to implement a method from an interface).

Should I write a new interface to implement the methods that aren't covered by the interface? If not how do I keep a good overview of the methods that are unique to a class?

PS: @gnat's duplication mark: I am not asking if every class should implement and interface. I am asking if every method should be defined in an interface. (or how to organise those so it is clear that those methods are unique to the class)

4

1 Answer 1

7

I would say no. I'm not sure what language this is but first of all, some (most?) methods on a class should be private. They are implementation details and should not be exposed at all.

So if we assume the is only about public methods, I still argue the answer is: no. The point of an interface is to define the methods that are to be used in contexts where the implementation class isn't known and doesn't matter. While it would be possible to create 'filler' interfaces that specify implementation specific methods, this is just cruft unless there is going to be some code referencing that interface in a implementation agnostic way. This will surely result in devolving into something approximating having an interface per class which is something I consider noise.

For example let's say I have defined a simple, read-only dictionary interface for use in an algorithm:

interface ReadOnlyDict<E> {
  E get(Object key);
}

My algorithm doesn't care about how you implement that method. Maybe it's a Map. Maybe you use a switch statement to call different methods on an object. Maybe it reads from a file, maybe it calls from the remote web service. In each of these cases, there could be plenty of things that the code creating those objects needs to be able to call to prepare it for use. Maybe those methods are defined by other interfaces and that's great. But if they aren't, creating an interface simply so that they are defined on one is a pointless exercise, in my opinion.

2
  • Really, if you have to sniff out whether or not a method is actually implemented on an interface, you need two interfaces, or you need an additional level abstraction - and likely another interface. If you really do care about implementation, then you don't have use for an interface. Apr 10, 2018 at 17:14
  • I agree completely. An interface documents (and/or enforces) a common public interface shared by multiple different implementations. Methods that are necessary for a specific implementation are not part of the (public) interface of that class, and thus should be kept private (or protected) and not exposed through an interface. It's also good practice to keep the "surface area" of public interfaces to a minimum, because this lowers the cognitive load and the complexity of the connections between different parts of the code (and of docs and tests). Apr 11, 2018 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.