Why I'm Choosing C++

For many years I was happy as a mostly two-language programmer. Use Python for mostly everything, and drop down to C when you really need the performance.

But then I started to write a medium-sized library in C that required a larger toolbox than usual. I needed reference counting of pointers, array copying and moving, hashed data structures such as sets and dictionaries, function tables, iterators, and all sorts of other things.

You're probably thinking: "what's the big deal? I can do all those things, because I'm a bad-ass programmer!" Well I am not a bad-ass programmer. On a good day, I'm probably at best an average programmer. When I sit down to implement a hash table, first I spend a day going back to my college textbook to remember all the implementation details of hash functions, open addressing, chaining with linked lists, and all the rest of it.

Life is too short to implement your own reference-counting schemes, and the C programming language isn't very helpful. Don't get me wrong--I still love the elegant simplicity of C. As a language, C is close to perfect. It is portable assembly, no more, no less. The grammar is consistent, easy to understand, and generally unsurprising. But as a tool, C is just not very powerful.

But you're probably thinking to yourself, "that's what C++ is for!". My most recent memory of doing anything non-trivial in C++ was writing programs in my college C++ class. I remembered a language with lots of additional complexity just so you can use classes. To make things a bit worse, I am a frequent reader of the front page of Hacker News, and it seems like most people have a negative attitude about C++ nowadays. Every day there is another article extolling the virtues of some new language. Slow C++ compiles? Try Go! Unsafe memory access? Try Rust!

So I went out looking for a new language. These days there are lots to choose from. There are the "new and hot" languages like Go, Rust, and Swift. Then there are the "corporate" languages like C# or Java. Finally, there are the "other" languages like Eiffel, Haskell, and Racket. Programming languages have a lot in common with religions. You can spend your life searching for the "one true" way, and either find enlightenment, or discover you were worshipping false idols.

To make a long story short, I looked into some of these languages, but eventually came back to C++. The reason was that C++11 is a much better language than old-school, C-with-classes C++. It's seems as if the language committee has been observing all the innovation going on in the world at large, and has done a great job of reacting to the times.

The new C++11 has copy and move semantics, iterators, range-based for loops, lambdas, the auto keyword, smart pointers, and a whole slew of new data structures and algorithms in the STL. These features are a boon to productivity, and I haven't even mentioned all the new stuff coming out in C++14 and C++17.

Just as an example, I managed to write 2500 lines of C++ using the more modern style of RAII and shared pointers, and there were zero memory leaks on the first try! The equivalent functionality in C would have probably been 2x the code, and an additional week studying Valgrind errors.

One thing that slowed me down was how to interface a C++ library with Python. C has cffi, and ctypes, which are both dirt-simple to use. The landscape for C++ extensions is a bit hairier. If you don't want to just write your own CPython extension, you can try using Boost Python, pybind11, or SWIG. So this adds a bit of cost, but it still seems do-able.

I want to congratulate the C++ standards committee for all the work they've been doing lately. And they're not even slowing down! The roadmap for C++17 looks like we'll get even more fantastic features, especially in the all-important areas of concurrency and parallelism.

So if you're like me and you've been ignoring C++ due to distrations from all kinds of shinier things such as Go, Rust, and Swift, I encourage you to take another look. Not only will you probably find new things to love about C++, but you might even find yourself being more productive due to seamless integration with all the C libraries you still depend on.

I'm choosing C++.

Comments !