September 14, 2019

Is the C++ language dead?


On the first look, the idea behind C++ make sense. C++ combines object oriented programming with a compiled language. Buth features are perceived as powerful technique in modern software engineering and the C++ is the most important language with this aim. But, if C++ is so amazihg, why so many alternatives were developed in contrast to C++?
In the Open Source / Linux domain there is a widespread concern, that object oriented programming doesn't provide a useful addition to structured programming but make things more complicated. The complete Linux kernel for example was written in plain C, and most libraries like the GTK+ too. In case of GTK+, a plugin was used to provide simple OOP features which is called gobject, but it's not a C++ library. The same mistrust against C++ is obvious in the Windows operating system. Microsoft has developed since the year the C# language and the dot net framework which stands in direct contrast to the C++ language. Programming with C# is very similar to programming in Java, which means, that the language is not compiled into machine code but interpreted as runtime. Last but not least, the famous Python language which supports also object oriented programming doesn't provide a compiler but it's an interpreted language.
To analyze the situation in detail it's important to give some facts. It's widespread accepted that the lowlevel C language is compiled. Another fact is, that most object oriented languages like Java, C#, Ruby and Python are interpreted ones or they are running in a virtual machine. What is a potential explanation for this gap?
Converting a structured C like program into machine language is not very complicated. A normal program contains of variables and program text. A C compiler converts high level C sourcecode into assembly statements and this is executed by the operating system. On the other hand, an object oriented programming style asks for a different kind of converter. The first object oriented languages like Simula and Smalltalk were realized as interpreted languages. The reason is, that objects are not stored in assembly syntax but in a datastructure. Let me give an example:
Suppose, in the pacman game the object “ghost” was created in the program. Ghost contains of variables for storing it's x/y position, and it contains of methods for moving the object to another position. The code for moving the object is stored only once in the physical memory, but all the instances have access to the code. The translation from sending on a high level layer a message to the object to low level instruction execution is done by the interpreter / virtual machine. A second reason why most object oriented language are implemented with an interpreter is because it allows interactive edit and test the sourcecode.
The question which remains open why C++ isn't working in this way, but combines a compiled language with object oriented features? I'm not the first one who asks the question. Microsoft struggles too by answering it and the Linux community as well. This was the reason, why they have developed both an alternative to C++. The disadvantage is, that these alternatives are slow (in case of C#) or they are complicated to program (in case of GTK+ and gobject). That means, the original idea of C++ is not perfect, but potential alternatives are also criticized as the wrong way.
To investigate the situation in detail we have to do again a step back and describe first which kind of technology is perceived as stable. The combination of a structured language like C plus a compiler is a best practice method in modern software engineering. There is no need to interpret a c program because this would reduce it's executation speed. All the important libraries in an operating system are written in pure C, and this is used in all operating systems. The open question is, if the same compiler technique can be utilized for object oriented languages. It seems, that the problem has to do with pro and cons of object oriented programming.
OOP is a relative new development. It is something which is not available in assembly language and it isn't there in classical languages like Fortran or C. It's hard to define what object oriented programming is. On the first look, it's a language feature, which is built in into the syntax, but at the same it's also a software engineering technique which was made famous with the UML notation. Especially in the Python environment, OOP is used as a prototyping technique to develop software from scratch.
From a critical perspective the question is, if OOP is needed in classical programming. Suppose the look and feel of an application is already known and the algorithm is fixed. Then the advantage of OOP is relative low. That means, in most cases, the more efficient way in creating sourcecode is to not create classes but to utilize lowlevel techniques like pointers and linked lists.
I think it's important to make clear what kind of best practice method is accepted widely and which not. What is used by all programmers is to compile a structured language like C, and use Object oriented design for prototyping new applications. This is done with interpreted prototyping languages like Python. The open question is, how both parts can be combined. One option is to use C++, the other idea is avoid any OOP feature in executable code, or to introduce a Java like bytecode concept.
From a technical point of view, it's interesting to ask how an existing object oriented design in the UML notation can be converted into a non-OOP language like C. What are the steps to convert the Pacman ghosts and the other objects in a game, into normal structured programming code which gets compiled by a c compiler?
The answer can be found in concrete game project which utilizes the C language but not C++, https://stackoverflow.com/questions/43127769/creating-a-game-board-using-struct In the example game, the current game state is stored in a struct. And then the struct is given as a parameter to a function. It's easy to imagine who a pacman clone would be realized in plain C. The first thing is, to create an array struct which holds the ghosts. And then the item in the array is given to a move routine which adjusts the position.
What is different to object oriented programming is, that in the sourcecode there a no explicit classes given, but the machine model is used as a representation. That means, the program gets access to adress in memory, and can call subroutines. The prediction is, that this kind of programming style results into a faster program executation, because it comes closer to the inner working of a computer. Object oriented languages like C# or C++ are providing an additional layer not available in assembly language which makes the code slower.
Conclusion
Object oriented programming is at foremost a software engineering technique which allows humans to develop a prototype. Implementing OOP features into a computer language or a compiler make the system slower. From a computer point of view, the programmer should avoid object oriented programming and type in the sourcecode in normal C code.

No comments:

Post a Comment