DEV Community

C++ is awesome, here's why...

Vinit Jogani on July 24, 2021

C++ is, hands down, one of the most misunderstood languages in software developer pop culture today. People often compare it to C because it is a...
Collapse
 
codenameone profile image
Shai Almog

Counterpoint: there are way better languages.

C++ is unsafe. Rust is better while still giving low level control and performance. Not to mention VM languages like Java...

C++ isn't faster than Java, Go, Rust, C etc.

C++ libraries aren't even remotely close to the stuff you get with Java, Python etc. Even basic stuff is pretty hard to do. Hell, I had a job to fix an XSLT processor in C++, it would have been easier to just rewrite that whole thing in Java in a couple of hours.

C++ tooling isn't nearly at the level of Java. Neither in terms of debuggers or in terms of static analysis, refactoring etc. The closest thing is visual studio which is a proprietary windows app. Even jetrains tools lack when it comes to C++, it's just too hard even for the tools.

C++ still isn't portable in 2021. Yes you can code defensively but it's still bad and you end up in Macro hell with cmake configuration from hell.

C++ takes ages to compile because of features such as templates... When it fails the compiler output is downright unreadable.

C++ has a lot of hidden features/behaviors that are often compiler specific. It has nuances that are easily triggered and fail in odd ways.

C is much simpler, doesn't suffer from template bloat and compiles faster. It's more portable (albeit also with its own problems).

I liked C++ a lot in my youth. As I grew older and had to maintain code on multiple projects the pain of C++ became very apparent and I learned to hate it deeply. When I last needed to write a JVM I used pure C and it was remarkably easy. C++ is just unpleasant to work with.

Collapse
 
vnjogani profile image
Vinit Jogani

Ultimately you're entitled to your opinion but there are some things I disagree with.

Like I mentioned in my article, tools like Bazel make builds significantly fast. I personally don't prefer using CMake. There are static analysis tools like Kythe that also work beautifully with C++.

Like it or not, virtualization has a performance cost and a simple Google search would confirm that this makes C++ faster than Java at least. For many use cases, the advantages may outweigh the costs and that's fair. I personally love the Erlang VM and all the guarantees that it provides and I'd much rather use Elixir/Phoenix than C++/OpenMP to build real time distributed apps.

If you like the simplicity of C, there's nothing wrong with that. It just comes with its own memory management nightmares when you need to work with the heap. C++ simplifies that a lot without losing performance advantages of C. There are other things like function pointers which are wonky in C but much better in C++. Also, for larger projects, OOP makes it much easier to design systems IMO, but that is a personal preference.

Depending on how you define portability, C++ actually can run on any system and if you don't use very low level features (which you can't use in Java/Python anyway without C/C++ shared libs), it really does compile and run seamlessly.

CLion and Visual Studio suffice all my general needs when writing C++, but I have friends who use VSCode with C++ extensions as well and that works well for them.

C++ is only unsafe and unpredictable if, like I said, you don't know what you're doing. Languages like Java take thinking away from you by putting all objects on the heap and only maintaining pointers. If you know what you're doing, you might not want that in some cases. This causes issues with Garbage Collection being extremely slow.

Again, in the end it comes down to preference. Whether or not you find a language beautiful and elegant is subjective and while I've tried my best to convince you with this article that it's not as unpleasant as most people remember it to be, I don't mean to belittle any other languages. They're all awesome and I use several of them all the time.

Collapse
 
codenameone profile image
Shai Almog

Fast is subjective but inherently C++ compilation is MUCH slower than most languages. There's just a lot more to evaluate and unpack. This is an inherent limitation. It might be OK for smaller projects but even at 100k LoC this is very noticeable.

I pretty much disagree with most of your points but this one bothers me the most:

C++ is only unsafe and unpredictable if, like I said, you don't know what you're doing. Languages like Java take thinking away from you by putting all objects on the heap and only maintaining pointers. If you know what you're doing, you might not want that in some cases. This causes issues with Garbage Collection being extremely slow.

Two things bother me here:

  • Thinking we're smart - that's a dangerous path to walk. All you need is one lapse of judgement, one botched code review, one weak link and boom. I worked with a lot of very smart people who wrote dumb code a few times. The assumption that we're smart or that we "know what we are doing" is a flawed assumption. You're also making that assumption of every team member, every dependency and library you use and every update you get...

  • GC is slow - This is a common falsehood that derives from bad implementations and configurations.

GC allows super fast allocators in Java. In fact it beats some of the fastest C allocators. The cool thing about GC is that de-allocation can happen asynchronously on a separate thread during CPU idle time. That's very efficient...

The points where this becomes a problem are:

  • HUGE heap sizes
  • Low memory mode where GC thrashes

Newer GCs deal well with both of those cases through multiple strategies that include concurrency, generational division etc. The thing is that memory management becomes a "deployment decision" and not something the programmer should decide during development. You can profile the performance of your application in runtime and fine tune the GC/memory to produce predictable high throughput results.

Thread Thread
 
vnjogani profile image
Vinit Jogani • Edited

I respect the fact that you have had different experiences than mine, so I'm not going to argue with your assumptions. I've worked on C++ and Java apps at Google scale and in my experience, C++ builds have been no slower than Java builds (with the right tooling, like I mentioned before). But again, there are a lot of infrastructural factors that can come into play that can influence your experience.

Things like fine-tuning GC just defers the problem instead of solving it. Furthermore, C++ being a compiled language, it does in fact run faster than bytecode in production. Again, it's a tradeoff that you may be willing to make but it's a bit extreme to impugn that the tradeoff doesn't exist.

I'm not assuming there isn't going to be a lapse in judgement at any point. In fact, if you want to play it safe, you can just use shared pointers all the time and avoid all the memory allocation risks that you'd otherwise face. In that regard, C++ is no more unsafe than other languages. As a class designer, however, I find it a lot more expressive to be able to further impose constraints on how other programmers use the interfaces I provide to make it harder to use it incorrectly (oreilly.com/library/view/97-things...).

All of these are design choices and tradeoffs, and they are intrinsically subjective in nature. As a result, I am not going to defend C++ any further on this thread. It's a widely popular language with or without your liking towards it.

Thanks for the discussion though, I did learn some new things and it's always engaging to hear other perspectives!

Thread Thread
 
codenameone profile image
Shai Almog

C++ was faster when Google search was developed. It's not as simple today. Performance is a more nuanced debate at this level and benchmarks can mislead both ways. C++ is not faster and neither is Java. There are benchmarks that can go either way.

A JIT inlines virtual methods at runtime, that's something a C++ compiler can't physically do. This is one of those super optimizations that can make a frequently used code path scream since it serves as a basis for further, even deeper optimizations. A well tuned GC can place the peek where you have idle time. C++ will pay a higher price usually on the same thread where things physically happen. To be fair a GC typically needs more RAM to play around with, especially with a JIT. It can tradeoff of more RAM vs. higher performance vs. fewer pauses but would still take more RAM overall.

Using shared pointers for everything is 2x slower at least, which is exactly my point. The C++ new operator is already slower than Javas new. So you pay a performance penalty for careful coding and lose the advantage C++ is supposed to give you.

 
cipharius profile image
Valts Liepiņš

I wanted to slip in a real world example of a very performant GC. GHC, the most widely used Haskell compiler, uses GC in the compiled runtimes and it's designed around the fact that Haskell works with immutable data.

In result Haskell applications easily match other application performance written in lower level language, at times even exceeding their performance, thanks to optimizations that GHC can apply due to highly expressive Haskell typesystem.

Collapse
 
kobi_ca profile image
Kobi

C is nice and all but we should stop using C and start looking into a safer lang. The unsafe properties of C is not gonna cut it in the next years where safe code is going to be important. Code is going to run even more on cars and other mission critical platforms. Maybe Rust. dont know.

Collapse
 
codenameone profile image
Shai Almog

I very much agree. When I built ParparVM I picked C as the underlying language. Rust wasn't a realistic option back then. Had I done that today I would totally have picked it.

Collapse
 
eljayadobe profile image
Eljay-Adobe

I was going to rebut your counterpoints since I'm a C++ fanboy, but I think they're all true. (I've been programming in C++ for 33 years now. My current project is an extremely large, extremely old C++ project)

Some of the counterpoints are a bit a "matter of degree", most of others are "yep, no doubt about it".

C++ tooling will never be as good as Java or C# tooling. Lack of reflection, lack of introspection, lack of AST manipulation, and the preprocessor are all huge barriers to having powerful reliable and robust tooling. The refactoring tools for Java and C# are killer features in IDEs like Visual Studio with Resharper or CodeRush, or Eclipse, or the wonderful IDEs from the folks at JetBrains. That's the counterpoint that stings the most.

Collapse
 
afaliagas profile image
Apostol Faliagas

Mr Almond, you said that C++ is unsafe but you didn't give any clues. Then you said Rust is better (did you mean safer?) but you didn't mention in which sense. And you didn't give any clues again. Then you said C++ isn't faster, but you didn't mention any benchmarks. I stopped reading further because from what I read I conclude you are biased and opinionated against C++.

Collapse
 
codenameone profile image
Shai Almog

These are common terms and easily checked. I suggest reading this: en.wikipedia.org/wiki/Rust_(progra...

You can also do a slight bit of self research to learn about these things on your own e.g. this article is a bit out of date (and has problematic choices) but shows off some of the complexities of decreeing C++ as faster: en.wikipedia.org/wiki/Java_perform...

I've been a C++ developer since the early 90s. Hell yes, I'm biased against C++. I've also built JVMs (mostly in C but also in C++) so I'm pretty familiar with the performance nuances and benchmarks. Feel free to disregard my experience and go back to your bubble.

Collapse
 
efpage profile image
Eckehard

I love Delphi, but that´s not the point. The times that people used only Windows or Linux are over. With C++ you are bound to one or the other platform.

Though I did not like Javascript too much, I have switched over from Delphi/C++ to Javascript for some reasons:

  • It runs (in a browser) on all platforms
  • with respect to speed and functionality it has catched up a lot
  • HTML5 delivers phantastic graphics performance in an easy to use package
  • Though the class implementation in Javascript has some quirks (and too much this...), it is usable and no general reason not to use Javascript.

I personally think that some concepts of HTML and CSS are really bad and prevent a good programming style (See my post: What´s wrong with webdesign), but this is nothing you cannot overcome. You can use for example DML for web programming without html, and there are possibly other solutions to deal with that topic.

I perfectly understand people who love "their favorite programming language". But finally, we should not forget, that a programming language is a tool only. There are different tasks, so we need different tools. Visual Basic is the pure horror for me, but it is ok for some scripting in Excel.

So, many thanks for the writeup, it´s good to know about the concepts and powers.

Collapse
 
vnjogani profile image
Vinit Jogani

Don't get me wrong, JavaScript works pretty well for web dev use cases. But I disagree regarding your point about being bound to one or the other OS with C++. With WebAssembly, C++ can run on the browsers. C++ can also run on OS X, Androids and even embedded devices.

That aside, like I said, I would likely not use C++ for building web servers. It serves a specific purpose in your toolkit and it's good to have. If the only thing stopping you from choosing it is because you're scared it's esoteric, the purpose of this article was to convince you it's not.

Collapse
 
efpage profile image
Eckehard

Well, we had that precise question: We had some well designed applications, some with more than 60.000 lines of code. Our question was: can we bring this apps to the web somehow? I suppose, WebAssembly will not help us here, as they provide a completely new ecosystem. Finally it would mean to rewrite most parts of the code.

We will see how WebAssembly develops. Currently is seems to be a young project that has not too much impact. Maybe if they get direct access to the DOM this could change. I would really appreciate if there was a new binary layer able to run languages other than JAVA. But currently we cannot ignore that this is not the common state.

Thread Thread
 
vnjogani profile image
Vinit Jogani

Yep that's a fair point. The sad truth is that the dominance of JS on the web is hard to replace given how much tooling we now have built around it.

Thread Thread
 
efpage profile image
Eckehard

Initially I did not like JS at all. There are still somethings I hate the designers for (most of all the overwhelming use of "this" in class definitions). But I was amazed that we had less trouble than expexted in our projects. You can have real trouble with the sloppy type conversions. But we had this kind of tricky errors too in languages with static typing.

You will need some more checks and a bit different style with JS, but the loose typing can also be very elegant. Overall it is not as bad as expected. Definively better than Visual Basic. It is more like an ugly, but very handy friend.

Initially I thought, TS is a must-have. After I got a bit used to JS I'm not that sure.

Collapse
 
hebigami profile image
Hebigami-nya

Back when I learned coding I learned C/C++ as my first languages and I loved it! Over a few years I occasionally had to use other languages like Python/JS for larger projects but I was kind of disgusted by the fact that even most Senior Devs I met - especially if they only used JS - didn't know how the languages worked internally.

C++ ist awesome to learn because it requires you to have a rather high level of technical knowledge/understanding. I used to believe every dev had that level but that's simply not true.

Anyway, a few years later I found Rust.

Nowadays I strongly believe that anyone who prefers C++ over Rust simply doesn't know Rust.

Collapse
 
vnjogani profile image
Vinit Jogani

I may be wrong, but libraries and frameworks like OpenCV, Torch/Tensorflow, Unreal Engine only really have unofficial Rust support. The purpose of this article wasn't to discuss X is better than Y, but rather to discuss why C++ is in itself a pretty cool language and not as bad as people deem it to be. And while not everyone might understand the nuances of C++, it is a great language to really learn how things work and gain that technical knowledge and understanding.

Collapse
 
metacoding profile image
Meta Coding

I used C++ around 30 years ago for 3-4 years while I was working on my startup project. At that time I was in love with C++. For me the most dangerous and annoying problem of C++ is having features like multiple inheritance that let developers create a very complicated non-managable code. Even though these features are superpowers of C++ but still are not necessary. Also in my opinion C++ syntax is ugly, old and complicated. In my opinion, a programming language should have the following charactristics:
1- Push developers to use best practices more and more
2- Have a tendency to simple readable code (anyway these languages are created because our brain cannot simply read the assembly language or binary codes)
3- Generate fast, reliable, predictable and safe code.

To my understanding, programming languages are now having a much more sensitive role than what they had two or three decades before. They are controling our homes, cars, sensitive medical devices and even our homeland security. Neglecting of the above features is far beyond the purpose of having a good software creation tool. That is why I think we should stop using C++ in our embedded vital devices, and instead use Rust or other alternatives. C++ still can serve in many areas such as game industry.

Collapse
 
vnjogani profile image
Vinit Jogani

I feel like what points (1) and (2) come down to is how opinionated a language is. Python and Go, for example, really care for "one right way to do something" which automatically produces code that follows best practices and are readable. C++ isn't really designed to be opinionated, and that has its advantages and disadvantages, but large codebases typically define their own guides and enforce them automatically to achieve that. It may not be the best solution for all teams and developers though, I agree.

Collapse
 
gat786 profile image
Ganesh Tiwari

Great article!
Makes me wanna try c++ again

Collapse
 
yoshida profile image
Masao Yoshida

I also think C++ is the most wonderful programming language in the world.

Collapse
 
edwilkins1447 profile image
ed-wilkins-1447

I love C++. I have been doing C++ since the 90's. Thing is, there wasn't much of an option back then for Windows developers. There are things about C++ which is a big pain. char*, wchar_t*, BSTR, CString, std::string, _T, L, I mean sometimes it really feels like a joke.

I believe you HAVE to be really good at understanding whats happening under the hood to avoid making silly mistakes in C++ and I do like working close to the system so I have always enjoyed this. However, when there is a deadline, this type of potential hazard isn't something which is a lot of fun.

Today, software has to be developed super fast and ship yesterday. C++ isn't really the language for this. Most application have now become web based. A lot of stuff involves dealing with dynamic data at runtime. Without strong reflection support like .NET or Java, life becomes pretty complicated.

I have left C++ to the system folks and I am not one of them. I work mainly on business applications, Apache Flink, Java, .NET, SQL and others. The amount of productivity required by my teams will not be supported by the use of C++.
I still follow the C++ community. I recently got a book on C++ 20 and I intend to read it cover to cover. Yes, I do love C++ but I will not introduce it in my projects.

Coming to Java, having coded .NET since Beta-1, Java has always been a bit of a joke. Its 2021 and the language features of Java look really primitive. If one says C++ is redundant, Java should be more so. Its a terrible language. int vs Integer. No real function pointers. but propping up modern features using age old constructs like interfaces? Its hilarious. When I see the advertisement "Java runs on 4 billion devices" I often wonder if the C-19 virus thinks the same, "Infected 4 billions people". People need to stop using this terrible language. The JVM is great, but not Java, no there are much better alternatives.

Java Script. This is something which should never have been invented. It was kind of OK when all it did was check if textboxes in a web form were left empty, but nothing more than that. By the time I finish writing this comment, I am pretty at least 2 new JavaScript frameworks would have been written. Using frameworks like Angular, Vue, React is a complete waste of time and energy. None of these frameworks last more than 2-5 year. Strangely code written using C++ MFC, Java, even Visual Basic 6 is still running fine in majors banks and other companies.

C# in my opinion is the best language there is. The tooling is pretty good, Visual Studio is a great IDE. I hope .NET core really breaks some ground specially with the good folks at Apache foundation. I hope there is a time when .NET is the language used to create indexing services, distributed computing frameworks, frameworks dealing with big data and more.

Cheers.

Collapse
 
jdgamble555 profile image
Jonathan Gamble

Should we all just use go instead? Just wish it was oop!

Collapse
 
suabahasa profile image
Joshua Siagian

I learn c++ on university

Collapse
 
aheisleycook profile image
privatecloudev

I like c+