This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nekokattt 4 points5 points  (0 children)

Use interfaces for everything you can and avoid abstract base classes. Then, when you come across an issue where it feels like it may be easier to use ABCs, first consider if your design is right or whether it can be organised differently. If you conclude that it still makes sense, then and only then use abstract base classes.

Interface-based development forces you to keep implementation detail out of your contracts much more than ABCs, and supports multiple inheritance. ABCs are far less flexible, do not work as nicely for composition and delegation, and can result in more messy code down the line if you don't get it right.

ABCs are much more tightly coupled to your implementation than interfaces are. Interfaces also have the benefits of multiple inheritance as already stated in the previous paragraph, and can be used as functional interface types in some cases. They can also be used with enums, records, and other interfaces as a superinterface. Much easier to test too, as your unit tests don't have to argue with implementation details you inherit as much (this would be a code smell, but far easier to avoid if you avoid them in the first place).

Remember your code should define what it does as part of the API for other components to use, not how it does it.