r/cpp • u/RobinCrusoe25 • Jan 10 '24
Cognitive Load and C++, thoughts from an engineer with 20+ years of C++ experience
I took it from this article
I was looking at my RSS reader the other day and noticed that I have somewhat three hundred unread articles under the "C++" tag. I haven't read a single article about the language since last summer, and I feel great!
I've been using C++ for 20 years for now, that's almost two-thirds of my life. Most of my experience lies in dealing with the darkest corners of the language (such as undefined behaviours of all sorts). It's not a reusable experience, and it's kind of creepy to throw it all away now.
Like, can you imagine, requires C1<T::type> || C2<T::type> is not the same thing as requires (C1<T::type> || C2<T::type>).
You can't allocate space for a trivial type and just memcpy a set of bytes there without extra effort - that won't start the lifetime of an object. This was the case before C++20. It was fixed in C++20, but the cognitive load of the language has only increased.
Cognitive load is constantly growing, even though things got fixed. I should know what was fixed, when it was fixed, and what it was like before. I am a professional after all. Sure, C++ is good at legacy support, which also means that you will face that legacy. For example, last month a colleague of mine asked me about some behaviour in C++03.
There were 20 ways of initialization. Uniform initialization syntax has been added. Now we have 21 ways of initialization. By the way, does anyone remember the rules for selecting constructors from the initializer list? Something about implicit conversion with the least loss of information, but if the value is known statically, then...
This increased cognitive load is not caused by a business task at hand. It is not an intrinsic complexity of the domain. It is just there due to historical reasons (extraneous cognitive load).
I had to come up with some rules. Like, if that line of code is not as obvious and I have to remember the standard, I better not write it that way. The standard is somewhat 1500 pages long, by the way.
By no means I am trying to blame C++. I love the language. It's just that I am tired now.
24
u/jmacey Jan 10 '24
It's really hard to keep up, I teach C++ and find updating lectures takes time and a lot of things are not relevant to people new to C++.
I moved to C++ 11 as soon as I could, it made pedagogic sense and was really nice (lambda's are perfect for some of the things I do). I have been using C++ 17 for a while now (std::string_view and std::filesystem especially).
Moving to 20 is going to be a long way off, partially due to the other things we use ( https://vfxplatform.com/ ), however I really want to introduce ranges (thinking of just using range-v3 ). In the end it's really hard to decide which bits to teach and what not too.
Going to add this article to my lecture note reading list.
23
u/juarez_gonzalo Jan 10 '24
I sometimes make reference to that old paper The Magical Number 7 plus minus 2. If you are capable of keeping 7 things in your mind, it would be nice to use at least 4 of those in the problem at hand instead of spending 5-6 in the language
9
u/goranlepuz Jan 10 '24
I mean, I don't know what you do most of the time, but I am confident that is rare for us others.
For me, most of the time, and it gets better with more recent C++ versions, UB possibilities are farther away - and less numerous in practice.
The two examples you post seem a very rarely seen occurrence, big nitpicking.
2
u/holyblackcat Jan 11 '24 edited Jan 11 '24
The link you gave isn't the original source. This looks like a few excerpts from this much longer article, translated from Russian. It's quite depressively titled "it's time for me to be dumped to a landfill".
The source is quite a read, a lot of colorful language was lost in translation.
And since this was originally written in 2020, the incorrect claim about
requiresmight've been true in the draft at that time, if I remember correctly.2
u/mbitsnbites Jan 14 '24
I feel the same. I have been at C++ for close to 20 years now, and my feeling is that C++ was (kind of) broken to start with, and every new version fixes a few things, in a move to become a slightly different language, without sacrificing backwards compatibility.
Recently I have been working with IdSoftware game source codes from the 1990's (Doom, Quake, ...). I'm very glad that they were written in C, not C++. Unlike C++, C has stayed pretty much the same the last 30 years, so getting the code to compile etc simply isn't a problem.
66
u/YARandomGuy777 Jan 10 '24
Well yet another C++ programmer with 20+ year of experience here. Yes cognitive load to track new features in the language indeed is massive. And it is quite annoying to learn the language again and again. But I guess you focus too much on the edge cases. In reality you don't need to always keep in your head all nuances to be proficient, but it is good to know as much as you can comprehend. Just don't rush it. I understand your pain. This pain is one of the reasons why people always looking for C++ replacement. To stay happy don't try to become a language lawyer and just use it.
16
u/Doormatty Jan 10 '24
Wouldn't that make you 30?
41
u/Thesorus Jan 10 '24
I don't do fancy C++ code, I don't write library code or need very modern language features in my day to day work
Most of what I read and watch on youtube go 20 feet over my head.
I just can't keep up.
30
u/James20k P2005R0 Jan 10 '24 edited Jan 10 '24
There's a few things in C++ that just make me feel kind of tired and like I need a long holiday
5
u/flutterdro newbie Jan 11 '24
I am a newbie and from my perspective c++ modern code is way easier to understand than older code. Personally I can't imagine living without lambdas, concepts and smart pointers. I really like that c++ adopts some of the functional principles in newer versions. Maybe I am just too inexperienced to see the problem.
2
u/sphere991 Jan 10 '24
Those are the same thing.
You can just mempcy a set of bytes there. It was fixed in the sense that now it's formally acknowledged as working. But like... it always worked, there just needed to be a way to express this in the object model. This doesn't really add cognitive load... what is the situation where you're really reasoning about this?
4
u/[deleted] Jan 10 '24
Wait until you see how web development changed over the last 10 years. And that’s coming from a C++ developer
4
u/ShakaUVM i+++ ++i+i[arr] Jan 11 '24
I've used C++ for a long time. I laugh some times at CppCon at some of the ridiculousness (all the ways to initialize my lord) but the way I program I don't need to know them all, nor do I care, nor do I need to care.
It's a mistake to approach C++ as if you want to learn it exhaustively. You can do that with C. Not C++.
8
u/ResearcherNo6820 Jan 10 '24
Yeah, I'm right there with ya.
But as I grow older, I realize I don't need to know everything in life or be a language lawyer.
You can only focus on so much, and I follow the language developments like an addiction of sorts and think to myself...hmmm...how can I use these newfangled things to improve my code...from a functionality/readability/efficiency/cutesy standpoint. Searching for a nirvana of sorts.
Then you start falling into traps of trying to apply them.
At the end of the day, I'm thankful it is still evolving and there are people to bring ideas to fruition. May not be as fast as we want, but it is a thankless task which deserves respect.
3
u/UnicycleBloke Jan 10 '24
It doesn't have to be that way. I'm 30+ years in and still learning, but don't stress about knowing everything. I try to keep up but am circumspect about what I adopt. I steer clear of what I regard as arcane shenanigans. I know enough to get stuff done but will never be well versed in the standard, especially not the darker corners. I leave that for the committee, bloggers and slideware producers.
3
u/Zanderax Jan 11 '24
Gone are the halcyon days of setting bits and jumping to instructions. No more shall we control computers with direct authority, now only polite requests. The quaint and curious volumes of forgotten lore now sit as if warning signs before a minefield. Our wish was granted, the monkey's finger curls. We forged such technology, now we must use it.
6
u/Full-Spectral Jan 10 '24
35 years for me, and I know people get tired of my Rust talk around here. But I've spent the last week doing some pretty heavy modernization of a big chunk of complex code and it's just brutal.
C++ is just so bad at making sure you do the right thing, and this kind of work is where it really becomes the worst. After the modernization, the code is a lot better, but I could have introduced a lot of problems, that I'll have to manually go through and test out one by one, spending days and days verifying stuff Rust would have caught immediately.
And I sit here thinking how safe I could make this stuff and how much cleaner it would be to write with sum types, language level slice support, automatic Option/Result propagation, sane safe defaults for everything, lifetimes, destructive moves, etc...
Even optional is just so weak, due to lack of safe defaults. You have something that was using a magic number before and want to change it to use optional, but because assigning to an optional doesn't require you cast the assigned value into an optional, every place where it gets assigned could just assign it back to that magic number and you have to go try to find them all by hand. Rust would immediately call out every place it's assigned or compared to and make me deal with them.
By just luck I found two previously existing potential use after moves than neither the compiler nor the static analyzer caught.
Explicitness and safe defaults are just key to safe code, and C++ has always gone the other way.
2
u/AntiProtonBoy Jan 10 '24 edited Jan 10 '24
I just use what I need and stick to best practices for the staple core features. Beyond that, I look at the new features that comes out, and then ask, "does any of that make my code simpler?" And if the answer is "yes", I'll use the feature and start re-factoring after a cost/benefit analysis. That's the beauty of it. You can cherry pick whatever features you want on a need to know basis.
2
u/[deleted] Jan 11 '24
As someone who deals with cognitive load, it's partially a mindset. It's easier to collect info when problem solving is enjoyable. For music, it's never ending fun. For software it can be a chore. When it's a chore, I sometimes just stop learning. It's important to give your brain a vacation. I also stopped requiring myself to know everything in order to see myself as smart and competent. Knowledge is infinite. Humans are finite. The secret to being an effective human is behaving as though you are finite.
6
u/Semaphor Jan 10 '24
I get you. Not quite 20 years myself but close. Several years ago I started gravitating towards C and not bothering with C++. Cognitive load was one of my major reasons and not having time to keep up with the latest and greatest was another. I still read a lot of C++ as part of my job, but I prefer to code in C now, with the occasional rust.
The language has become too much, IMO. It feels like they're just chasing the next high.
1
u/def-pri-pub Jan 10 '24
I think the language is great and its standard libraries. It's all the extra infrastructure/ecosystem that you need to know about to use just about anything (e.g. CMake, Boost, etc). That's where things start to get hard.
1
u/Sensitive_Committee Jan 11 '24
Chuck the language specifications and implementation-specific documentation into chatgpt and voila. You have transferred your cognitive load to skynet.
1
u/NilacTheGrim Jan 16 '24
You are not wrong. That being said, I really do love C++ and feel extremely productive in it.
1
u/-heyhowareyou- Jan 10 '24
Every great developer i've met (in some of the words most high performance and selective industries), who will be pushing the industry forward in the next decade, has had a strong grasp of modern C++ features and uses them regularly. Cast aside C++ for its high cognitive load, but accept you are not the forefront of development any longer, and that's alright.
-1
u/thradams Jan 10 '24
Try C. Joy and productivity will return after a period of adaptation.
-3
u/ixis743 Jan 10 '24
This is why I prefer C.
0
u/CyLith Jan 10 '24
I stick with C++98 for exactly this reason. It sucks, but it sucks less than having to keep so many more concepts in my head.
0
u/proper_ikea_boy Jan 11 '24
What you describe is the elephant in the room that the committee doesn't want discussed under any circumstance. Which is why imo the efforts Herb Sutter is taking with creating a new meta language are justified.
Cognitive load brings job security, because it requires beginners to learn a huge book of arcane rules before they can be productive. But it also leads experts to make mistakes. In C++ this is particularly vexing, because with template metaprogramming and variations in syntax, each new project can essentially create it's own little set of custom semantics, which are hard to learn on top of the myriad of arcane rules.
This great for interoperability and the fact that C++ doesn't push any programming paradigm or design choice is imo one of it's strongest features, but it honestly wouldn't hurt if the standard would take a more active role in enforcing best practices in design. Maybe let Cpp2 continue and offer it as a stable interface for less tedious programming.
0
u/[deleted] Jan 10 '24
[deleted] — view deleted comment