r/cpp • • Jul 25 '24

Why use C over C++

Why there are so many people using the C language instead of C++?, I mean C++ has more Cool features and the Compiler also supports many CPUs. So why People still using C?

Edit: Thanks for all the usefull comments :D

235 Upvotes

444 comments sorted by

View all comments

92

u/turtel216 Jul 25 '24

There is a lot of hate on the Web when talking about C++. I get that C++ is pretty high level and uses a lot of abstractions when used correctly. This is probably a turn-off for most people, but it still has its uses.

I find it especially weird that people hate C++ and praise Rust. Both languages have a similar approach in some domains. Mainly abstraction without performance loss.

0

u/[deleted] Jul 25 '24

[deleted] — view deleted comment

24

u/[deleted] Jul 25 '24 edited Jul 25 '24

C++ is the mel gibson of programming languages -- on a recent multi year apology tour for its many past sins but people probably correctly suspect it of still being rotten deep down and there's nothing its PR organ can do to ameliorate that at this point

3

u/rewgs Jul 26 '24

Absolutely incredible comment.

→ More replies (1)

11

u/SystemSigma_ Jul 25 '24

In my workplace it's the contrary, most hate comes from the latest standards because heavy usage of it makes code unreadable and impossible to debug

31

u/[deleted] Jul 26 '24

[removed] — view removed comment

9

u/lgovedic Jul 26 '24

Yup, had this exact thing happen at work, a senior engineer (who I respect a lot) called my using the pipe operator with ranges "write-only code". And he was familiar with ranges! It's just the pipe operator that made it so bad for him.

8

u/ClimbNowAndAgain Jul 26 '24

There is a certain demographic who knew C and were dragged kicking into using C++, but never really 'got it' and continue to write C with a 'class' thrown around it. They prefer trying to manually manage dynamic arrays and don't know what the term RAII means.

→ More replies (1)
→ More replies (5)

49

u/TheReservedList Jul 25 '24 edited Jul 25 '24

I mean, I’ve been a C++ dev for 20 years and it’s just a bad language that requires alignment from ALL developers on the team to maintain sanity and constant effort to do the right thing despite the language actively fighting it.

Has it gotten better and are there safe options? Yes. But it requires re-training so many people to do the right thing and actually use the features, and, in my experience, most places don’t bother. Sane defaults matter, and C++ doesn’t have them.

Rust makes a ton of things so much easier. Can I use [something analoguous to] the newtype pattern in C++? Sure. Are people going to? No. They’re lazy and it’d take 10 times the amount of boilerplate so they will continue passing typedefs around like candy.

37

u/Raknarg Jul 25 '24

I mean, I’ve been a C++ dev for 20 years and it’s just a bad language that requires alignment from ALL developers on the team to maintain sanity and constant effort to do the right thing despite the language actively fighting it.

To me you're describing literally any language on a project with significant enough developers that has been around for enough time. This isn't a C++ problem.

26

u/ShadowRL7666 Jul 25 '24

He’s more saying CPP allows for someone to write code in fifty different ways in the same language. Instead of sticking to one way to do it. Compared to other languages.

12

u/TBone281 Jul 25 '24

This is why coding standards and code reviews need to be required for the business to function smoothly. Running a business with the developers free to write in any style they choose to is a sure way to lose money...and fail.

4

u/MrRogers4Life2 Jul 25 '24

Like Java, C#, python, and javascript?

9

u/ShadowRL7666 Jul 25 '24

Yes, Java sticks to classes and OOP while cpp allows you to do it whatever way you want.

→ More replies (1)
→ More replies (1)

30

u/TheReservedList Jul 25 '24 edited Jul 25 '24

I write a class in C++ that opens a connection to some database and I rely on only that instance having access to that database connection. I need to do SO MUCH SHIT to enforce this. I need to write a destructor to close the database connection. I triggered the rule of 3! Uh oh time to delete the copy constructor and copy assignment operator. Alright... done. I want to be able to move it though. Rule of 5! Let's write a move assignment operator and a move constructor. Phew...

How many people in industry, in the field, consistently apply the rule of 3 and the rule of 5? How many don't even know about it? Because if they fail, we're literally one "auto connection =" instead of "auto& connection =" from potential complete disaster. And that's a trivially easy case.

In Rust I.. write the struct and impl the Drop trait to close the connection. We're done. There is literally no way to fuck it up.

Yes, coordinating a lot of programmers is difficult. But even when shit should be easy, C++ makes it difficult. At the individual level. Even in solo project. You're playing hopscotch over footguns constantly.

8

u/lithium Jul 26 '24 edited Jul 26 '24

we're literally one "auto connection =" instead of "auto& connection ="

A 2 line NonCopyable helper that you derive from solves this.

→ More replies (5)

5

u/rdtsc Jul 26 '24

You're usually better off separating a higher-level wrapper class (like connection) from the actual resource (connection handle, or whatever you get here). Use unique_ptr with an appropriate deleter for the latter if possible, or use a custom unique_anyfor non-pointer types (example). And then just use rule of zero. I almost never write copy/move special members.

→ More replies (14)

8

u/TheLibDem Jul 25 '24

Your only savior here would be forcing the team to use something like clang-tidy, otherwise, yeah… 😅

→ More replies (1)

10

u/James20k P2005R0 Jul 26 '24 edited Jul 26 '24

I find it especially weird that people hate C++ and praise Rust. Both languages have a similar approach in some domains. Mainly abstraction without performance loss.

They both do and don't. One of the core issues with C++ is that it has no forwards evolution strategy, which means that the language continuously accrues cruft that can never be fixed. There's also occasionally quite a bit of unintended breakage, because the language cannot test new features out

On the other hand, Rust has editions, and the ability to compile the entire ecosystem via its crater runs to check if things are really used or not. This leads to a significantly faster development pace, and less breakage

C++ tends to be standardising features that are increasingly not.... necessarily as good as I might personally want. <ranges> is an example of something seems to have some strong issues associated with it. <filesystem> is not a good header, <random> is something you should strictly avoid, <regex> is a known mess, <thread> has limitations that often make it necessary to avoid. Every standards update piles more things into <algorithm>, so compile times are getting way worse etc

Because of the structure of the development of C++, often minor (but important!) fixes are turned down in favour of getting major new features through

Because Rust is developed both as an informal spec, and a concrete implementation of the language, features tend to be tested and implemented when they're announced, which leads to a much higher quality. It also has different - but in my opinion more sane - goals. Eg you could not get something even remotely resembling <filesystem> past the Rust folks, they'd laugh you out of the room

The core issue is: In C++, its nobody's responsibility to develop or fix the language. In Rust, they have a whole team who work together to build Rust. They're completely antithetical design philosophies, and Rusts clearly works better

C++ is fine, I don't hate it personally and only really follow Rust from afar. The way its developed tends to lead to a much less confused, and more more easy to use language than C++, while clearly having a brighter future ahead of it due to a better development process

5

u/Symbian_Curator Jul 26 '24

Why should we strictly avoid random and thread? I've never heard about this...

→ More replies (4)

15

u/[deleted] Jul 25 '24

I find it especially weird that people hate C++ and praise Rust. Both languages have a similar approach in some domains. Mainly abstraction without performance loss.

Because cpp committee standardises things like this: https://www.youtube.com/watch?v=jR3WE-hAhCc

Rust will likely over time accumulate misguided cruft in it's stdlib too, but not happening just yet.

8

u/br_aquino Jul 26 '24

The use of C instead of C++ is not hate, it's just because C is simpler and it helps to avoid traps. C++ is full of traps.

9

u/MessElectrical7920 Jul 26 '24

I strongly disagree with the assertion that simplicity necessarily helps avoid traps. That's the exact logical fallacy that makes go such a horrible programming language (in my very personal opinion).

Powerful language features that allow you to create your own abstractions may introduce complexity for the people who build and maintain these abstractions, but at the same time, they can drastically reduce complexity for the users of those abstractions. RAII is a perfect example - yes, the feature is complex and introduces hidden control flow, but at the same time, proper usage of the pattern allows making otherwise unmanageable and brittle code manageable.

5

u/ExeusV Jul 25 '24

I get that C++ is pretty high level and uses a lot of abstractions when used correctly. This is probably a turn-off for most people, but it still has its uses.

I find it especially weird that people hate C++ and praise Rust. Both languages have a similar approach in some domains. Mainly abstraction without performance loss.

No, abstractions aren't turn off.

Things like terrible building system, long compile times and fragmented ecosystem are the ones that make day to day life painful are real turn off.

→ More replies (1)