The C++ language is a widely discussed subject among computer programmers. Thousands of books and lots of stackoverflow entries are written about the language. At the same time, C++ is quite difficult to learn for newbies and they are preferring more clean designed languages like C# or Java. The reason why C++ is perceived as complicated has to do with a certain sort of tutorials how to program. In case of C/C++ there is a gap what programmers in the reality are doing and how they have documented their sourcecode. To understand the gap we must go back into the early 1990s.
In the early 1990s there was a big transition from older C compilers to modern C++ compilers. Or to be more specific, the books and published literature about C++ has increased while the reports about C programming has declined. Let us analyze the typical C programming book. What is written in the book are the C language standards and what is missing is a tutorial how to use the C language for creating object oriented code. The interesting is, that programmers in the reality are knowing very well how to create OOP with C. I have searched a bit in the sourcecode at github. Most of the C projects, especially games and C libraries are using a certain C programming style which was described briefly by https://softwareengineering.stackexchange.com/questions/308640/is-it-bad-to-write-object-oriented-c
The idea is to put a struct plus the function into the same file and call it from the outside, very similar of creating classes in C++. This programming style is seldom described in the manuals because it combines the classical C language with modern OOP design. But it's used in real github projects and perhaps most commercial C projects are working with the same style. That means, in the reality all the programmers are familiar with creating OOP software in C, but they haven't it documented in books.
What is described in the book is Object oriented programming with C++. This is described everywhere. The paradox situation is, that in reality nobody is using C++, especially not for serious projects. The amount of videogames written in academic C++ is low, the same is true for library of operating systems or serious applications created by experts. And exactly this mismatch explains why newbies struggle in learning C++ at all. They are reading the C++ tutorials in the hope to learn how to program modern C++. But they can't use this knowledge in real projects, because they are using the wrong programming language.
The reason why C++ was invented is to replace the C language. In reality C++ has failed in doing so. If the normal C language provides enough features to program semi-object oriented code with the help of structs, modules and pointers why should somebody switch to C++? Right, and because of this question around 65% of the code in the Debian Linux distribution was written in C and the prediction is, that the ratio wlll be constant for the next 20 years. Basically spoken, if a newbie wants to learn a modern objected oriented language which is used in the reality he should learn C and search for tutorials which are explaining how to combine C with object oriented programming style.
The gap between C programs in reality and manuals about how to create C programs is obvious. The average C programmer knows very well what Object oriented programing is. The C sourcecode is formatted like a C++ program which includes classes. So the assumption is, that the programmer has a deep knowledge of object oriented design. At the same time this knowledge isn't made explicit in the literature. The amount of tutorials who are explaining who to program OOP in C is very small. It's some kind of implicit knowledge how to combine the C language with object oriented features.
It's easy to predict what the future will bring. Instead of inventing better C++ programming language, in the future better tutorials were written how to use the well working C language for creating object oriented code. This will allow newbies to reproduce the existing C codebase and copy the programming style of existing C programmers.
Game engines
According to an often repeated story, the C++ language has become the standard language for game development. All the major game engines are written in C++. At least, this is told to the newbies. But let us take a deeper look into the problem. At first, the sourcecode of all the proprietary game engines is not available. In thoery it can be written in plain C and what is told to the public is the opposite. Some of the game engines have published the sourcecode, and indeed it's written in C++ syntax. A normal gcc compiler can't convert the code into a binary file. But is the code really written in C++?
All the so called C++ sourcecode contains of cpp files plus header files. It's interesting that all the code contains of pointers to structs. The reason is, that pointers combined with header files are the only option to realize object oriented paradigm in C++. Let us make a small thought experiment. What will happen if an expert programmer takes the existing C++ code and rewrites it in plain C code? That means, he replaces the class keyword with the struct keyword and adjusts some minor formatting issues. The resulting plain C sourcecode will look nearly the same like the original C++ code.
This thought experiment shows, that the so called C++ sourcecode is in reality normal C code. It contains of header files, is using pointers in every function and can be realized with a normal C compiler easily. So why it is called a C++ project? The funny thing is, that the programmers doesn't call it a C++ project. They know that the difference between C and C++ is small. They are calling it a C/C++ program and they are using the C++ syntax without a purpose.
No comments:
Post a Comment