Why I Still Love C++

This article is actually a precursor to my article on the D programming language. I lay the foundations for my interest in D by talking about what I love about C++.

I have been coding in C++ for a long time now. Even though from the start the language had certain warts I didn’t like, certain aspects of it were irresistible to me and made it top dog.

1. An expressive language – Once learned, few languages are intrinsically as expressive as C++. It has a million nuances (like the English language) that somehow make expressing exactly what you want to say seem easier than other languages. Having true const support, multiple inheritance (in those rare circumstances where it makes sense) and references that can never be null are just a few of the unique ways that it supports saying exactly what you mean. Even if saying it sometimes may seem to the novice a little verbose and complicated. How many times have you checked for null in a Java or C# program and gritted your teeth knowing that the value should in fact NEVER be null. In C++ once you define a reference you have made a contract with the compiler expressing your intent. The language spec will not let you check for null.
2. Multi-paradigm – A Even though 10 years ago I jumped on the OO paradigm in a big way, single paradigm language never could quite cut it. Even though generics exist in other languages, few are as strong as C++. Simpler, yes. Powerful, no. Support for objects and generics is a must in my book. Supporting a simple functional style is important as well. Some concepts in the real world are both functional and freestanding. Granted – it is rare that I use freestanding functions, but there are times when they can reduce coupling in a big way. In object insistent languages you are forced to wrap these guys in a class to “objectify” them. This is silly at best.

3. Supporting low-level constructs – someday this will go away. But doing a good amount of 3D coding I still like to be able to optimise things pretty tightly sometimes. The day has come and gone when assembly or flat C was a must in this category. But C++ is still very useful in this way.

4. Huge open-source support – let’s face it C  and C++ are still the reigning kings here.

5. Ability to interface with C and C++ effortlessly – given this “isn’t quite fair”. Of course C++ works well with C and C++. This is important because of #4. Many languages support clumsy bindings to C++ and C and this adds a layer of inconvenience to “get at” the wealth of open-source code already out there.
Many other aspects of C++ have been absorbed by other languages. I like the availability of generics. Being able to force strong typing when it is important is key to me – like const correctness. You know exactly what you want. Being able to also use generics and avoid strong typing is equally important. Your intent is clear. C# is a close runner-up for me in many respects – the two main things that makes it difficult to fully embrace include clumsy bindings with C++ and its lack of ubiquity on other platforms. The thought of writing a large body of code in C# and then having to rewrite it for say the Wii or a handheld makes me cringe. Mono may help, but as far as I’m aware the jury is STILL out on how widely accepted it will be.

For the certain types of coding C++ is still essential to me. It’s a love hate relationship. I look forward to the day when everything I enjoy about C++ is in a language that removes the things I truly dislike.

In this article’s follow-up I’ll talk about the D programming language. You might be pleasantly surprised by what it brings to the table. There I will mention the things that D does right and C++ clearly does not.

13 thoughts on “Why I Still Love C++

  1. Anonymous
    #

    good

    Reply
  2. Raf
    #

    Hi, I’d love to see this on eioba.com . Could you add it there ?

    Reply
  3. pongba
    #

    IMO, the biggest advantage of C++ is its static typing, which massively helps detecting problems as early as possible.
    The emphasis on performance will eventually go away, but the benefits static typing offers will last.

    Reply
  4. pongba
    #

    Oh, by the way, the first-class support for static typing in C++0x will make this go another level, of course:-)

    Reply
  5. Clinton Forbes
    #

    I make my living by building .NET applications. However I am forever finding little things that I miss from C++. Despite its quirks, C++ has so many benefits – including those you have listed.

    The main thing I like about C++ is its C pedigree. If you are not afraid of pointers you are free to use them. In Java or C# this power is taken away in order to support a garbage-collected ‘managed’ environment. I can certainly see the benefits of these newer environments, and also scripting environments. In projects with tight deadlines it definitely allows quicker development. But these newer languages will always be behind C++ in pure performance potential.

    The funny thing is, to me C++ always feel like ‘proper’ coding and C#/Java/VB.NET/Any-script-language feels like ‘play’ coding. C/C++ is much more like trapeze without a net.

    Reply
  6. Gordon J Milne
    #

    What a nice, cosy world it is when you use a really clever language like C++.

    The trouble is, it is an adults-only language and it is far too easy to screw up. With C you had pointers and that was confusing enough. With C++ you get a whole lot more, including operator overloading. Isn’t that a bundle of fun.

    A belief system that praises the complexity and arcanity of a language is one that is destined to go the way of the Aztecs. When it was all we had C++ was great; however, these days, with resources streaming out of every computational orifice, we can, and should, be able to do better.

    The value of a language is in how it helps you solve problems. If that language becomes so obscure that it starts to resemeble line noise then you are failing to communicate. Computers understand binary. Developers do not. The language exists for the benefit of the developers. When you write a program you are communicating with your peers.

    Reply
  7. Steve Riley Post author
    #

    Pongba – I am really looking for to C++0x to give C++ a boost up in lots of ways.

    Reply
  8. Steve Riley Post author
    #

    Clinton,

    I agree – these days C++ isn’t for everyone, but for those who know it well – it can still be hard to beat!

    Reply
  9. eulerbernays
    #

    C++/C(on occasion) will still rule the desktop, OS, gaming systems and virtually all virtual machines for OTHER languages/tech like .NET, Java, PHP etc for some time to come. Its a logical layer below the 5th gen of software memory managed frameworks. Its both more powerful because of this, and much much more dangerous. All the more reason to like it.

    Reply
  10. Kai
    #

    People claiming that “we have all the resources we need so who cares about good coding” are advocating a dangerous and irresponsible position.

    Reply
  11. Steve Riley Post author
    #

    Eulerbernays,

    I think that sums it up well. These days C++/C is a great logical layer beneath 5th gen software memory managed frameworks.

    And that’s part of the reason I can’t help but like it:)

    Reply
  12. FTorres
    #

    There days, if you are an entrepreneur or a vp, director and run a team to build a product from scratch. C++ will do a lot of things for you, among others lose money and time.
    C# or Java and eventually Python or Ruby are the way to go for 90% of the applications.
    You C++ guys are spoiled kids. Paid the most. Spending their time writing complex stuff. Nobody can understand.
    In the end if VCs have money to waste that is their problem.

    Reply
  13. Loup Vaillant
    #

    True, some characteristics of C++ are hard to beat. Being Multiparadigm, low level possibilities, and wide capability are great. However, this combination comes at a cost: C++ allow to express pretty much anything by forcing you to.

    The STL and the boost libraries reveal this aspect best. Their interface show C++ greatness. They are easy to use, they naturally lead to concise code, and they allow many things, even garbage collection —sort of. Their implementation, however, reveal C++ suckiness: STL and boost are ridiculously hard to implement. Sure, you don’t care, until you have to implement this sort of thing yourself (due to unavailable functionality or whatever).

    I am currently writing a plugin for a proprietary application, where using boost is not an option. The plugin parses a file which can contain optional data. So, I reimplemented boost::optional. This should have been easy: in Haskell, for instance, this concept is completely implemented with this single line (found in the standard library):

    data Optional a = Nothing | Something a

    This is a tagged union. It can contain an element of type “a” (Something a), or nothing at all (Nothing). To access the data, you must check first if it is there at all (well, you don’t have to, but the compiler will warn you if you don’t).

    Now, my own incomplete C++ implementation is over 50 lines. I suspect the (complete) boost implementation is about 500 lines. That’s what I mean by “ridiculously hard to implement”. I could of course go on and on for pretty much anything in the STL or boost.

    This is a tradeoff. C++ may be worth it for computation intensive programs, piloting hardware, embeded programs… Not for a minesweeper, a web site, a mail client (however complex), or many other things.

    Just a final remark about static typing. C++ has one of the most permissive type system I know of. Don’t even try to argue with that. The const and throw in prototypes can still fool us, but it is easy to forget them or get them wrong. We can find much stronger type systems. Go check Ocaml and Haskell for example: no “null pointer exception”, safe genericity for free (you don’t even have to tell the compiler). Even Plain Old Assignment (mocked as “destructive update”) is disabled by default.

    Reply

Leave a Reply