BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Should Developers Start Learning C++?

Should Developers Start Learning C++?

Leia em Português

This item in japanese

Bookmarks

With the introduction of C++ 11 and C++ CX there has been a lot of renewed interest in the language. And a lot of developers, especially Windows developers, are wondering if they should set aside C# and Java in favor of it. John Sonmez argues no.

In his article titled Why C++ Is Not ‘Back’, John Sonmez argues that there are only three reasons to use C++:

  • You absolutely need to ink out every bit of performance possible out of your software and you would like to do that with a language that will support OO abstractions.
  • You are writing code which will directly interface with raw hardware.  (Example: you are writing a low level driver.)
  • Memory control and timing is of absolute importance, so you must have completely deterministic behavior in your system and the ability to manually manage memory.  (Think real time embedded operating system controlling a moving piece of machinery.)

Herb Sutter, who has heavily praised this article for offering a “a thoughtful hype-free opinion” adds to the list:

  • Servicing, which is harder when you depend on a runtime.
  • Testing, since you lose the ability to test your entire application (compare doing all-static or mostly-static linking with having your application often be compiled/jitted for the first time on an end user’s machine).

One of the reasons that John Sonmez offers against learning C++ is the sheer complexity of the language. Even though C++ 11 has made development easier, it doesn’t excuse the programmer from learning all of the old ways of writing C++ code. “You will encounter C++ code from 20 years ago and it will look like a different language entirely.” To reinforce his point he posted the 36 questions he asked candidates for a C++ developer position. Here are a couple of examples,

1. How many ways are there to initialize a primitive data type in C++ and what are they?
12. What is a copy constructor and when is it used, especially in comparison to the equal operator.
16. When is it and when is it not a good idea to return a value by reference in C++?
33. Why should you never throw an exception in a destructor?

Another argument against C++ is that “programming languages really need to get simpler and increase the level of abstraction not reduce it.” He continues,

There will always be a need for low level code, but a majority of the code that we write today is at a much higher level.

I first jumped off the C++ ship many years ago when I finally could no longer make the argument that I could develop apps faster in C++ than C#.

I held out for a long time trying to believe that all the investment I had made in C++ was not lost, but it turned out that C# simplified things to such a great degree that the extra power C++ gave me was not worth the extra responsibility.

John Sonmez concludes by saying that learning C++ is still useful for understanding how computers work in general, “but I don’t think it is going to make a comeback any time soon, and that is a good thing.”

To this Alo adds,

I started out on C++ and spent the first four years of my career exclusively on C++. That experience has been extremely valuable for me down the line because – as you pointed out – once you learn C++ to an adequate level, you can pick up any other language really fast and you’ve developed a deeper understanding of how software works on a lower level – a knowledge that’s much harder to acquire when starting out at higher-level languages; I’ve always frowned upon the idea of starting programmers out on Java for that very reason.

Richard Dunks counters with,

I think C++ is unhelpful for use in first semester introduction to programming classes and in teaching data structures, you have to spend so much time on the implementation, the students often lose sight of the structure they’re trying to replicate. I’m glad I’ve gained the proficiency in C++, but I don’t think it’s worth the cost and definitely not a one-size fits all instructional language.

Stephen Cleary offers a comment on reusability,

I’m a former C++ master who became a C# developer a few years ago due to marketplace pressures. I am CERTAINLY more productive in C#, but it’s simply not possible to reach the same level of code reuse as you can with C++ templates.

The classic example is the triumvirate of containers, iterators, and algorithms. In C++, you can create one algorithm that will work with any container and adjust itself at compile time to take advantage of random access if necessary. Try doing that in C#. And I’m not talking about the “new C++” here, either; C++ permitted greater code reuse in 1998 than C# does today.

And in regards to performance, Herb Sutter offers this bit of advice,

In any language, if you are serious about performance you will be using arrays a lot (not “always,” just “a lot”). Some languages make that easier and give you much better control over layout in general and arrays in particular, while other languages/environments make it harder (possible! but harder) and you have to “opt out” or “work against” the language’s/runtime’s strong preference for pointer-chasing node-based data structures.

In additional to numerous high quality comments on Herb Sutter and John Sonmez’s respective blogs, there is much to be learned from Programming and Coding subgroups of Reddit.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • It's just a question of perspective

    by Cameron Purdy,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I started out on C++ and spent the first four years of my career exclusively on C++. That experience has been extremely valuable for me down the line because – as you pointed out – once you learn C++ to an adequate level, you can pick up any other language really fast and you’ve developed a deeper understanding of how software works on a lower level – a knowledge that’s much harder to acquire when starting out at higher-level languages; I’ve always frowned upon the idea of starting programmers out on Java for that very reason.


    Yes, and similarly, real programmers have always frowned upon the idea of starting programmers out in a high-level language like C++. If you can't read the self-modifying code on a 12-bit IBM punch-card (as Guy Steele was demonstrating yesterday at work), then how can you be expected to really understand these higher level concepts?

    And yes, I'm being at least a tiny bit facetious ;-) but C++ and the libraries one uses with it do hide an awful lot of the real complexity of the machine from a developer, such that most C++ developers have little more understanding of the true working of a machine than your typical Javascript developer.

    Peace,

    Cameron Purdy | Oracle

  • C++ will be required for some time yet.

    by Paulo Pinto,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Until we get mainstream languages which compile to native code by default and allow the same level of control as C++ does, C++ will be around.

    The world needs proper languages to write games, operating systems, device drivers, virtual machines, high performance applications and so on.

    Sure it would be nicer to use Ada, D, Rust, Java, C#, just to give a few examples, but it hasn't happened. And the world keeps on using C and C++.

    As long as it does not happen, developers need to learn C++, unless they want to join the ranks of web developers out there.

  • Re: C++ will be required for some time yet.

    by Ror Pop,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    There are many games written nowadays in C#, and there have been/are multiple attempts to write operating systems in C# (Singularity, MOSA, COSMOS to name a few).
    It'll take sometime for real OSes to be fully (or mostly) written in a managed-memory language, mainly because there's no real commercial incentive to rewrite existing systems.
    But technically, I believe there's no real barrier to this. JITted C# code achieves comparable performance, and sometimes better, than C++.

  • Re: It's just a question of perspective

    by marc prades,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    And what's your typical javascript developer ? you can keep on insulting people like you do , it doesnt make you smarter or even look smarter. And you have no shame in writing "Peace" after that direspectfull statement ? well you work at Oracle , in my opinion this only makes you look even worst and stupider. Only insecure people like you take pleasure in bashing others without a shred of argument. Shut up and go back fixing Java.

  • Re: C++ will be required for some time yet.

    by Paulo Pinto,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I am fully aware of Singularity and other experiments.

    To have lets say C# replace C++, we need to have available compilers able to target native code directly (like Bartok in Singularity), available in all operating systems and architectures as C++ is available today.

    The language also needs to be standardized so that multiple vendors are able to offer their own compilers. C# standard is currently a bit fuzzy still as it has not been updated since 2006 and there are also the Microsoft patents.

    I am only using C# as example, the same can be said for any possible replacement.

  • Re: It's just a question of perspective

    by Jerven Bolleman,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Wow lets keep some perspective and a civil tone will you. Cameron, is actually saying that most developers independent of language have little idea of what their machine does to actually execute the code they write. I would put myself in that "most developer" category, can barely read guess at what x86 assembly means when confronted with it and have never opened a CPU manual/tuning guide from anyone.

    Speed of execution depends more on quality of compiler and memory management than your language choice. But in reality it depends far more on how well your data structures are chosen and validated with experiments.

  • C instead of C++

    by Luis Espinal,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    As a person doing C/C++ for a living (and who has also done a lot of work in Java and other higher level languages), I would strongly suggest against teaching C++.

    It looked great at a time when we didn't have much other options for object-oriented systems programming. But it just terrible. The amount of minutia one has to learn to be proficient (proficient enough to not shoot oneself on the foot) is just terrible.

    Mind you that this is not minutia related to learning how things work under the metal (for that, I would suggest learning C AND assembler.) It's just the language's syntatic and semantic baggage.

    It does not mean that it is impossible to be productive with C++. Myriads of successful companies have produced successful systems with C++. But likewise, a lot of C++ shops are nothing but limping, bleeding train-wrecks (for reasons beyond mere usage of C++ obviously.)

    But as a pedagogical tool, it is terrible. People in college should learn C and assembly inside out (or any compiled language, procedural or otherwise, without garbage collector).

    Then such people are in far better conditions to learn other, higher, more productive-by-sloc programming languages.

    If I had my say, I would start projects in C over C++. The former might not have all the niceties of the later (C++ templates are nice), but the set of gotchas of the former is far smaller than the later.

  • Re: C instead of C++

    by Thomas Fitzpatrick,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    I have not touched C or C++ since university but for the sake of my own interest I am going back to it in my spare time. I believe the first C++ compiler was called Cfront and was basically a tool that read in C++ and spewed out C, rather than compiling all the way. If it is possible to do this with the gcc compiler (generate C from C++) then I would consider it a valuable exercise to create a few simple C++ classes and generate C from them, and then read the resulting C. This would help a student understand that if two languages are Turing complete then one can express the same program in each language, and write a code converter that will migrate the program from one language to the other. While learning C and Assembler can help you understand memory management, performance optimizations (i.e. loop unraveling) and where the software meets the hardware, understanding a bit about the internals of C++ can help one develop a broader understanding of how to augment low level constructs to the high-level world of object orientation. For pure academic purposes C++ has it's place, but not on a first year course. As for using it on the job, I would avoid it where I can.

  • Re: It's just a question of perspective

    by Cameron Purdy,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    And what's your typical javascript developer ? you can keep on insulting people like you do , it doesnt(sic) make you smarter or even look smarter. And you have no shame in writing "Peace" after that direspectfull (sic) statement ? well you work at Oracle , in my opinion this only makes you look even worst (sic) and stupider. Only insecure people like you take pleasure in bashing others without a shred of argument. Shut up and go back fixing Java.


    What a weird response. I personally program in C++, Java, and Javascript, but I'm not even sure which one of those you're defending. I'm not a fan of C++, but that's largely because I have experience working in C++. C is ok, if you need to write code that interacts with the underlying system, but -- IMHO -- C++ is far too obfuscating to be a safe language for development.

    My point was that the libraries that most people use in any / all of these languages are designed to heavily shield the programmers from knowing about -- and having to know about -- the underlying machine, and even the details of the compiler and native code generation are well-hidden by the tooling and language design, so the claim that programming in C++ automatically gives "a deeper understanding of how software works on a lower level" is pretty far-fetched in 2012.

    Peace,

    Cameron Purdy | Oracle

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT