DEV Community

Cover image for Modern C++ Isn't Scary

Modern C++ Isn't Scary

Jason Steinhauser on August 16, 2019

C++ has a reputation for being an obtuse, antiquated language that is only for neckbeards and angry professors. Sure, it's fast, but why do you wan...
Collapse
 
aminmansuri profile image
hidden_dude • Edited

What's scary about C++ is not the syntax. Yeah it looks a bit weird these days and all but that's not the problem. (I programmed in C++ for 10 years)

The problem is best illustrated with books like "Effective C++" and "More Effective C++" aka. "The 100 things you have to bear in mind to avoid inadvertently creating a memory leak".

With the addition of more features in the language I'm pretty sure they've created another 100 things that you need to bear in mind to avoid implicit memory leaks. So the surface area of expertise needed to do this increases.

At some point it just becomes easier to select a cleaner language.

Collapse
 
serbuvlad profile image
Șerbu Vlad Gabriel

This is exactly it!! There seem to be 2 ways to do memory: the first is to let the developer handle memory like in C. The second is to let the language handle memory, like in GC languages or in Rust.

If you let the developer handle memory that's all fine. Using malloc() and free() is actually easy once you get used to it. You have to figure out when objects get created and when they get destroyed, and plan things out accordingly, but it generally works out. Which is why memory leaks in C programs are rare and, for good code, easy to fix.

Letting the language handle the memory is also fine. You get a performance hit, at least traditionally with GC (can't speak for Rust. haven't used it), but in most cases it's worth it to free the developer of having to think about memory usage, and allow him to spend more time thinking about other problems or writing code.

However, what you get in C++ is an attempt to get the best of both worlds. It does this, not by having some sort of unifying idea like Rust's ownership thing (again, haven't used), but by adding on features (stuff the developer has to worry about), as if that's going to fix anything. It seems only to be digging a bigger hole for itself.

Collapse
 
aminmansuri profile image
hidden_dude • Edited

The problem isn't with malloc and free (or new and delete in the case of C++).. but with things like implict object creating when returning values the wrong way, or by improperly setting up copy constructors and things like that. Or the need to make destructors virtual if you use inheritance (don't do it? memory leak)

If it only was a matter of new/delete then it would be simple, like C is.
C++ is unwieldy.. too many rules. There are better, more productive languages these days.

Collapse
 
serbuvlad profile image
Șerbu Vlad Gabriel

I am extraordinarily confused when it comes to the C++ community. On the one hand they encourage code reuse: it's OOP after all. On the other, code becomes obsolete if it's not using the latest practices (last 5 years).

Collapse
 
vimmer9 profile image
Damir Franusic

At some point, most languages take a turn for the better, or for the worse. Maybe C++ is finally heading in the right direction. Thanks

Collapse
 
xowap profile image
Rémy 🤖

I've been considering starting a C++ project about 5 minutes ago and what discouraged me in the end is the absence of dependencies management. Is there any good solution for that? Development inside Docker? An emerging dependencies management solution?

Thanks :)

Collapse
 
jdsteinhauser profile image
Jason Steinhauser

A lot of libraries are being built with CMake now, which makes it pretty easy to do dependency management. I've also had good luck with vcpkg from Microsoft. They're not as full featured as yarn, mix, or Maven yet, but they're a lot better than a decade ago!

Collapse
 
xowap profile image
Rémy 🤖

Oh yes CMake is definitely a step forward, but you still need a package manager (other that apt I mean). I've looked a bit, there is things like Conan coming up but I have no idea the actual value of them.

Collapse
 
delta456 profile image
Swastik Baranwal

Amazing post! I also wanted people to know that Modern C++ is great and should be used more frequently than Traditional C++ but there are very less people who know about it as no good books and documentation for beginners.

I myself have learned from 5 blogs, GitHub, drafting papers, some books etc.

Collapse
 
jdsteinhauser profile image
Jason Steinhauser

Excellent! I'm glad to hear that it was helpful for you!

 
jdsteinhauser profile image
Jason Steinhauser

C++ does have a ways to go to have the tooling that languages developed decades later have. I think a lot of it has to do with backwards compatibility, and honestly I'm glad to see languages like Rust and GoLang come along and try to address those issues in new ways.

And I agree, leave C alone! It does exactly what it needs to do, and quite efficiently.

 
aminmansuri profile image
hidden_dude

Other languages just take care of this stuff.. you don't need to worry about it.
C++ is by default leaky.. its like a gun that unless you aim it perfectly will always shoot you in the foot.
Its possible to write good code, but there's a lot to take in to get it right.

I'd say if you don't declare your destructor virtual, you've made a dangerous coding error and the compiler should tell you so. But everything is on manual in C++ (except for the proliferation of memory leaks).

 
vimmer9 profile image
Damir Franusic

Leave our beloved C devoid of any new unnecessary "features" 😁

Collapse
 
woubuc profile image
Wouter • Edited

Great post, as someone who has been eyeing C++ from the sideline I've indeed seen a lot of improvement with C++14 and especially 17, enough to make me want to start using it more.

I'm still waiting for modules to drop before I really dive into C++ though. The whole headers & include system is just a little too archaic and messy for my taste (coming from JS modules and Rust's module system).

Collapse
 
jdsteinhauser profile image
Jason Steinhauser

Modules are getting a little better. vcpkg from Microsoft does a decent job, and a lot of projects that are built with CMake can be included directly from their Github repos like GoLang.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

I spent a while in modern c++ I liked it but it's still really verbose. I am working in rust and find the experience more enjoyable. But don't let that stop anyone modern c++ really is easier.

Collapse
 
absinthetized profile image
Matteo Nunziati

Tbh I see this synthax way worse then the old one. But it should be my bias...

Collapse
 
jdsteinhauser profile image
Jason Steinhauser

I completely respect your opinion. I'm curious to know what you don't like about it though!

Collapse
 
absinthetized profile image
Matteo Nunziati • Edited

Well I started using c++ a lot of years ago and I've never found the new auto/for stuff really terse. When I say I prefer the old synthax I mean that in c++ I always iterate over indeces 'for(int i...)'.
Other languages are different. I heavily use 'array.map()' in JS, or a ton of 'foreach' in .net, and analogous constructs in Python.
But c++... No they have added more noise to my eyes. It is a matter of taste, nothing really technical: just taste.

The same goes with the whole pointer stuff. Ok, smart pointer are a thing. But when I code with pointers I prefer to stay low level... It chrushes my mind to understand all the cool new stuff and I end adding more bugs!

Let consider that nowdays I use c++ for hi perf computations only (machine vision) so the context can also add bias...

Collapse
 
isalevine profile image
Isa Levine

This definitely makes me want to (finally) take a look at C++. Thanks for sharing, this is awesome!

Collapse
 
aminmansuri profile image
hidden_dude

more like unnecessarily evil..

for example if you do inheritance you better remember to make your destructor virtual. Lots and lots of these rules..

Collapse
 
theodesp profile image
Theofanis Despoudis

Maybe, but try to convince my manager...

Collapse
 
jdsteinhauser profile image
Jason Steinhauser

What does your manager want to use? Or is he/she/they just wary of C++ in general?