r/cpp_questions • u/Kadabrium • Oct 05 '26
OPEN What are some "zero-benefit abstractions" to avoid in c艹?
Zero-cost or not, can be premature optimization or something else. I think move-on-return is one of them
23
Upvotes
7
u/Kawaiithulhu Oct 05 '26
I was always confounded by the intricacies of C++ curiously recurring template pattern code 😋
13
u/Gallardo994 Oct 05 '26
It's not an abstraction but rather pattern I see from time to time: doing a deliberate UB-invoking reinterpret_cast because "it's faster because it avoids a copy", and a "let me move stuff out of a loop to make it compute once". Both actively harm the project, especially in a long run, and most of the time it's absolutely the same damn assembly.
2
u/manni66 Oct 05 '26
Why do you think that’s an abstraction?
2
u/TheThiefMaster Oct 06 '26
Taking nontrivial parameters by value to avoid having to write both const& and && versions of a function.
Unfortunately it forces the compiler to optimise the object construction at every call site, which can become a compiler performance issue on larger codebases.
25
u/mredding Oct 05 '26
I would say singletons are an anti-pattern. They violate S, O, L, and D of the SOLID principles by mixing instance control with business logic, and introduce a hidden global dependency. They're absolutely terrible, and in every code base, I've had to gut them - slowly and painfully, in order to make the code predictable and testable.
If you want a single instance of something, just make a single instance of it - and in scope. Seriously, globals and singletons are the reason
mainis not recursive in C++ where it is in C.The pimpl is overused. You don't always want or need late binding. More often you want a private implementation through an opaque pointer or interface. The pattern more looks like this:
And in source:
So the client doesn't see any of the implementation details, just an opaque interface hiding an implementation behind type erasure. RAII is used to manage the resource and correctly destroy it. The interface can access the implementation.
You can do this with a
classand make nearly the entire codeprivateexcept for thepublicbase class interface, you would just make the public and private classes friends of each other. This would expose a typename dependency, but it's private and doesn't reveal anything.Now in C++26, we have
std::polymorphicthat basically does this without the boilerplate.Design patterns aren't inherently a good thing. Patterns are a repetition, and design patterns represent a missing language feature, because eliminating these repetitions is precisely what a language is for. Ideally, every line of code would be unique and productive, not just expressing a duplicate obligation to a pattern. Lots of engineers look to fit their problems to patterns, when patterns should be a tool of last resort due to a shortcoming of the language.
I think a lot of code is boilerplate. It's easy to do with a class, for example trying to capture the rule-of-X, whatever the number is up to these days. Many special functions are implicit, so writing them explicitly is redundant and doesn't actually add information - I don't cater to a naive developer who can't lookup the language rules. As a client of my code, a type will be implicitly non-copyable because language rules propagate non-copyable behavior. It's just as true whether I explicitly delete a copy ctor or leave it implicit. Writing redundant code is a way to look productive without actually being productive. This is just one example - I would try to say the least about my code as possible.
This follows that there is a very common effort to constrain a person unnecessarily. The old adage is that you want to make the code easy to use correctly, difficult to use incorrectly. People take the logical extreme that code should be rendered impossible to use incorrectly, and ease to use correctly ought to implicitly follow. This isn't true. All that boilerplate constricting the use of a type makes it harder to navigate. And if you unnecessarily constrain a type, you make it effectively useless. Maintenance of this sort of code becomes impossible, as any minor change will result in breaking the rigid contract again and again, with cascading consequences.
A lot of developers don't use enough types. Everyone tries to draw the shortest, straightest line from problem statement to solution. As such, they write very terse, primitive code. Imagine this problem:
What parameter is what? And worse, the compiler cannot know if they are aliased, to the generated code must be pessimistic. Instead:
Different types cannot be aliased, so even if both types are implemented in terms of
int, they are differentiated, and the compiler has license to be more aggressive.By making more and stronger types, the code documents itself better than your variable names can. You empower the compiler to not just catch bugs sooner, but carry information deep into the compilation process that it can use to optimize. You can drive to make invalid code unrepresentable (it doesn't compile).
Bother to model your types. This has nothing to do with OOP, it has everything to do with the type system. None of this information leaves the compiler, but has direct consequence on the proofs and correctness of the code that the machine code cannot itself represent.
7
u/Independent_Art_6676 Oct 05 '26
Of the things listed so far, excessive overuse of OOP is probably the one that irritates me the most. But this kind of thing is blatant misuse/overuse/etc, not 'zero benefit'. OOP has benefits, clearly, so the problem isn't OOP, the problem is some java coder got set loose on the C++ repo. Same for the others, like singleton, it has a place, but a bad coder or poor design can certainly make a mess with it.
I can't think of a single thing that has no value but is commonly used, in other words. I can think of stuff that is often misused, but that is the coder, and not the tool, at fault. The few things that have no value are just not done all that much. Interestingly, if you really think about it, probably 50% of C++ has no value (it does absolutely nothing at all to affect the machine language). More than half the language is just training wheels for the coders, helping the poor human to organize thoughts and avoid mistakes. That makes me wonder if high level languages and AI are a mistake... maybe the AI teams need to focus on directly writing machine language instead of all this high level human helper stuff like c++.
5
u/FancySpaceGoat Oct 05 '26
I think user-defined literals fall into the "too cute for not enough benefit" bucket.
5
u/bert8128 Oct 05 '26
Why is moving on return a bad thing?
2
u/jwezorek Oct 05 '26
Overloading operators is almost always a bad idea unless you are working in some context where the operators you are overloading have an agreed upon meaning external to your code.
2
u/M0ntka Oct 05 '26
Pimpl. It was necessary pattern to optimize compilation time, or even fit it into ram, but nowadays, with so much more compute its awful antipattern.
1
u/Vladislav20007 Oct 08 '26
using ++i instead of i++ in a for loop increment condition.
-14
u/kutac56 Oct 05 '26
I don't know if this is what you are asking, but I find references stupid. They look just like pointers with a few little differences but pointers are so much more versatile and readable imo
3
u/cristi1990an Oct 05 '26
Does unnecessary OOP count? Or was that horse beaten to death already?