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.
View all comments
Show parent comment
2
u/crashcompiler Mar 15 '26
> When I was talking about value semantics, I'm mainly referring to it being rather unique for C++. [...] We all consider it basics.
I agree that every C++ developer should know about value semantics.
I had this problem 4 years ago, when I posted another C++ article of mine. The title was bad and I had to change it. I feel like "basics" implies that "these are the things you must know as a C++ developer, otherwise you don't have the right mental model".
What I mean by "basic" is more like programming fundamentals in the broader sense. What are common concepts between languages that you need to know. What is fundamental to computer programming as a whole because of the types of problems we are trying to solve. How well does this translate to code. How does the evolution of these concepts look like between C and C++.
I think I need to think of a better title. Ideally, you should be learning about value semantics from the standard literature.
> For lambdas, they are functionally the same as a class with operator(), yet it does require a lot less typing.
Yes, very good! I added a function object example and some text to the Lambda with Capture section. I've seen at least two instances of function objects in our code base. And I learned that you can pass a function object to `std::function` - nice!
> If you enable the compiler warning [...], your compiler will notify you here
I added a sentence that clarifies, as you said, that this will compile, the comparison might even return true, but it is most likely a bug. I noted that some compilers can warn you about this problem and added a footnote for the clang compiler flag.
> For me warnings are static analysis.
I will make a note to add mentions of compiler warnings when they are appropriate. But I cannot make any promises because clang is different than MSVC is different than GCC. There is just too much to unpack. We have also used static analysis tools like SonarQube, which pulls in stuff from the Core Guidelines and the SEI CERT Coding Standards.
> Coming back to ranged for, if you use it to rewrite all standard loops, the more complex situations stand out more.
Ha, yeah, like iterating backwards through a container ...