5 ms·
I can count on 0 hands how many times I have used inheritance in the past 6-7 years. And if you don't count times that a framework requires me to use it to acco
by sdegutis 8y ago
I can count on 0 hands how many times I have used inheritance in the past 6-7 years. And if you don't count times that a framework requires me to use it to accomplish something, like React.Component or NSTableView, then that number goes back something like 15 years.
- pdimitar 8y agoIt boggles my mind that inheritance is still taught in schools and universities. It's so easy to debunk the common lovely idea of "Square and Circle are a type of Shape, Rectangle is a type of Square" that it's not even worth talking about it -- you might want the square to be rendered in a GUI framework but have the Circle rendered only in your PDF reports, being one very lame example I can think of in under a minute. Many others exist on the net and they are much better than mine. (Traits of types -- and thus multiple inheritance -- make much more sense if you so want to model the real world.) "OOP is necessary" is a lie that is worth billions of dollars. Inheritance ends up being leaky and non-isolated. Every time.
- sdegutis 8y agoInheritance works great in mathematics because then it's all about using some laws to compute derived properties, but in programming we use objects for behavior with side effects. So it's like apples and oranges.
- dan-robertson 8y agoI’m not really sure where you think mathematics is full of inheritance. Do you have an example? Off the top of my head: Subsets, sub-anything-else, not really inheritance Definitions of the form an X is a Y with the following extra properties, I think don’t really count as inheritance either. Example: A ring is a group with ..., but if you wanna to prove anything interesting about a ring, you can’t kust use the group properties or you would have proven a theorem about a group, and so the only useful thing about the group structure is that you know what it is. Any useful theorems will need other useful definitions (eg homomorphism) and these don’t get inherited and so the isomorphism theorem for groups doesn’t become the isomorphism theorem for rings “for free”. There’s also the fact that a field is a ring plus an extra property but most ring things become basically useless because much ring weirdness becomes trivial in a field and what is not trivial in a field is typically impossible in a ring, so knowing about rings doesn’t tell you much about fields. Example: a topological space is a set plus a subset of its power set such that ... but all the interesting things in topology typically have little to do with those sets and more to do with eg functions (but not just any set function) and the added properties. I suppose the theme is that one adds structure to (ie inherits from?) something because the new structure bans a lot of the weirdness of the bigger thing and allows for more interesting things to be done. The structure of the bigger thing is therefore not so useful because what is known about it is less relevant once more structure is imposed. In applied maths, the Fourier and Laplace transforms are part of a more general family of integral transforms yet this knowledge is basically useless in their application and it would not make much difference if they were not related. Fourier series fall into part of a bigger theory called Sturm-Liouville theory but in many of the places one uses a Fourier series this bigger picture is not relevant whereas the specific details about Fourier series are. This is not to say that Sturm-Liouville theory is not useful.
- wvenable 8y agoIf you're merely a consumer of libraries and frameworks then you won't use inheritance very much but if you're a designer of libraries and framework will use it almost all the time. I love that inheritance lets me customize 3rd party code and integrate with it smoothly; nothing pisses me off more than a "sealed" class that you can't inherit from.
- sdegutis 8y agoThat's exactly what I hate about Cocoa. Sure Carbon wasn't perfect, but subclassing NSTableView so that you can inject your subclassed NSTableHeaderView just to make some UI tweaks is awful. The entire framework is like this, and Swift doesn't help much. I get that it's a legacy design due to performance issues back from when Cocoa was created 25 years ago, but I would absolutely love to see a modernized macOS UI framework where functions were always used in place of delegates or subclassing, and where lexical scoping is the backbone of your app's own internal architecture instead of class hierarchies.
- wvenable 8y ago> functions were always used in place of delegates or subclassing I'm not sure what this means? In your modernized UI framework, how would you want to be able to make UI tweaks to built in OS/framework components? I've seen a few other system for customization and they all tended to be worse than just being able to subclass internal classes and override methods. Now I admit this can be terribly brittle but often the alternative is no way to customize at all.
- aaaaaaaaaab 8y agoI guess instead of overriding methods like “windowDidLoad”, you would set a function (I mean block) as “window.loadHandler” or something like that. I don’t really see why this would be better, to me the two approaches seem isomorphic...
- pka 8y agoUse higher order functions to customise behaviour. I.e. your hypothetical NSTableView would look like this: nsTableView :: { renderCell :: a -> NSView -- this is the function that renders a cell given an `a` , header :: NSView , footer :: NSView } -> [a] -> NSTableView a I.e. pass in a function that renders a cell for a given `a`, a header and a footer NSView and whatever else, a list of `a`s and get back a NSTableView which displays that list of `a`s. React calls this pattern higher order components iirc.
- sytelus 8y agoCan you elaborate on what you do when inheritance feels quite natural? A classic example would be to allow a plugin. For that natural thing to do would be to define base abstract class with bunch of overridables, utility methods and pure virtual interface methods. I can imagine it doing without OOP but it doesn’t seem as clean or natural.