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?
Why I prefer C# over java:
– It has real generics as opposed to the useless bizarre “type erasure” java stuff.
– It has Delegates and Events which enable and simplify the creation of much cleaner ways of communication between classes and allow to treat code fragments and functions as “data” that can be passed around classes to be called/executed when need.
– It has async/await which makes asynchronous programming practically transparent from a code standpoint.
– It has Extension Methods which enable the creation of Fluent API-like constructs in a really clean way.
– It has LINQ which introduces several Functional Programming-like constructs and simplies and reduces the amount of code required to do almost ANY operations on collections of data (sorting, grouping, filtering, combining, etc). java is trying to copycat this but they failed because the language isn’t good enough and they refuse to change it.
– It has real properties as opposed to the useless bizarre “getXXX()” and “setXXX()” convention.
– It has type inference which saves a lot of useless typing the same thing twice in the same line of code.
– It has optional arguments which remove the need to create 200 overloads of the same method with different number of parameters
– It has Iterator Blocks which enable the creation of deferred-execution code and Coroutine-like constructs in a really clean way.
– It has Dynamic Typing if you ever need it.
– It has a much better naming convention as opposed to the all-lowercase, “com.this.is.horrible” java stuff.
– IT HAS THE .NET FRAMEWORK which is a comprehensive set of STANDARDIZED, mostly bug-free libraries as opposed to the useless duplication seen in java where you have (for example) 10ths of different libraries (made by unknown hippies and supported by no-one) which try to imitate a single C# or .Net feature such as LINQ or ASP.Net, and from which you never know which one to choose. These libraries also make use of all the language features mentioned above and are most of the time really pleasant to work with, as opposed to having a bunch of convoluted patterns to cater for the limitations of a poor language.
– It allows me to create Android and iOS apps without having to resort to a horrible dinosaur language such as java and without making my way thru the complexities of Objective-C.
Well, C# is better at some things and Java at different things but they are very similar and they are both dinosaurs from 20th century:) Nowadays there are many interesting emerging programming languages.
I don’t want to elaborate more on the subject because C# runtime is not really cross-platform and does not work on my system.
I always wonder why people believe that in C++ there is a need for ‘manual memory management’ and prefer a Garbage Collector. Actually there is a need for manual resource management in Java (though it’s semi-manual with try-with-resources, but still error prone), while in C++ with deterministic destructors it’s done fully automatically.