Like, when do you actually pick behavior over inheritance in stuff youre writing? I mean, the question is out there, asking for real experiences.
For me, inheritance seemed okay at first, you know, building these trees that looked neat. But then they turn rigid, and youre stuck. It burns you a few times.
Shifting to behavior feels better. Its not about what the object is exactly, but what it can do. That changes things.
Protocols help make that clear, or interfaces in different languages. The idea is, if it acts like it can do the thing, then it works. Kind of like the duck thing, if it quacks like a duck.
I have this rough idea in my head. Use inheritance when you want to share the actual code implementation. But for protocols, its more about what everyone expects, the shared behaviors.
Still, Im not totally sure where to draw the line between them. It gets a bit fuzzy.
How do others figure it out? What are the signs that inheritance is going to cause problems down the road, like aging badly?
Having said that, I read (I think also from Luciana Ramalho) that inheritance can make sense when developing a library/package itself. Nevertheless, I would try to use static protocols as long as it seems possible. Inheritance for code reuse is an implementation detail. It can often be replaced by composition and delegation.
I hope this gives you a bit more context to the article and idea of using behaviour over inheritance.
go26•1h ago
sneakyPad•16m ago