←back to thread

In Defense of C++

(dayvster.com)
185 points todsacerdoti | 4 comments | | HN request time: 0s | source
Show context
jillesvangurp ◴[] No.45273175[source]
C++ and C rely, heavily, on skill and discipline instead of automated checks to stay safe. Over time, and in larger groups of people that always fails. People just aren't that disciplined and they get overconfident of their own skills (or level of discipline). Decades of endless memory leaks, buffer overflows, etc. and the related security issues, crash bugs, data corruption, etc. shows that no code base is really immune to this.

The best attitude in programmers (regardless of the language) is the awareness that "my code probably contains embarrassing bugs, I just haven't found them yet". Act accordingly.

There are of course lots of valid reasons to continue to use C/C++ on projects where it is used and there are a lot such projects. Rewrites are disruptive, time consuming, expensive, and risky.

It is true that there are ways in C++ to mitigate some of these issues. Mostly this boils down to using tools, libraries, and avoiding some of the more dark corners of the language and standard library. And if you have a large legacy code base, adopting some of these practices is prudent.

However, a lot of this stuff boils down to discipline and skill. You need to know what to use and do, and why. And then you need to be disciplined enough to stick with that. And hope that everybody around you is equally skilled and disciplined.

However, for new projects, there usually are valid alternatives. Even performance and memory are not the arguments they used to be. Rust seems to be building a decent reputation for combining compile time safety with performance and robustness; often beating C/C++ implementations of things where Rust is used to provide a drop in replacement. Given that, I can see why major companies are reluctant to take on new C/C++ projects. I don't think there are many (or any) upsides to the well documented downsides.

replies(4): >>45273957 #>>45274194 #>>45275074 #>>45277154 #
lelanthran ◴[] No.45275074[source]
> C++ and C rely, heavily, on skill and discipline instead of automated checks to stay safe.

You can't sensibly talk about C and C++ as a single language. One is the most simple language there is, most of the rules to which can be held in the head of a single person while reading code.

The other is one of the most complex programming languages to ever have existed, in which even world-renowned experts in lose their facility for the language after a short break from it.

replies(2): >>45275179 #>>45275382 #
estimator7292 ◴[] No.45275179[source]
Have you written significant amounts of C or C++?

Most people don't write C, nor use the C compiler, even when writing C. You use C++ and the C++ compiler. For (nearly) all intents and purposes, C++ has subsumed and replaced C. Most of the time when someone says something is "written in C" it actually means it's C++ without the +± features. It's still C++ on the C++ compiler.

Actual uses of actual C are pretty esoteric and rare in the modern era. Everything else is varying degrees of C++.

replies(3): >>45275505 #>>45275849 #>>45278216 #
rramadass ◴[] No.45278216[source]
Right on the money!

Other then hardcore embedded guys and/or folks dealing with legacy C code, I and most folks i know almost always use C++ in various forms i.e. "C++ as a better C", "Object-Oriented C++ with no template shenanigans", "Generic programming in C++ with templates and no OO", "Template metaprogramming magic", "use any subset of C++ from C++98 to C++23" etc. And of course you can mix-and-match all of the above as needed.

C++'s multi-paradigm support is so versatile that i don't know why folks on HN keep moaning about its complexity; it is the price you pay for the power you get. It is the only language that i can program in for itty-bitty MCUs all the way to large complicated distributed systems on multiple servers plus i can span all of applications to systems to bare-metal programming.

replies(1): >>45284993 #
1. unscaled ◴[] No.45284993[source]
In practice, C++ is a language family more than a single programming language. Every C++ project I've worked on essentially had its own idiolect of C++.
replies(1): >>45285702 #
2. rramadass ◴[] No.45285702[source]
This is just a oft-repeated cliche and nothing more. Because C++ is a multi-paradigm language (with admittedly some less than ideal syntax/semantic choices) people overstate its complexity without much study/experience. Herd mentality than takes over and people start parroting and spreading the canard.

For the power and flexibility that C++ gives you, it is worth one's time to get familiar with and learn to use its complexity.

replies(1): >>45321697 #
3. pod_krad ◴[] No.45321697[source]
>people overstate its complexity without much study/experience

The is no need in any experience to have ability to estimate C++ complexity. C++ specification is about 1500 pages.

replies(1): >>45331814 #
4. rramadass ◴[] No.45331814{3}[source]
That is just silly; you don't need to read the entire specification to learn to use the language subsets productively as pointed out in my original comment here - https://news.ycombinator.com/item?id=45278216