101
you are viewing a single comment's thread
view the rest of the comments
[-] dragontamer@lemmy.world 19 points 2 years ago

Inheritance is useful.

However, "Dog isa Animal" is worse than useless, it actively hampers your code and makes your life worse.

Useful inheritance patterns are all over the place in GUI / Model View Controller code however. "Button isa Window", and "FileStream isa Stream", and "StringStream isa Stream" in C++. If you stick with SOLID principles, inheritance helps your code significantly.

[-] snaggen@programming.dev 9 points 2 years ago

But even there a TcpStream, a FileStream and a StringStream might have quite differen behaviour, since they all abstract very different things. So, even this simple example may fall apart very fast if you need to care about those. I'm not saying that Inheritance is always bad, but it is quite rigid and might cause problems in a large codebase if you suddenly run in to the corner cases where the assumptions the abstraction is based upon is no longer upheld.

[-] dragontamer@lemmy.world 5 points 2 years ago

But the fact that TCPStreams isa file-descriptor, Files isa file-descriptor, Pipes isa file-descriptor, and other such "stream-like objects" in the Linux kernel proves that the read/recv and write/send system calls are generic enough to work in a wide variety of circumstances.

Yeah, they're all different. But as far as the Linux API goes, they're all file descriptors under it all and have abstractions that work well for inheritance in practice. In many cases, inheritance doesn't work. But in many cases, it works. And works well, for decades.

[-] nous@programming.dev 4 points 2 years ago

Inheritance here is limiting. Composition creates a far nicer API. Take both go and rust, they use a set of different interfaces/traits that cover, Reader, Writer, Closer, Seeker. Not everything implements all of these and can implement any set of them. For instance, a File implements them all, as it can be read from, be written to, closed and arbitrarily seeked. But a TCP Stream cannot random seek to other locations inside it - you cannot suddenly reset a network connection to the start again, those bytes are lost. So even if they are both file deciphers they do have different behaviours.

And these can be used for more than just files and network connections - you can write into or read from a vector/list of bytes, even seek randomly into it, but cannot close it as that does not make any sense.

Most languages cannot have multiple inheritance - so you are stuck with a abstract class with all the functions on it with blank implementations so the unsupported functions become no-ops. But IMO that is just a hack to workaround the lack of being able to split these different types up.

[-] dragontamer@lemmy.world 4 points 2 years ago

You're not describing composition.

Go Files do not "hasa reader". You don't do file.reader.read(), you just do file.read(), that's inheritance as file has inherited the read() method.

[-] colonial@lemmy.world 8 points 2 years ago

You're confusing polymorphism for inheritance. read is a method on an interface that File implements - it is not inherited from a base class. You can use that File directly, or wherever a Reader interface (or whatever the name is, idk I don't really do Go) is expected.

[-] orangeboats@lemmy.world 4 points 2 years ago

Composition do not necessitate the creation of a new field like x.reader or x.writer, what are you on?

[-] dragontamer@lemmy.world 1 points 2 years ago
[-] platypus_plumba@lemmy.world 1 points 1 year ago* (last edited 1 year ago)

Man, I honestly have no idea why they are downvoting you. Composition literally means taking common behavior and placing it in external objects behind interfaces using a has-a relationship.

No idea why they are denying this. Inheritance vs composition is the same as "is-a" vs "has-a". In composition re usability isn't done via inheritance but code encapsulation.

Saying that in Go objects can implement many interfaces is the Interface Segregation principle from SOLID. Basically having small interfaces instead of a big interface with methods that may not be implemented by all implementors.

this post was submitted on 03 Jul 2023
101 points (96.3% liked)

Programming

17674 readers
39 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS