Computer
Is AI Really Taking Jobs? Or Are Employers Just 'AI-Washing' Normal Layoffs?
Read more of this story at Slashdot.
Linux Kernel Developer Chris Mason's New Initiative: AI Prompts for Code Reviews
Read more of this story at Slashdot.
CodeSOD: Wages of Inheritance
Tim H writes:
Some say that OOP was the greatest mistake of all. I say they weren't trying hard enough.
This code is C++, though Tim submits it as "C with classes." That usually means "we write it as much like C as possible, but use classes to organize our modules." In this case, I think it means "we use classes to punish all who read our code".
Let's look at an example. They've been anonymized, but the shape of the code is there.
class Base { public: enum class Type { derived_1, derived_2 }; Base(Type t) : t_{t} {} Type getType() const { return t_; } private: Type t_; }; class Derived_1 : public Base { public: Derived_1() : Base(Base::Type::derived_1) {} };This is what one might call "inheritance". You shouldn't, but you might. Here, the base class has an enumerated type which declares the possible child classes, and a field to hold that type. The child classes, then, must set that type when they're constructed.
This is inheritance and polymorphism implemented from first principles, badly. And you can see how badly when it comes time to use the classes:
void Foo(Base *b) { if(b->getType() == Base::Type::derived_1) { // do it } }That's right, they need to check the type field and branch, instead of leveraging polymorphism at all.
But this isn't the only way they've reinvented inheritance. I mean, why limit yourself to just one wrong way of doing things, when you can use two wrong ways of doing things?
class Derived_1; class Derived_2; class Base { public: Derived_1* getDerived_1() { return dynamic_cast<Derived_1*>(this); } Derived_1& getDerived_1_ref() { return dynamic_cast<Derived_1&>(this); } }; class Derived_1 : public Base {}Here, the base class implements methods to get instances of child classes, or more accurately, pointers (or references) to instances of the child class… by applying the cast to itself. The base class contains logic which casts it to a child class.
Once again, we've reinvented a kind of inheritance which requires the base class to know about all its derived classes. I believe the intended use here is that you may have a variable of Base* that is pointing to an instance of Derived_1, and you want to cast it to access the derived class. The problem is that it may be a pointer to Derived_2, so what happens when you ask for getDerived_1()? dynamic_cast will check the runtime type information- if you compiled with RTTI enabled. Otherwise you're flirting with nasal goblins.
Tim writes:
These "idioms" are inconsistently used everywhere in the codebase. The member function names really do have the "_ref" for the return-a-ref version.
Now, I know that they are running with RTTI enabled, and thus when they do a bad cast, dynamic_cast will throw a bad_cast exception? How do I know?
Double fun is when users report an unreproducible std::bad_cast.
As Tim points out, they do get bad_cast exceptions. And because this is a pile of bad choices and risky code, they can't trace why it happened or how.
Sometimes, things just blow up.
.comment { border: none; } [Advertisement] Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.Is the TV Industry Finally Conceding That the Future May Not Be 8K?
Read more of this story at Slashdot.
EU Deploys New Government Satcom Program in Sovereignty Push
Read more of this story at Slashdot.
What Go Programmers Think of AI
Read more of this story at Slashdot.
Anthropic's $200M Pentagon Contract at Risk Over Objections to Domestic Surveillance, Autonomous Deployments
Read more of this story at Slashdot.
Is Meta's Huge Spending on AI Actually Paying Off?
Read more of this story at Slashdot.
Bitcoin Drops 40% in Four Months. Bloomberg Blames Absence of Buyers and Belief
Read more of this story at Slashdot.
Walmart Begins Building Out Nationwide EV Charging Network Across America
Read more of this story at Slashdot.
When 20-Year-Old Bill Gates Fought the World's First Software Pirates
Read more of this story at Slashdot.
Fourth US Wind Farm Project Blocked By Trump Allowed to Resume Construction
Read more of this story at Slashdot.
Scientists Create Programmable, Autonomous Robots Smaller Than a Grain of Salt
Read more of this story at Slashdot.
Microbes In Space Mutated and Developed a Remarkable Ability
Read more of this story at Slashdot.
99% of New US Energy Capacity Will Be Green in 2026
Read more of this story at Slashdot.
99% of New US Will Be Green in 2026
Read more of this story at Slashdot.
China Executes 11 Members of Myanmar Scam Mafia
Read more of this story at Slashdot.
Five French Ubisoft Unions Call For Massive International Strike Over 'Cost-Cutting' and Ending of Remote Work
Read more of this story at Slashdot.
US Government Also Received a Whistleblower Complaint That WhatsApp Chats Aren't Private
Read more of this story at Slashdot.
AI Use at Work Has Increased, Gallup Poll Finds
Read more of this story at Slashdot.
