6 ms·
I disagree, the minimalism of C results in writing overly complex code to solve simple problems. Things like type punning and bit hacks are common place in C.
by preg_match 8d ago
I disagree, the minimalism of C results in writing overly complex code to solve simple problems. Things like type punning and bit hacks are common place in C.
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.
- lelanthran 8d ago> 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']
- deleted 5d ago[deleted]
- preg_match 8d agoThe absolute danger of a space shuttle is much greater than a bike. But the absolute danger of C++ versus C is… equivalent. All the suspicious memory things you can do in C++, you can do in C. In fact, with a couple more added in C (type punning via unions, for instance, is not standards compliant in C++, there are casts for that). 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.
- lelanthran 7d ago> The absolute danger of a space shuttle is much greater than a bike. But the absolute danger of C++ versus C is… equivalent. Okay, let's pretend, for the sake of this conversation, that the absolute danger in the event of failure for both the bike and the space shuttle are exactly the same. My analogy still holds in this hypothetical case: the odds of a failure operating a space shuttle is much much larger than the odds of a failure when operating a bicycle. Using C++ is a much larger cognitive load than using C, same as launching and piloting a space shuttle is a much larger cognitive load than riding a bicycle. It doesn't matter that the space shuttle has hundreds, if not thousands, of safeguards built into both the vehicle and the process, while the bicycle doesn't have so much as a seatbelt; the bicycle is still safer to operate because of the lower cognitive burden. There are simply more rules to remember in C++ to ensure that you don't get exploited than in C. Ask the average working C++ devs about the danger between virtual vs concrete destructors in derived classes and half of them won't know what you mean. Ask about putting objects into a std vector, and maybe half of them will remember the surprise they felt when it first broke (because it worked until that point). C++ provides global ways of avoiding footguns, but those global ways are not easily inspectable in the local scope. The problem is the sheer number of C++ footguns is simply overwhelming[1]. There's a handful of C footguns to commit to memory (signed overflows, used-after-free, etc), and they're all visually inspectable in the local scope. When I need to a language better than C, I don't reach for C++. ------------------ [1] Even world-renowned C++ superstars with decades under their belt can't come back to it after a mere 2 years. You don't see that with C.
- chris_wot 5d ago[dead]