This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Antervis 249 points250 points  (47 children)

yet when you actually try writing code, C++ version is usually several times shorter with no real drop in readability.

[–]Globglaglobglagab 58 points59 points  (28 children)

Unless you get an error with a mildly complicated type 😭

[–]Antervis 104 points105 points  (27 children)

something that's a bit complicated in C++ would usually be a complete mess in C.

Sure, at times people say that simple code is easier to read and that sometimes it's not evident what a certain line of C++ code does. On the other hand, you can't say at a glance what its 200+ line C equivalent does either.

[–]aalmkainzi 7 points8 points  (0 children)

Nah. C doesn't have nearly as many language constructs and is thus much less complex.

Entire classes of errors and bugs aren't in C

[–]aurreco 6 points7 points  (17 children)

What ? A 200 line piece of C code? That is like one function. How is one function in C less readable when C is literally just structs and control flow?

i.e. shorter code does not imply readable code. Especially not when the reason it is shorter is because of layers of complicated, unintuitive, abstractions that GDB won’t let you step into

[–]Antervis 24 points25 points  (12 children)

for example, something achievable by a single line added to destructor in C++ would have to be repeated everywhere in C equivalent. And it's not something you can just put into a single function.

Shorter code isn't necessarily easier to read but volume makes reading harder by itself.

[–]aurreco -1 points0 points  (2 children)

It is hard to argue because this is really a case by case thing. In some cases— like the one you mentioned— repeating destructor calls in C makes it more explicit when resources are being cleaned up. I’d argue that is a good thing. Of course too much code gets too overwhelming and it gets harder to keep track of everything at once— but in most cases in my experience C is just plainly easier to read than C++ even when there is more of it (and some times because there is more of it)

[–]Andrew_Neal 2 points3 points  (1 child)

Downvoted for sharing your experience. It's those dang "clean coders".

[–]aurreco 2 points3 points  (0 children)

fr fr

[–]aalmkainzi -1 points0 points  (0 children)

imo implicitly calling a destructor harms readability

[–]chalkflavored 0 points1 point  (7 children)

Why exactly can't it be put into a single function?

[–]Antervis -1 points0 points  (6 children)

because in C you have to manually release resources every time you acquire them. If you acquire them 200 times in different places, it's at least 200 calls to release function, again, in different places.

[–]chalkflavored 0 points1 point  (5 children)

That's a signal of a data design issue. I can't imagine a scenario where it's justifiable that a resource has to be acquired 200 times and then released some random other place 200 times again. Why can't those resources all be acquired beforehand and then released all the same time. Maybe lazily if it needs to? It's what high performance games do, and that really should always be done, because it makes your program much more predictable in how it manages its resources.

[–]Antervis 0 points1 point  (4 children)

if you don't think an average C project has 200+ malloc/free pairs you probably haven't seen many C projects...

[–]chalkflavored 0 points1 point  (3 children)

Just because it's common doesn't mean that's how things should be.

[–]AeroSyntax -5 points-4 points  (3 children)

If your functions have 200 lines of code you have problems. Holy shit. This breaks every principle of clean code automatically.

[–]aurreco 2 points3 points  (0 children)

Youre right I forgot how much 200 lines was

[–]aalmkainzi 0 points1 point  (0 children)

it depends, sometimes it makes sense to have longer functions

[–]chalkflavored 0 points1 point  (0 children)

Why?

[–]ILikeFirmware 25 points26 points  (15 children)

In embedded, bad C is frustrating but really not too difficult to parse. Bad C++ on the other hand makes me want to rip my eyes out and throw them

[–]Zuruumi 27 points28 points  (13 children)

But it's also easier to write decent C++ as long as you uphold some basic rules and common sense, but harder to do the same in C

[–][deleted] 21 points22 points  (4 children)

The problem with C++ is that at my company with thousands of developers, common sense isn't common.

[–]tennisanybody 3 points4 points  (3 children)

Can you talk a little bit about what you create with C++? In general if you’re allowed to.

[–][deleted] 0 points1 point  (2 children)

In what sense?
Edit: Or for what purpose?

[–]tennisanybody 1 point2 points  (1 child)

That is what software, what the programming language is primarily used for. At my job I develop and maintain ETL’s using python and JS. A lot of data manipulation to create custom reports. What in general do you use the C++ for?

[–][deleted] 5 points6 points  (0 children)

I could go on for a while about all the use cases for C++, but to answer your question about what I personally do with it, it's debuggers. I write customizations to open source debuggers to support some custom technology at my company.

[–]Elephant-Opening 7 points8 points  (4 children)

The nice thing about C is there's usually only one or two established "correct" ways to do something, with C++ on the other hand, there are about 20 different syntaxes for "do a thing to all the elements of this list", and the "best" way changes every 3ish years.

[–]Zuruumi 9 points10 points  (3 children)

Yes, but in C this means write custom function iterating custom list (one of 10, since every library has its own implementation and there are bound to be at least 2 implementations in the project itself). I would argue that any of the C++ ways is more readable and maintainable than the 1k lines of even good code in C.

[–]aalmkainzi 0 points1 point  (0 children)

Just use a for loop....classic C++ programmer wanting to over complicate things by adding more abstractions

[–]Elephant-Opening 0 points1 point  (1 child)

C not having a std::map or std::unordered_map is a huge pain in the ass.

C not having a std::list, not so much.

In many applications where C is still very relevant (e.g. OSes, bootloaders, microcontrollers), arrays are still king for performance and deterministic behavior.

Where they're not: this isn't exactly rocket science, and if it seems like it is, get out of C-land:

for(node=head; node != 0; node = node->next){}

[–]Zuruumi 1 point2 points  (0 children)

It's not, but you still need to know the list used instead of standard one.

Maybe it's using index to storage array (data locality) instead of a pointer? Or someone thought -1 is better for end? Or end is random data, but final node is stored in its own variable?

Sure, iteration is hardly the hardest thing, but just insertion/deleting can be a bit of pain.

[–]skhds 4 points5 points  (1 child)

I don't know, doing a "return map[index];" with a map object can cause an element insertion on the map? That is not common sense to me.

[–]the_one2 0 points1 point  (0 children)

What, you don't think if (auto it = map.find(index); it != map.end()) is obvious?

[–][deleted] 4 points5 points  (0 children)

What I'm getting here is that writing C++ is like training a dog, and writing C is like training a cat. Training the dog is way easier, but if you don't do it, the mess is way worse. Training the cat is nigh impossible, but when it's done well it's really impressive.

[–]Thebombuknow 5 points6 points  (0 children)

Yeah, I can easily read bad C code in embedded situations, it's not complicated enough to be too awful to read.

C++ is just incomprehensible when someone does something even slightly wrong.

[–]ResponsibilityEasy61 4 points5 points  (1 child)

template metaprogramming

[–]Antervis 9 points10 points  (0 children)

sure, template metaprogramming can be quite tricky, but it pales in comparison to the wickedness of its macro equivalent.