4 ms·
Or: OOP theory essentially says that classes should never be "subclassed." As in, the code isn't supposed to evolve that way; you're Doing It Wrong. In OOP, th
by derefr 9y ago
Or: OOP theory essentially says that classes should never be "subclassed." As in, the code isn't supposed to evolve that way; you're Doing It Wrong.
In OOP, the only reason for something to be a "subclass" of something else, is that you first had several subclasses (FooA, FooB, FooC) as plain classes, and then noticed that they all obeyed the exact same interface (a hypothetical IFoo) and had common logic, and so you then took the opportunity to factor out the interface + common logic into a common superclass (Foo).
The interface, if separated into an abstract contract (a real IFoo) is reusable by downstream code; but the common logic, 99% of the time, isn't. (It's potentially reusable by people contributing to your library, but more-than-likely they'll have to modify the "shape" of your common logic when they add a new concrete subclass with its own new concerns, even if the contract IFoo would stay the same.)
Given a full prover-level type system, you would be essentially unable to usefully do anything with the superclass Foo once it's been created, other than what the "causally prior" subclasses FooA, FooB, and FooC are already doing with it. Foo is the reificiation of an equivalence class whose members are exactly FooA, FooB, and FooC. A "FooD" shouldn't be able to be defined without changing the definition of Foo.
Assuming you accept this, how does code reuse work?
• Specializing behavior: this is what the delegate pattern (composition!) is for. Rather than SomeLib exposing a base class SomeLib.Foo for subclassing, SomeLib instead gives you a (final, sealed) SomeLib.Foo class which takes a SomeLib.IBar as a delegate, and calls methods on it. You create YourLib.Bar that implements SomeLib.IBar, and pass it to the constructor of a new SomeLib.Foo.
• Extending behavior: this is what plain-old component-wise composition is for. Call it an Adapter, or a Decorator, or a Facade; it's just a new object of a class you've defined, that is implemented by holding onto an instance of the (final, sealed) SomeLib.Foo class. (If you want nice interoperation, the upstream library author should provide a SomeLib.IFoo interface and define their methods to take/return it; and you should have your wrapper class implement SomeLib.IFoo. In less statically-typed languages, this translates to adding a contract method to your object that can be probed for, e.g. Ruby's #to_str.)
- rhapsodic 9y ago>Or: OOP theory essentially says that classes should never be "subclassed." As in, the code isn't supposed to evolve that way; you're Doing It Wrong. Got a reference for that? I've heard "prefer composition over inheritance" (GoF) but never have I heard that classes should never be subclassed.
- avinium 9y agoThe cleanliness and "refactorability" of my code improved significantly when I drastically reduced the amount of subclassing I was doing. I wouldn't go so far to say "never subclass", but I'd say most of the time, you won't need to subclass.
- hackits 9y agoI would say you `never never subclass` most of the time its just proves your design is turning into a taxonomy. You really don't need classes for a OOP language, its just simply a convention that developers use that is convenient.
- rhapsodic 9y ago>The cleanliness and "refactorability" of my code improved significantly when I drastically reduced the amount of subclassing I was doing. >I wouldn't go so far to say "never subclass", but I'd say most of the time, you won't need to subclass. Yeah, I would agree with that. A lot of OOP mechanisms like inheritance, or design patterns, get overused a lot. I'm not saying it's what you were doing, but I've seen it mostly in code written by newish programmers. It seems that once the concepts of OOP click in their mind, they get excited and eager to put them to work, so they use them where they're not really appropriate. I was definitely guilty of that, way back in the day. But once the novelty wore off, and I realized that I was just creating over-architected messes, I quit doing it.
- avinium 9y agoLearning Clojure was, IMO, the best thing I could have done to improve my development skills. It helps you realize that programming is simply about data, and how you manipulate it. Design patterns, OOP, conventions, etc are all just secondary tools that sometimes help you do that.
- Nition 9y agoThey're not saying never create subclasses, so much as never start with a base class and then create subclasses from it. The idea presented is that you should instead be starting with the specific classes you need, and if they have common operations, possibly creating a base class out of those.