this post was submitted on 02 Sep 2024
886 points (99.0% liked)

Programmer Humor

35292 readers
141 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 

Incase it doesn't show up:

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 13 points 7 months ago (1 children)

I have not heard this consensus. Definitely inheritance where the base class holds data or multiple inheritance, but I thought abstract was still ok. Why is it bad?

[–] [email protected] 6 points 7 months ago (4 children)

In 99% of the cases, inheritance can easily be replaced with composition and/or interfaces. Abstract classes tend to cause hard dependencies that are tough to work with.

I’m not sure why you would use abstract classes without data. Just use interfaces.

[–] [email protected] 3 points 7 months ago (2 children)

Perhaps we have a terminology mismatch, I tend to use abstract class and interface interchangeably. I’m not sure it’s possible to define a class interface in c++ without using inheritance, what kind of interface are you referring to that doesn’t use inheritance?

[–] [email protected] 1 points 7 months ago* (last edited 7 months ago) (1 children)

I don't think it's what the person you're replying to meant, but template metaprogramming in modern c++ allows the use of "duck typing" aka "static polymorphism" where you can code against an interface without requiring inheritance.

[–] [email protected] 2 points 7 months ago

Typically this is done with CRTP which does require inheritance. But I agree, you can do some meta programming or use concepts which can enforce interfaces in a different way. But back to the original comment that interfaces via inheritance are objectively bad, I don’t think there’s any consensus that this is true. And pure virtual interfaces and CRTP are both common use cases of inheritance in modern C++ codebases and are generally considered good design patterns.

[–] [email protected] 3 points 7 months ago (1 children)

Say List is an interface.

You have implementations like ArrayList and LinkedList.

Many of those method implementations will differ. But some will be identical. The identical ones go in the abstract base class, so you can share method implementation inheritance without duplicating code.

That’s why.

[–] [email protected] 1 points 7 months ago (1 children)

If the lists have shared components then that can be solved with composition. It’s semantically the same as using abstract classes, but with the difference that this code dependency doesn’t need to be exposed to the outside. This makes the dependency more loosely coupled.

[–] [email protected] 1 points 7 months ago (1 children)

In my example, how is the code dependency exposed to the outside? The caller only knows about the List interface in my example.

[–] [email protected] 0 points 7 months ago (1 children)

In your example, the declaration of ArrayList look like:

public class ArrayList extends AbstractList implements List {
}

The dependence on AbstractList is public. Any public method in AbstractList is also accessible from the outside. It opens up for tricky dependencies that can be difficult to unravel.

Compare it with my solution:

public class ArrayList implements List {
    private AbstractList = new AbstractList();
}

Nothing about the internals of ArrayList is exposed. You’re free to change the internals however you want. There’s no chance any outside code will depend on this implementation detail.

[–] [email protected] 1 points 7 months ago

That's not C++, which has more control over such scope.

[–] [email protected] 5 points 7 months ago (2 children)

How do you implement an interface in C++ without an abstract class?

[–] [email protected] 1 points 7 months ago (1 children)

I know at least three ways, one of them involves variadic macros.

You don't even need to look that far, take any sufficiently aged library, like OpenGL.

[–] [email protected] 1 points 7 months ago (1 children)
[–] [email protected] 2 points 7 months ago

Yet I still had an urge to explain an obvious thing. Because it's C++, so everyhing goes. There are even tools to auto-generate C++ interfaces, because of course someone decided that C++ is inadequate and must be improved using some kind of poorly-documented ad-hoc extension language on top of C++.

[–] [email protected] 3 points 7 months ago (1 children)

Ask Bjarne to add interfaces enough many times until he gives in.

On a more serious note, I’m not exactly sure what the best C++ practice is. I guess you just have to live with abstract classes if you really want interfaces.

[–] [email protected] 6 points 7 months ago (1 children)

An abstract class with no member variables serves the same purpose in C++.

[–] [email protected] 2 points 7 months ago (2 children)

The only problem is to ensure the entire team agrees to only use it like an interface and nothing else. But I guess that’s the only proper way to do it in C++, for now.

[–] [email protected] 3 points 7 months ago

That's not really the job of the language, though. If they can't read the design docs and source annotations, they don't really have any business touching anything.

[–] [email protected] 2 points 7 months ago

this seems like the only proper way to do anything in C++. it’s a language where there’s 5 ways to do 1 thing and 1 way to do 5 things.

[–] [email protected] 1 points 7 months ago (1 children)

The way I was taught was that you usually start off with only an interface and then implementing classes, and then once you have multiple similar implementations it could then make sense to move the common logic into an abstract class that doesn't get exposed outside of the package

[–] [email protected] 2 points 7 months ago

I usually break it out using composition if that’s ever needed. Either by wrapping around all the implementations, or as a separate component that is injected into each implementation.