June 21, 2018

Learning C++ fast


C++ itself is an awful language. Concepts like object-oriented programming, pointers, templates and low-level assembly are not the right choice for a future ready programming language. The best way to deal with C++ is to ignore it and take help at other programming language who fits better to the beginner. The interesting aspect is, that everything what the newbee learns outside of the C++ community helps him to write better C++ later. For example, if somebody starts programming with Python and is familiar with object-oriented paradigm. Then he is able to create class diagrams in a programming language and perhaps he sees the advantage of inheritance. If he switches from Python to C++ he can use this knowledge the same way. Because the OOP feature of C++ are very similar to Python.
Or if somebody is familiar with the Forth programming language and is an expert for writing factored code, which means to write short 3 lines long functions and using stackbased method calls, this knowledge is also useful for writing more efficient C++ code. If somebody is coming from the C# community and loves the absence of pointers, he is also welcome in the C++ language, because the new C++17 standard is trying to convince the programmer to not using pointers anymore.
What i want to explain is, that it is possible to learn something about programming outside of C++ and use this knowledge later to write better C++ code. Let us make an example. We are taking the best practice methods from C#, Forth and Python and mixing them together. Then we are writing in such a style C++ code. This project is very good to read, compiles to fast machine code and can be seen as a wonderful example of C++ how it should be. I think, C++ is some kind of melting point for all the other communities.
The best example is perhaps a transition from Python to C++. If somebody has written Python Code in the past, and uses this knowledge for writing C++, then his C++ programs will look like Python. That means, they are using object oriented features and have no pointers. Because this is the python writing style. The typical Python sourcecode is very elegant. That means, it has no complicated language features or is written for performance, but Python is some kind of pseudo-code with pedagogical reasons. The interesting aspect is, that a C++ compiler allows to program in the same style. A C++ program can be formatted that it will look like Python.
But why should a Python programmer use a C++ compiler if he can program against the Python2 Interpreter? That is a joke question, because from a computational point of view, the Python2 interpreter is not very efficient. That means, it can't generate machine code and it doesn't support threading very well. And to be honest: Python2/3 will never be as fast as a modern C++ compiler like GCC. Python was simply not designed as a compiler language. Most languages outside of C++ have similar bottlenecks. For example, C# is also a wonderful language which is in some aspects superior to C++. But C# has the problem, that it is not often used in the Linux environment and doesn't compile very well on Android devices.
But why should all programmer transit to C++, why not to another general purpose language like Java? The answer has to do with a potential alternative to a language. C++ is the only language which can't be replaced by something else. That means, it is not possible that in 30 years C++ is dead. Sure, it is possible to invent new programming languages, for example PHP, go or D. But none of them can replace C++.