[OT] C vs C++

Ali Çehreli acehreli at yahoo.com
Tue Aug 30 17:29:49 UTC 2022


On 8/26/22 14:16, Dukc wrote:

 > one can always fall back to C features when the
 > more complex features of C++ don't justify themselves.

It can be too easy to fall back to C without justification. Real code 
I've seen not too long ago:

    foo(("hello" + bar()).c_str());

Why the c_str() there I wonder... But don't worry: foo makes a fresh 
string to put it in a struct object member anyway. O... K..., I guess...

Another one:

   strncpy(obj.path, zar("blah").c_str(), FILEPATH_LENGTH - 1);

Four problems:

1) As mentioned, what the c_str()!

2) Faith-based programming by using obj.path's hard-coded array size as 
FILEPATH_LENGTH.

3) Where is the null terminator, buddy? Rather, when are you going to 
read the man page?

4) What happens if the path doesn't fit?

Isn't that way too many problems in a single line of C++ code?

 > Can it be that C++ is so complex

True.

 > that even conservative use of it makes
 > your codebase so unreadable that even the archaic C is a better choice?

I disagree. I had decided long time ago that C++ would always beat C by 
the following features alone:

1) Constructors and destructors

2) Exceptions

3) Templates

Except, you need to code to a platform where a C++ compiler is not 
available.

I say that under long experience with a very well-written C framework. 
It was trying to be C++ and that's fair but C is too cumbersome to use. 
Defining equivalents of vector, hash map, etc. with macros is amazing 
but gets painful very quickly.

I liked the bail macros though; they were imitating exceptions:

   err = foo();
   bair_error();

finally:
   // Do cleanup
   return err;

But correct usage of those goodies depended on programmer attention. For 
example, you had to have an 'err' variable in such functions, you had to 
'return err', etc.

 > There has to be HUGE downsides in C++ for this competent people to
 > resort to this drastic avoidance.

Let's not underestimate humans. It is much easier to rationalize staying 
with your favorite language than learning a new one. And C++ makes that 
choice easy.

Personally, I went to laughable extents to search for dislikable things 
in Rust. :/

 > it had been long since generally aknowledged: C++ guidebooks would
 > tell to avoid less-known language features absent strong reasons

There are the clearly failed ones like std::auto_ptr, std::strstream, 
etc. Other than that, C++ people carry the esoteric guidelines like 
badges of honor. I used to be one. I was proud that you could not write 
good C++ without having read 5 specific books. And it was an equity 
issue as well: Those books were not available to most of the non-English 
speaking people.

C++ is a language that requires one to read and learn and read and learn 
and follow at least 400+ rules to write correct programs. There is no 
other way to say it. And that is nothing but a failure. (Why am I 
bashing C++ again! Argh!) My respect towards leaders of the C++ 
community fell because they don't see any problem and continue to sell 
C++ as the best thing ever, "inventing" everything, etc. They even say 
things like "Another language is not a solution [...] Java tried..." 
Stomping language design just because Java failed is not an argument. 
How about getting back to what C++ tried and failed. And instead of 
saying things like "A language with reference types? No thanks!", C++ 
experts should say "A language with 400+ rules to follow? No thanks!"

 > This inconsistency in our attitude towards language complexity is
 > interesting in my opinion.

One word: Humans...

 > would you
 > rather use C or C++ in your job if you had to pick one

Hands down C++. And again, I used both of those languages very 
extensively in very good written frameworks.

 > What do you make of
 > that C++ complexity seems to be so appreciated and so at contempt at the
 > same time?

Humans... I did ask a Google intern once who was visiting from Europe 
and working on some awesome C++ project. He was an expert in C++ and was 
presenting his work at one of our local meetups. I asked him "Why C++ 
when there are so many new modern languages popping up?". He laughed and 
said "Because it's hard!".

C++ is carried forward by an amazing mass of very smart people who are 
in it for the standardese, the conferences, knowledge of the esoteric 
("do you know how you can create integer ids at compile time for 
somethings in your struct definitions and blah"), blog posts, etc.

Ali



More information about the Digitalmars-d mailing list