Is it necessary to use interfaces for small projects? I work at a shop writing small custom applications for clients, primarily data manipulation. I’m mostly self-taught but also took some programming classes in college. I’ve been trying to apply more OO concepts to my coding practices, but I really don’t see the need for interfaces in the work that I do. My largest project so far had around 10 classes in it.
Are interfaces necessary for small projects/applications or only for large-scale software systems?
EDIT: This is not a duplicate. I’m asking about using interfaces in general for small projects, not using an interface for when there is only a single implementation…
4
Short answer: No.
Long answer: There’s a trade-off between using a class and an interface. Interface implementations can be swapped at runtime. That’s nice because it’s flexible – but it also means someone can code up a bad IFoo
and pass it to one of your functions that otherwise works fine. With a concrete (and sealed
/final
) Foo
there’s only one implementation, so if you get it right, it always works.
Another drawback of interfaces is that they can’t inspect the implementation details of other members of that interface. Foo
methods can inspect the private variables of other Foo
s, but IFoo
s can only look at the interface of other IFoo
s. This can limit certain optimizations, so if there’s only one implementation of an interface it can hurt you without providing any benefits.
Unfortunately mainstream OOP languages don’t provide a way to swap class implementations. Ideally you’d be able to have multiple Foo
implementations and pick one of them at compile time. However, with an IDE it’s not hard to extract an interface out of a class after the fact.
If you don’t need to juggle multiple kinds of Foo
s in the same program there’s no need to add an extra layer of indirection with an interface. Even if you do need multiple kinds of Foo
s in the same program, you don’t necessarily need an interface. You can have an abstract base class with a private constructor and implement a finite number of types of Foo
s as static final
inner subclasses. See also How do you encode Algebraic Data Types in a C or Java-like Language.
Interfaces really shine when you don’t want to limit the number of possible implementations usable at runtime at all.
EDIT: For a more thorough explanation of the trade-offs involved, see On Understanding Data Abstraction, Revisited.
4
Even if you only have a small project, if you want to have unit tests, you’ll probably want to use interfaces for your services and dependencies. That way you can inject mock implementations of those in your unit tests.
If you’re not doing unit testing, then there are other common uses for interfaces, such as the strategy pattern.
1
define “necessary”
if two classes must implement the same interface in order to be interchangeably used by some object, then using an interface is technically necessary – though inheriting from a common base class can get around this (for languages that don’t support interfaces, for example)
the size of the project is irrelevant
2