logoalt Hacker News

gigatexalyesterday at 4:14 AM4 repliesview on HN

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

yxhuvudyesterday at 4:21 AM

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.

show 1 reply
vram22yesterday at 12:00 PM

>I’m gonna buy the book but I prefer composition over OOP.

The GoF book (the design patterns book) says in a page right near the start, "Prefer composition over inheritance", in the middle of an otherwise blank page, presumably to emphasize the importance of that advice.

As others have replied, composition is one technique you can use in OOP, not something that is the opposite of OOP.

You can also use composition in non-OOP procedural languages like C, by having a struct within a struct.

https://www.google.com/search?q=can+you+have+nested+structs+...

Toutouxcyesterday at 5:39 AM

Then you're going to be pleasantly surprised, because composition is actually a genuine OOP technique and Sandi Metz advocates for exactly this kind of sane OOP focused on encapsulation and objects making sense, instead of masturbating with class hierarchies.

show 3 replies
inopinatusyesterday at 4:37 AM

These are complementary not contradictory ideas. One of the principal takeaways from the Ruby edition (and many of Sandi Metz’s conference talks) is undoubtedly a mindset of, and techniques for, writing compositional OO code.