C++ is a language I used for almost 2 years at work, for all my university projects except when there was an explicit requirement to use a different programming language and at home for very different tasks. The language alone is really powerful and well designed although the designers had to keep it compatible with C. Like every programming language, C++ has its own difficulties and problems:
- Pointers They are one of the strongest points of C++ but are also the most difficult concept for beginners. With some experience one can get used to them and find them obvious and natural but the biggest problem is that it is very easy to overuse them and do really tricky things using pointer arithmetic. If used wrongly, they can result in an unexpected behaviour and bugs
- Multiple inheritance The idea is simple but once you start thinking about this you can find many cases where it is not obvious what given statement or construction will do. It becomes even more complicated when inheriting from 2 or more base classes which in turn inherit from the same class. There are of course methods to resolve this ambiguity but they are not widely known and understood.
- Templates They are ultra powerful and you can do pretty impressive things with them but it comes at very high cost. If you make a mistake when using template class or method, the compiler will often generate one or more screens full of error messages which are hard to read and does not describe root cause of the problem.
- Handling of programming errors When accessing invalid memory location (including null pointer) or when throwing exception which is not caught anywhere, the program will abort without any good hint what was the root cause. If it happens in development environment, you can attach to the running process using debugger or analyse the backtrace. But if it happens in production, you are generally left with very little information.
- Slow compilation Whenever I compiled larger programs in C++, I was always surprised why it takes so much time although the number of includes was minimal. The worst thing was that similar code in Java would compile much faster. I know that its not a fault of the language but rather of compiler but I think that other compilers than GCC are not much better. Slow compilation of source code generally slows down your work with all its consequences.
- Manual memory management In C++ programmers have to manually track when the object or memory block is no longer used and have to explicitly free it. Java automates memory management which is a big step forward but still there are some problems with it.
Don’t get me wrong – I really like C++. I got used to the issues mentioned above and they are not big impediments for me. They just slow me down. I like pointers (the references in Java are very similar concept), I have no bad experience with multiple inheritance and I like templates. I fully understand why the handling of programming errors is not much better, why the compilation is slow and why memory management is done this way.
But there is still one and the biggest reason why I don’t want to use C++ at work: very poor libraries. Of course, you can say that we have famous STL and Boost in C++ but to me they are just a bunch of weird macros, even weirder templates and unnecessary inheritance. Just ask yourself whether you can easily understand how to use given Boost class by just looking at its header and how hard is to understand the compilation errors when using them wrong.
There are of course good and easy to use libraries but many of them are under GPL or other license which disqualifies them from using in commercial projects or it is necessary to pay for using them. I don’t blame these licenses because everyone is allowed to choose the best suiting license for its own software but even largest companies don’t want to pay royalties for using the licensed libraries although it could speed up the development and increase quality of software.
If you write open source software you usually have several good libraries to choose from but in commercial products you have almost no choice. To sum up we just waste our time on writing code which was written already many times before.
If you take a look at Java standard library, you will see how many features it provides. In Java SE there are classes for collections, compression, IO, networking, GUI, XML processing, security, accessing databases, sound processing just to name a few. But there is also Java EE, Spring and much more just waiting to be used and many of them on very liberal licenses. Of course, I am not saying that these libraries are perfect and Java is perfect because they are not.
But isn’t it just better to concentrate on developing your application instead of constantly reinventing the wheel?