←back to thread

249 points mattcollins | 9 comments | | HN request time: 0.387s | source | bottom
Show context
gigatexal ◴[] No.42190772[source]
I’m gonna buy the book but I prefer composition over OOP. I prefer to have an init that takes some params where those params are fully baked clients of whatever services I need and then the class just uses them as needed. I don’t see a lot of value in having a Python class that might have a few or more classes that it extends where all the functions from all the classes crowd up the classes namespace.

Class Foo.__init__(self, db, blob_storage, secrets_manager, …)

Instead of class Foo(DB, BlobStorer, SecretsMgr)

Etc

replies(4): >>42190800 #>>42190856 #>>42191086 #>>42193088 #
1. yxhuvud ◴[] No.42190800[source]
Why on earth do you put composition and OOP as opposing techniques? Composition is just one more technique in the OOP toolbox and there is nothing in OOP that mandates an inheritance based architecture.
replies(1): >>42191144 #
2. crabmusket ◴[] No.42191144[source]
Mainstream OOP languages (looking at you Java) have failed to make composition as convenient as inheritance.
replies(4): >>42191297 #>>42192325 #>>42192756 #>>42193209 #
3. flakes ◴[] No.42191297[source]
The common toolkits today (spring boot, google guice, etc) are much more focused on composition over inheritance, by injecting arguments and implementing pure interfaces rather than extending base classes. Older legacy Java frameworks and bad teachers are more at fault than the Java language itself IMO.
replies(1): >>42191310 #
4. crabmusket ◴[] No.42191310{3}[source]
I take your point, though having `extends` as a first-class language feature surely encouraged that culture and approach in older frameworks right?
replies(1): >>42191327 #
5. flakes ◴[] No.42191327{4}[source]
There are some valid cases where extends really can help, and IMO the language would feel limited without it. Maybe if the language designers had their time back they could have taken an approach like Golang with nested structs and syntactic sugar for calling their attributes/methods.

The main reason I see new devs opt for extends, is because that was 99% of the content in their Java 101 programming course, not because it exists in the language. Imagine how many more `friend`s we would have in cpp if that was crammed down everyone's throats? :)

6. yxhuvud ◴[] No.42192325[source]
How is composition inconvenient?
replies(1): >>42197858 #
7. _old_dude_ ◴[] No.42192756[source]
Very true, in Java, at least in the last 20 years, inheritance is de-facto deprecated, all new bits and bolts like enums, annotations, lambdas or records do not support inheritance.

So you have to use composition.

8. watwut ◴[] No.42193209[source]
How is composition in Java inconvenient?
9. mdaniel ◴[] No.42197858{3}[source]
Contrast the Java way

    class Delegated implements Base {
        final Base b;
        public Delegated(Base b) { this.b = b; }
        @Override
        public void printMessage() { b.printMessage(x); }
        @Override
        public void printMessageLine() { b.printMessageLine(x); }
with the Kotlin way https://kotlinlang.org/docs/delegation.html#overriding-a-mem...

OT1H, yes, sane people using IJ would just alt-Insert, choose delegate to, and move on with life. But those misguided folks using VS Code, vim, or a magnetized needle and a steady hand would for sure find delegating to a broader interface to be a huge PITA