Remix.run Logo
tomjakubowski an hour ago

> In my case, I often use object inheritance. It's a damn cheap way to DRY. However, when people hear "inheritance," they often think "polymorphism." There's a really big difference between the two, but popular culture has jammed them into one ball, and it's not worth the agita, to try to explain the difference.

I agree that we should think of inheritance and polymorphism separately. If we want to express this intent in object-oriented code, how can we use inheritance to deduplicate code, while preventing misuse of the resulting object hierarchy i.e. the use of base classes in a polymorphic context?

In C++, IIRC private inheritance would do the trick (you cannot static_cast DerivedWidget * to BaseWidget * if DerivedWidget : private BaseWidget), but most OO languages don't support private inheritance. It's also not possible, as far as I know, to "lock down" BaseWidget * so it cannot be used as a base class pointer from any derived class: instead, you have to apply the private inheritance to every derived class to enforce this rule.

Another approach is to use has-a instead of is-a: i.e. instead store a BaseWidget object as a member of DerivedWidget. This allows for re-use without supporting polymorphism.

ChrisMarshallNY 12 minutes ago | parent [-]

Or...we could hire folks that actually know how to write code, without screwing the pooch.

This is especially true, with languages like C++. Someone (I have heard it attributed to Bjarne, but I don't think he said it) said "With C, you can shoot yourself in the foot. With C++, you can blow your whole leg off."

In tech, we have folks that seem to be absolutely convinced that we can have tools, so marvelous, that we can hire total incompetents, and that they will magically write good code. I know of no other engineering discipline, or craft, where people think like this. They usually have rigorous career ladders, with lots of gates.

Maybe Finance sometimes lets knuckleheads behind the wheel, but then, you get things like the Barings Bank disaster.

"What's Barings Bank?" you ask. "It doesn't exist! Is it a hallucination?"

No, it is not. Unfortunately, they let a rather junior trader, named Nick Leeson, behind the wheel...

It's possible that LLMs may finally give us something like what people want, but I suspect that we'll be seeing folks stumping around on one leg...