Thursday 28 January 2010

Why is C++ Getting Left on the Bench?

During my recent 6 month sabbatical I spent some time soul-searching. Not searching my soul mind you. Nope, I was trying to fathom out where the future of C++ lies, with respect to the corporate/financial world – the pond in which I swim. As I prepared to return to work again some 4 years after last looking for a new contract, there was a distinct lack of C++ roles. Well, duh!, there’s a global recession, which means there isn’t much of anything at the moment. But it felt like there were far less C++ roles than there should have been. To make matters worse I was interviewed for my current role on the premise that it was for C++ development, but before being made an offer it switched to being a C# role. I was still offered it, even though I have no previous C# experience, which is fortuitous for me as it gives me the opportunity to retrain in C#/.Net on-the-job.

But, why should a Greenfield project, and one which many would consider in the past to be perfect C++ fodder (distributed computing), change direction? I experienced a similar effect during my last role. There was, for a brief period, a concerted drive to try and implement as much as possible in C# instead of C++ because it was perceived that they could deliver value so much quicker. Of course the comparison was largely the usual apples vs oranges affair, as they were comparing the time it takes to make changes to a “10 year+ waterfall developed legacy C++ system with 0 unit tests” against a “Greenfield agile developed C# based web service designed with testability in mind and a full unit test suite to boot”.

A recent thread on an ACCU mailing list has furthered my concerns about the potential future markets for C++. It was actually a question about why so much development continues in straight C, when C++ is “clearly” a better choice. Although I’ve never worked in the embedded market, I gather from the various discussion threads that tooling plays a significant part in its lack of adoption, especially with respects to the more heavyweight features like RTTI, exception handling and template meta-programming. But there also appears to be a level of paranoia that surrounds C++ code, no doubt due to the same heavyweight features, that I can see plays a part in the embedded world – but not for desktop or server-side applications. From what I’ve heard Linus Torvalds hasn’t exactly helped the cause by openly criticising C++ development; and having seen some shocking code myself I can see where he’s coming from.

Still, just because a feature is available, doesn't mean you have to use it - you can still write procedural code in C++. The most complex templates I’ve ever written are for smart pointers, oh, and some truly dreadful generic collections back in the early part of my career. However I’ve used boost::function (a non-trivial component) on many occasions. You don’t have to be David Abrahams to be productive in C++. At an absolute minimum you could write in C, but take advantage of C++’s greatest strength, RAII, and at least take the pain out of the resource management. RAII is an incredibly simple concept to understand, cheap to implement, and clarifies code by removing the need for the ugly Single-Entry-Single-Exit style so often demanded in C based projects.

As a distinctly non-polyglot programmer I have absolutely no credentials for claiming that you can be as productive in C++ as you could be in, say, C# or Java. As a language, I believe you probably could be, but the distinct lack of standard libraries means that you need to buy or build the moment you step outside of “Hello World” and into even meagre requirements like XML processing or TCP/IP communications. C++0X goes some way to alleviating the common ailments like threading and lambdas but is it too little too late? The stock answer to the anaemic library problem these days is often “Boost”. Yes, it’s full of goodness, and pretty stable, but F/OSS still doesn’t sit well in many institutions.

C++ definitely has an image problem. A lot of mud has been slung over the years and unfortunately some of it has stuck pretty solid. An old colleague of mine who was a C# developer often used to quip “Don’t you have some memory to manage or leaks to plug?”. The answer of course, was no. I haven’t had to deal with memory (or any kind of resource leaks) in my code for years. Probably the better part of a decade. RAII (which is described in exquisite detail in Matthew Wilson’s Imperfect C++) means I don’t think about it any more than a C# or Java programmer. In fact probably less so! Granted, techniques like Expression Templates are seriously complex and require real brainpower – but only if you’re going to implement them; as a consumer of these techniques do you really need a PhD in Software Engineering?

The tune from the recruitment consultants about the demise of C++ in the Windows based Finance world where I dabble only confirms my suspicions. .Net is king and no longer just client-side - it’s also encroaching on the server-side where performance is not measured solely in computations-per-second. That pretty much leaves just the quant style number crunching domain, which is not really my bag, or maintenance work on aging legacy systems that are limping along until the rewrite goes live - no doubt written in C#. “Keeping The Lights On” is fun for a bit, until you realise that you have absolutely no chance of making any significant contribution to the mass of Technical Debt.

Maybe the problem lies with the decision makers? Are Today's Managers just Yesterday’s C++ programmers who had such bad experiences in the ‘90s that they feel morally obliged to protect their workforce from such abuse? Once bitten twice shy and all that? OK, pretty unlikely. How about excellent marketing from the other camps? Good PR probably helps to kick-start a revolution, but developers are a pretty savvy bunch and not afraid to point out when the Emperor is dressed in his Birthday Suit - I don’t think anyone could pin the phrase “Passing Fad” on Java or C# these days.

I know it’s a massively overused quote but those immortal lines from Uncle Ben in Spiderman are the closest thing I have to an answer “with great power comes great responsibility”. C++ welds great power, but potentially at an extremely high cost and I don’t think enough developers are willing to take on the responsibility of learning how to use it safely. They want to focus on delivering value to their customers and not getting bogged down trawling the internet for vendors or F/OSS libraries because their language doesn’t even provide a facility to search a folder for files. And rightly so. I love C++ to bits and I feel sad to have switched to another language after nearly 15 years of it. But the reality is that it just isn’t moving fast enough to fill even the basic needs of what a general purpose programming language has to provide today for a General Purpose Programmer like me.

5 comments:

  1. You must also take into account that C/C++ is generally less productive than a scripting language such as php or perl. For most cases something that would take a day to do in php might take weeks to do in C/C++.

    ReplyDelete
  2. Apologies if this wasn't clear, but I'm in no way comparing C++ to scripting languages - there is no comparison to be made as they target different problems.

    The markets I was commenting on are the Distributed Computing and Middle Tier Services, areas that require careful resource management and low latency processing.

    ReplyDelete
  3. Hi Chris
    I think C++ still has some legs left in it yet. I have a similar background to yourself and also pondered about my future a while back. Granted, C# and Java dominate the risk management and STP domains, but take a look at the skills required in high frequency trading and market-making spaces, firms are falling over themselves to hire experienced C++ developers like yourself. At the start of the year I decided to test the waters of the job market and I've managed to get 4 job offers within 1 month, all on low-latency trading systems and all offers were on the basis of my C++ experience. So I think C++ skills will still get you invited to some parties for some time ... but very select parties however.

    ReplyDelete
  4. @Anonymous: I'm glad that you've managed to come up trumps and I hope you keep getting those party invitations :-)

    A while ago a colleague described to me how the low-latency C++ based transaction processing system that he had worked on was being replaced by a Java based system with a latency that was slower by over two orders of magnitude. I'm not sure what the situation is now, but I find it hard to see why an organisation that was replacing its software messaging infrastructure with a hardware solution would even contemplate such a switch.

    ReplyDelete
  5. I Think Development platforms like QT have a great deal of potential for the C++ language. I would not call myself a programmer, but have written apps in Ms Access, vb and C++ using QT. QT provided a great API, the same code can be compiled for apps on windows, linux and Mac. I think c++ has been hammered because microsoft choose not to expand it's toolkit. Only time will tell, but I like it.

    ReplyDelete