4 ms·
I find interfaces are both over and under used. As others have noted, they are overused for things like mocking where we should be trying to create better alter
by flukus 10y ago
I find interfaces are both over and under used. As others have noted, they are overused for things like mocking where we should be trying to create better alternatives (like TypeMock), but they are under used at the method level. I always come across code where the best solution is a function like:
void DoFoo(IInterface bar)
or
T DoFoo(T bar) where T : IInterface
But what I find instead is the same functionality packed into an elaborate and delicate inheritance hierarchy. It's like an object implementing multiple interfaces is still alien to most developers.
- golergka 10y agoExactly. For example, in C#, it is still customary to use List<T> everywhere, although what the developer really needs is just IEnumerable<T> (which also guarantees that the collection will not be mutated).
- stevedonovan 10y agoRust (and Go) present an interesting challenge to people who have cargo-culted object-orientation. In Rust, data is dumb, and traits (aka 'interfaces') provide meaning. So you can go polymorphic with 'trait objects' or monomorphic with trait constraints on generics (i.e don't have to pay for virtual dispatch when it matters). The cool thing is that both languages allow interfaces to be _added_ in consuming code, they're not part of the definition as in Java.
- flukus 10y agoYeah I think rust did a really good job of avoiding the worst OO features but keeping the most useful parts. Combining data and logic was the most damaging idea the programming world has had. Once they're separated the code is easier to read, easier to test and easier modify.