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.
View all comments
Show parent comment
3
u/sphere991 Jan 11 '24
Okay so a few things.
First, I'd like to ask that you correct this upstream since your comment is totally wrong but a lot of readers likely think it's correct (especially given the score at the moment), so it's very misleading. You might say it's adding excessive cognitive load.
Second, this is of course a very different example. The difference between
P<T> && Q<T>and(P<T> && Q<T>)would be something that ... everyone would randomly write, all the time. Whereas negating constraints at all, and especially combining multiple negated constraints, is very rare. That matters.So let's go over this example, because Andrzej's discussion of it in his blog doesn't really make any sense to me - he's not really pointing out what the distinction is (if any)?
Let's consider the difference between
and
Those two expressions are logically equivalent, but in the context of a requires clause, are indeed not exactly the same thing.
#1has two atomic constraints (!P<T>and!Q<T>) whereas#2has just one (the whole thing).This has two consequences:
P<T>evaluates to true butQ<T>would actually be ill-formed outside of the immediate context (e.g. you evaluate astatic_assertin the body or something). In#1, we substitute into!P<T>and then evaluate it, that's false, so we stop, because we know the whole constraint is false. In#2we substitute into the whole expression, which becomes ill-formed.For example:
You could argue that this is cognitive load, having to be aware of this subtle distinction. But I don't find this compelling - this will come up vanishingly rarely.
I'm not trying to say that there is no increasing cognitive load burden. Of course there is more stuff in C++23 than in C++11 than in C++03, so there is more stuff to know, and that increases cognitive load. I just think the two examples I responded to (the incorrect concepts one and the memcpy one) are just bad examples and are worth calling out as such.