Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer composition to inheritance #71

Open
lukethehuman opened this issue Sep 3, 2024 · 1 comment
Open

Prefer composition to inheritance #71

lukethehuman opened this issue Sep 3, 2024 · 1 comment

Comments

@lukethehuman
Copy link
Collaborator

lukethehuman commented Sep 3, 2024

The current version of blobmaker relies heavily on inheritance. In general, it is preferable to use composition instead of inheritance.

The following video does a good job of demonstrating why and how to do so: https://www.youtube.com/watch?v=hxGOiiR9ZKg. In a nutshell, it can lead to expensive and messy refactoring when the inheritance tree gets sufficiently complex.

It might be that many instances of inheritance are justified, but it could be worth identifying where composition could be used instead to improve the code. The cases where inheritance is justified will be the cases where a composition pattern would lead to too many lines of copy and pasted code for the common functionality. Essentially, we have to trade-off between the rigidity of inheritance and the repetitiveness of composition.

@lukethehuman
Copy link
Collaborator Author

I have also found the following design pattern a useful one for this sort of tool: https://refactoring.guru/design-patterns/strategy. In this pattern, you have only one type of inheritance: implementations of abstract base classes. Again, this may be too extreme if some uses of inheritance are justified. But note that common code can also be wrapped in functions and used by different classes that way, rather than being inherited as a method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant