Monday 24 October 2022

Why C++ is difficult

C++ is a horrible language. It's made more horrible by the fact that a lot of substandard programmers use it, to the point where it's much much easier to generate total and utter crap with it. Quite frankly, even if the choice of C were to do *nothing* but keep the C++ programmers out, that in itself would be a huge reason to use C.

Source is here. Many of you will recognize these words: it's a piece of Linus Torvalds' rant aganist C++, when some developers asked him to move Git from C to C++.


At the time I laughed satisfied at that, because I prefered for a long time C's semplicity, few keywords and easy concepts. I mean: when, in C++ should you use a struct rather than a class1? And after years developing in C++ I can confirm that it is a really complex language. If you want to have fun, this thread on Stack Overflow will explain something really interesting about the internals of C++14.

Is really that difficult?

I recently saw a video where Scott Meyers talks on a conference about the new (for the time) standard, C++14. It's a nice video and I totally suggest it. Expecially for what he says at 36:58.

«You're all sick!»
(Courtesy of YouTube, Christian Semmler channel)

And the I realized what makes C++ so difficult. Agreed, the language itself is truly complex, but decades of work has been invested on making it less difficult. Compare these two fragments, curtesy of the C++ Code Guidelines. A good one...
vector v(100000);

for (auto& c : v)
    c = ~c;
...and a BAD one.
vector v(100000);

for (size_t i = 0; i < v.size(); i += sizeof(uint64_t)) {
    uint64_t& quad_word = *reinterpret_cast(&v[i]);
    quad_word = ~quad_word;
}
Guess what? They do the same. But the first is more readable than the second one. Why somebody should choose to write that way a fragment that can be way more easier to read? Probably to gain some speed and that's what I will talk about now.

[C++ is] made more horrible by the fact that a lot of substandard programmers use it

C++ difficulties are caused by many developers approaching a problem the wrong way. In the name of abstraction and efficiency, they create mountains of overcomplex, unreadable code. Sure, not all of them, but if you're a professional C++ developer, you surely met the crazy colleagues (ab)using pointers math, ternary operator or templates metaprogramming.
And maybe you even admire them!
So let's face it!
I am a monster!
If your code is unclear it's because:
  1. You're abstracting too much;
  2. You're doing premature optimization;
  3. You're using feature of the C language that were usefull when compilers were inefficient (the '80s);
And that is linked to the code I reported from the C++ Core Guidelines: the first and more readable is also faster, because the compiler knows how to generate more efficient code.
And, sorry to say that, but doing this kind of errors, is what makes what Mr. Torvalds calls a substandard programmer.
So, what's really difficult about C++ is knowing how to do stuff the right way.

Conclusions

I wanted to end this post with a checklist, suggesting how to improve your C++ code. But I found a better rule of thumb, simple and clear.
Write it readable as a Python script
And you will have something not ugly, maintenable and, probably, even fast.
We could talk also optimization, but I think that is enough material for another post.


Footnotes
1.The most reasonable answer I found says that structs should be use only when we're interfacing with a C API. In all other situations we should use a class.

2 comments:

Anonymous said...

What a nice article! I loved it. You make me think of an old colleague of mine. He was very entertaining, knew to create a story around a topic to make people understand the problem. He was also an horrible person
Continue your good work dude

Anonymous said...

Maybe it is the problem of the C++ because it is too difficult to understand and that is why developers cannot make clear, readable codes.
Maybe I am wrong, because I only coded in Matlab :)