Alternative title: mediocre C++ is arguably worse than mediocre rust.
I’d agree with that? I think the power and danger of C++ is that it assumes the programmer knows best. Rust assumes the programmer is wrong if it looks dangerous. So.. yes?
First, trivial: what's exactly mediocre in the Rust example? I don't think it's mediocre at all.
Now, the second, which is significant: what constitutes a "mediocre" C++ program? The author is the engineer who built the Meson build system - his code is representing the output of an experienced engineer.
This brings to the usual C/++ criticism: it makes it easy to make certain class of mistakes even for experienced programmers.
> First, trivial: what's exactly mediocre in the Rust example? I don't think it's mediocre at all.
A better way to phrase it would be "code produced by a middle-level engineer". It just so happens that in Rust a regular, adequate, middle-level engineer can easily write the best possible code because the language guides him to it, but in C++, a regular engineer still wouldn't know about some gotchas that would make his code inferior.
It's unknown unknowns: Rust shows most of them to you, while C++ stays silent.
> very good programmers who know the language well and what they’re doing
And never make a mistake.
Honestly, it's probably just because I'm not good enough, but I prefer to use my focus on improving the functionality of my code instead of ensuring any change I make is correct.
I've successfully maintained some 100k lines C++ code in the past. I would rather avoid that in the future.
Such programmers are mythical for practical purposes. Look eg at the constant stream of memory safety vulnerabilities in browsers and operating systems.
Nothing saves you in that situation. For example, some authoritarian ex-mediocre-programmer-turned-middle-manager who cares more about growing the team than shipping robust software could be the one choosing what language to write it in.
I'd argue that even very good C++ programmers make mistakes that can be harmfull, and that those mistakes will be hard to catch by the average programmer who will deploy the new code. That's in my opinion why on a small to medium project where objects are not needed, it is better to use C than C++.
> even very good C++ programmers make mistakes that can be harmfull
Are you seriously saying that this isn't also true for C, (and indeed for all programming languages)?
A challenge - try writing a program in C to read a text file containing lines of unspecified length, sort them, and output them in sorted order. Compare the C++ and C code and tell me which you think is the cleaner and safer.
To meet your challenge, think about the problem. How about this?
file size + 1 => s
allocate char buffer b[s]
read file to b (all data must be in memory, or merge sort. if on linux and allocation worked, read may fail! oom will get us, or we bail on read failure)
add final newline if missing
count newlines => n
allocate line pointers p[n] (if line pointers will not fit, we bail -- we could use offsets and fancy virtualization, if this is not a Z80)
fill pointer array p
sort => sorted[n] (if sorting fails, bail)
output sorted lines
Same for C and C++. UTF-8 is ok.
Do you want more data than will fit into memory? Even then,
C++ doesn't bring much to the table. Separating the sort algorithm? Yes C++ BEGINs to pull ahead by a bit.
safe? yes. easy to write? yes. no fancy bits? yes.
Transcribe to C C++ as you will. Java? won't be as pretty. Rust, Go? don't know.
Javascript. Not even close.
Since this is a "beginning programmer" problem, give a design that works for another language that makes sense to a beginning programmer.
Safe? If you never use any C functions that expect strings to end in a null character, maybe. (In your approach, the strings end in newlines, including, critically, the last string in the buffer, so the buffer is not null-terminated either.) So, your sort needs to not use strcmp, and your output needs to not use printf (or even puts).
I would argue that this is setting up for catastrophic failure. If not when writing, sometime later when maintenance happens.
But also, the Rust compiler teaches programmers to known their limitations so the Rust programmer will be forced to become a good programmer more easily than the C++ programmer.
I’d agree with that? I think the power and danger of C++ is that it assumes the programmer knows best. Rust assumes the programmer is wrong if it looks dangerous. So.. yes?