by mark_something · 2mo ago · 0 comments
I develop in C++ for a job, and when I need to use a library written in C I always have a bad feeling about it.
They aren't, usually.
C++ has all the C problems, and multiples more on top of those. It's a broad attack surface - literally no one is going to claim to be proficient in every single C++ feature available to their compiler. It's also quite opaque to visual inspection (making double-checking with an LLM difficult as it needs whole-program reasoning instead of localised reasoning).
One of those languages is one of the most complex programming languages ever invented, with the largest breadth of features, any of which may interact with any other feature in subtle ways.
The other is one of the most minimalistic languages created, with a dev able to keep the language standard in their head for the most part.
The end result is equivalent C code is much more complex than C++. For example, you need to remember to call free every time, forever, in every function. In C++ you just use unique ptr or shared ptr and you’re done. It’s a simpler model, enabled by the more complex feature of RAII.
Something like std::vector isn’t hard to write in C, it’s impossible. The language semantics don’t allow it. So you have to do hacks and remember to free, over and over again forever. It only takes just one time forgetting, and that’s a memory bug.
I mean, consider a large codebase. How many free calls does C++ eliminate altogether? Thousands, maybe tens of thousands? You just need to forget one of those in C, or even just put it in the wrong place.
And that’s just one class of things. Generics are much more complex in C, too. The type system in C is more complex IMO because it can be so easily defeated at every turn. C gives you practically no guarantees, no tools, for anything. The result is you are forced to write extremely defensive code everywhere, complex code.
The analogy I use is to physical tools. A screwdriver is simple, but building a house with only a screwdriver is complex. A suite of power tools is complex, but building a house with them is simple.
The analogy I prefer is that it's safer to ride a bike down to the store than flying a space shuttle down to the store.
[EDIT: Added trailing 'down to the store']
That’s not to say C++ is a good language. It’s a rotten, no good evil language. And the same things apply to C# versus C++.
A garbage collector is much, much more complex than reference counting and RAII, particularly the CLR GC which is a beast. But the extra complexity makes applications simpler, not more complex.