25 ms·
My impression is that inheritance is in many common programming languages the easiest way to share code. Sharing code in another way would require some more tho
by cryptos 2y ago
My impression is that inheritance is in many common programming languages the easiest way to share code. Sharing code in another way would require some more thoughts and sometimes code, so the lazy programmer takes inheritance.
Traits as a general concept would be very useful in many programming languages, but only some (like Scala) have proper support for it. In principle a Java interface with default methods (implemented methods) is also something like a trait, but very limited compared to traits in Scala. I have no statistics, but my impression is that inheritance is much less used in Scala, because the language has an easy to use alternative.
Another example is Go, where structs with their associated methods can be embedded, what is exactly composition supported by the language. Since Go doesn't support inheritance, programmers obviously needs to use this approach, so you would never see inheritance in Go programs.
So, my conclusion is that the usage of inheritance depends on what the language supports and how easy it is to use.
- ehnto 2y agoTraits and contracts or interfaces, but even still inheritance has it's merits. Being fast and easy is a benefit, and I find defensive programming in extensible systems can benefit from a foundation of expecting inheritance.
- josephg 2y agoWhat do you mean by this sort of defensiveness? Can you give an example?
- ehnto 2y agoFor example in a plugin system, I can say you must inherit from BasePlugin and in code I can enforce that, BasePlugin can then have some protected functions that I can disallow from being overridden. It's good for frameworks and extensible systems where your code needs to guarantee certain things will happen when running third party code.
- Supermancho 2y agoTraits are increasingly common. https://en.wikipedia.org/wiki/Trait_(computer_programming) https://en.wikipedia.org/wiki/Trait_(computer_programming)
- whyever 2y agoRust also has traits, and no struct inheritance. I think it works quite well.