Rediscovering C++…

I’ve rediscovered C++ for the first time in 17 years. It’s a different world now! Read on for what I’ve discovered.

Like many I first learned C++ at the start of University. At the time it was a bewildering language with many quirks. I avoided it since because of the pain I suffered at its hands!

Recently I’ve been working a lot on C++ for @MarkLogic. Outside of our ever busy Engineering team I’m one of the few C++ developers, so I got pulled in to a couple of customer accounts that meant I had to use C++.

Boy oh boy is it a different world now!

C++ back in the day

When I was learning just before I went to University in 1999 C++ was the wild west. Befuddling syntax, heavy c-like pointer use, and very hard to debug.

If you were lucky enough to get your app compiling you’d find it a nightmare to debug and test. There was little consistency between API and platforms. Oh and did I mention we had just got 56K dial up modems, so no Google or Stack Overflow to give you instant access to all the help you’d ever need to learn something?

The intervening years

Java became dominant starting in 1999. It’s what an entire generation of British Computer Scientists learnt – for everything from command line tool creation, to UI code, screen sharing apps, up to enterprise application development using EJBs and Servlets (which was very well documented at the time by my MarkLogic colleague, Jason Hunter!).

Java was syntactically much simpler than C++. It had its own memory management and pass by pointer rather than by value. It had cross platform API for pretty much everything you needed. Later on extensions written in pure Java – and thus cross platform – were added through the Java Community Process.

Slowly over time C++-isms crept back in. Typed collections echoed the C++ Standard Template Library (STL). Programmers needed more power. Some heathens even wished they had C++ power and performance as well as Java’s simplicity.

To be fair to Sun Microsystems, now Oracle, they did a pretty good job of accommodating this. Java has remained firmly in the Enterprise app stack, never made it for systems programming (outside of Mobile Phones anyway), applets within browsers died due to security issues, and Java in the UI is now pretty much synonymous with Eclipse based IDEs rather than pure Java Swing.

Meanwhile, the need for fast, machine code compiled, lightweight languages like C++ endured the test of time.

Modern C++

I must admit, although I know over 20 computer languages the thought of returning to C++ filled me with dread. Even worse the project I was working on needed a C wrapper for the API too! Ouch!

In the meantime though, C++ had been standardised more effectively, and kept evolving itself.

The standards bodies recognised what developers liked about Java. Various low level OS security issues meant there was greater demand for innovations in the language to enable security and prevent bugs due to mis-applying language features.

Smart Pointers are an absolute frickin’ god send. A std::unique_ptr<SomeType> object will delete the memory used by its wrapped pointer when the object it is a variable within goes out of scope and is destroyed. Very Java like, but without the lag of garbage collection.

The std::shared_ptr<SomeType> object extends this to where multiple objects may have a reference to a pointer. Very much like Java’s references.

No more hanging pointers – when used correctly.

Iterators that are consistent, and the ability to define your own using STL patterns, is a welcome addition. Even better, clever algorithms that are collection independent can be ran against members of collections classes. There is an ever increasing library of these.

Move semantics I’m still trying to get my head around – but it removes a lot of the object creation-copy-destruction overhead of older C++ programming idioms. It really makes code fly – but takes a bit of getting your head around! Especially Universal References – i.e. methods like the move constructor: MyClass::MyClass(const SomeType&& other);

There’s even std::function<ReturnType(CallParamType1,CallParamType2,…)> function pointer wrappers too.

The Boost library effort effectively prototypes new ideas for language features, with the most popular and useful ones becoming part of the next ISO C++ standard.

We’ve gone through a major revision with C++11 in 2011. This introduced many of the language features above, and standardised them. The STL too is now intrinsically part of the language, and pervasive. STL doesn’t feel like something hacked on to the side of the language, but a core part of it.

Best practices have also formed and become hard and fast rules. Effective Modern C++ by Scott Meyers (A Jason Hunter like language guru!) is a rewrite of a book that is my personal bible of C++ idioms and best practice.

Also, API Design for C++ by Martin Reddy has to be the best book on the subject of API design – for any language – I’ve ever read. I highly recommend this for any Computer Scientist, especially those about to start University… (You know who you are!)

These hard earnt lessons are very easy to understand and learn thanks to the practical examples. These examples are clear and concise – they don’t try and teach you C++, but rather correct your usage of it. Very easy therefore for any experienced programmer to use to pick up or relearn C++, like I did.

Thanks to this experience and these books I’ve learnt the best practice needed to create my own C++ API. This is the MLCPlusPlus client API project for MarkLogic. I’m not fully there yet, but already they have allowed me to apply modern C++11 features and create a very performant API.

I’ve applied lessons from basic struct and class layout, through inheritance best practice, iterator patterns, asynchronous task execution and synchronisation, easy API design, and even cross-language bindings using SWIG!

I now shall not fear any project for which C++ is required… which is for the best when you realise what I’ve been working on for the last month… Won’t be able to talk about that until 2017 though!


Learn C++. Now. DO IT!!!

It’s an absolute joy now. I’ve had more fun doing this than Java. Waaaaaaaay more fun than JavaScript.

Dare I say it, even more fun than XQuery. And I actually really like XQuery!

Don’t get me wrong there are days when I want to beat the compiler to death with a stick, but it’s normally due to me being too enthusiastic and not fully understanding the best practice to apply before trying to apply it. It was ever thus.

C++ now is a great language to use. Very predictable, consistent, and with hardly any hacks or workarounds required, unlike back in the ’90s.

I throughly recommend you all learn C++ again, and learn to love it like I have.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.