r/cpp • u/crashcompiler • Mar 14 '26
Discussion of Code Structure and Code Complexity Implications of Basic C++ Language Features
After 10 years of programming professionally in C++, I came to realize that I generally prefer a simpler subset of the language for my day-to-day work, which mainly involves desktop application development.
Working in a 30 year old code base for so long, you get to see which design decisions panned out and which didn't. This lead me to think about the technical reasons for why certain C++ language features exist, and what long-term impact they have in terms of code complexity and code structure. The result is a somewhat lengthy and very subjective article that I would like to share.
You can find the article here:
https://slashbinbash.de/cppbas.html
The premise of this article is: if you use simple language tools you can concentrate on solving the real problems at hand, rather than solving language problems. This is very much inspired by listening to interviews with Casey Muratori, Jonathan Blow, Bill "gingerBill" Hall, and others.
I discuss several aspects of the C++ language like functions, structures, statements, enumerations, unions, arrays, slices, namespaces, classes, and templates. But I also go into topics like error handling, and ownership and lifetime. I finish the article with a chapter about code structure and the trade-offs between different approaches.
The goal of this article is to give the reader a sense of what code complexity and code structure means. The reader should be able to base their decisions on the technical aspects of the language, rather than the conceptual or philosophical reasons for why certain language features exist.
I'd be thankful for any feedback, corrections, and ideas that you have!
Note: I still need to clean up the article a little bit, and add a few paragraphs here and there.
29
u/UnicycleBloke Mar 14 '26
I think of C++ as a well equipped workshop. I use some tools routinely, some rarely, and some never. Another developer might use a different set of features routinely, and so on. On the other hand, a "simple" language like C has essentially no useful abstractions of any kind, so you have to reinvent them. This inevitably leads to complicated verbose and error prone code for non trivial problems. I would rather have a workshop with dusty corners than solve every problem with my granddad's rusty hammer.
My experience of Casey Muratori is that he is a ridiculous blowhard who does not write C++ in any meaningful sense but feels justified in making long and boring criticisms nonetheless. It's just prejudiced drivel.
11
u/kammce WG21 | πΊπ² NB | Boost | Exceptions Mar 14 '26
This can be misleading and an example is warranted. This will make readers assume that you cannot call those APIs directly from the concrete class. But you can, you do NOT need to have a ref or ptr to the base class to access those APIs. If you want to pass it polymorphically, via a function parameters and only access the bases APIs then yes, you must pass it as its base.
Object slicing can also happen with fully concrete classes as well.
7
u/FlailingDuck Mar 14 '26
I read some and skimmed others. I can see there's a lot of thought and effort gone into this, so first off well done. I wholeheartedly agree with your premise. Only a subset of C++ is needed to solve any complex project. But that subset differs based on the project and problems that need solving.
I'm not sure who the target audience is for this resource. I have a few more YoE on you so I, obviously, am not the target. But, if it's for informing a younger audience who knows less or, as you imply has read how to program but not the why, it's diving straight into topics that assume the reader knows what concepts you're talking about and not just at a surface level. Someone needs battle scars to have the prerequisite knowledge to understand where you're coming from.
I know you say they need a beginner C++ knowledge. I think they'll need intermediate or even expert knowledge. So my core feedback is bring in that audience by adding that background knowledge. Explain some of the specific decisions you made for specific problems you solved. You want to level up your audience and teach them something, I believe this is why programmers read technical blogs.
I tried to read some parts in detail. The RAII has bad examples and I'm not sure if you're suggesting using goto: statements is an actual valid consideration? Other sections could do with more code snippets/examples alongside your justifications.
I think what you've written has some merit that could actually be better served in a book along side teaching the concepts themselves, reframed with the justifications and tradeoffs someone might need to make for certain problems. I think one of the best resources for learning C++ is Barne Stroustrup's own book because he offers the tools with good explanations without hand holding the reader into having to think less, he encourages them to think more beyond what the book says.
As it stands this resource looks like a brain dump of the better or worse coding decisions you've seen or made over the years, with little explanation of what those design choices were or what problems they were solving.
8
u/JVApen Clever is an insult, not a compliment. - T. Winters Mar 14 '26
I fully agree with your premise and conclusion: 90% of the code can be written with 10% of the functionality (and it should).
Reading through, it is becoming very large such that it loses attention. It might be useful to split this in separate pages.
There are a couple of things that I'm missing or disliking:
I'm also missing a couple of design elements: C++ is a pass by value language, references/pointers should be marked.
I'm also missing compiler warnings/static analysis. For example, the enum comparison can be blocked with warnings as error
1
u/sheckey Mar 18 '26 edited Mar 18 '26
I like this kind of stuff. We also have a 25 year legacy code base. I find myself writing emails or md files sometimes that discuss some of these topics to bring everyone up to the same place. Usually we get there from the same shared experience, but sometimes you need to write something to hurry that along. I like these kind of "we used to do this, but then we did this, and now we do this and here's why" type of discussions, just like the Scott Meyers books of lore.
We additionally have the remote, embedded sort of considerations like mainly no use of dynamic memory, at least after startup or not continuously allocating and deallocating (even better avoid it so that memory addresses show up in the linker map for crash analysis form the field), etc. It's nice to have decisions like these mapped out, and you have added inspiration for me top do the same at our office.
Thanks!
-1
u/tartaruga232 MSVC user, r/cpp_modules Mar 14 '26
No chance I will ever read that. The colors are horrible.
1
u/zasedok Mar 14 '26
AI slop