October 05, 2022

C vs C++

 

C is a modular compiled language while C++ is an object oriented compiled language. The question to answer is which one is better?
Object orientation has become famous with the advent of Smalltalk which has later evolved in C++ and Java. Programming something without OOP features sounds a bit outdated. On the other hand, C programmers are convinced that there is no need to utilize object orientation. And perhaps this would explain why the debate between C vs. C++ remains unsolved.
Creating short hello world programs is possible in any language. The more serous problem is to create programs which have 1000 lines of code and more. The promise of OOP languages is to support this attempt. On the other hand it remains unclear how to write such programs with C. The interesting situation is, that very large projects like the Linux operating system are mostly written in ansi c so there is no need for object orientation.
On the other hand, this understanding contradicts most programming books published in the last 20 years who are explaining to the audience that OOP is here to stay.
Let us try to listen to the C community and how they are solving the complexity problem. The idea of a C program is to use modular programming. Modular programming is the same technique used in Pascal programs. The idea is that a file is a unit aka translation unit. Roughly spoken, a unit is some sort of class. The interesting but seldom mentioned fact is, that a unit can have variables which are accessible from many functions of the same file. So it seems, that modular programming and OOP programming have some similarities.
Perhaps it makes sense to compare modular programming with OOP programming in detail. The idea behind a class is, that the class contains of variables and methods. The methods are operating on the variables, and this is the strategy to reduce complexity. If a bug was found in the program only a single class has to be modified which is located in the best case in a single file and has not more than 100 lines of code. But, exactly this feature is provided by modular programming namely the C language and Pascal as well. There are files which contains of variables and functions and a single file can be tested and improved independently from the rest of the code.
One possible source of confusion is located in the documentation and tutorials around the C language. Even many books were written in the last 30 years it is a rare case that a book will explain how to create larger programs. Most books are assuming that the user has never programmed in any language and it is explained in detail what a function is, what an if statement is doing and how to use for loops. But the average program will know for sure about these things because the concepts are available in all the language. The for language in a c language is mostly the same like for languages in C++, Java, or C#. The real bottleneck in learning C is to master to use header files and modules. The books about the C++ language and also books about C# and Python are explaining very well how to use classes. The explanation makes sense and is easy to read and perhaps this is the cause why most programmers think that dedicated OOP languages are much easier to use than Ansi-C.
Using the header files and creating modules in C is bit more complicated than using a class but it is not outside of reach to understand the situation. The only thing is, that writing larger software in C is less common or at least it is less frequently explained in the literature. Perhaps the bottleneck is not located in the c compiler itself but in the tutorials around the language?
Most famous books for the C language like “Mike Banahan: The C Book second edition” or “K&R: The C Programming Language” are failing completely to explain header files.. The subject is either ignored or explained in a short chapter in the appendix. What we can say for sure is, that the average programmer who has read these books won't be able to program larger software in C. In contrast, after reading a random book about Java or Python it is very likely that the programmer is able to split the code over multiple classes stored in different files.
The similarity between modular programming languages and OOP languages is, that the program is split over files. A typical smaller program contains of 10 files which have each 100 lines of code. The ability to split the code over files is a fundamental building block in creating more complex applications. There is no workaround for this problem. The assumption is, that dedicated OOP languages are able to explain the modularization easier than low level languages like Pascal and Ansi C.
Somebody may argue that OOP programming contains of more elements than only splitting the code into files. A typical OOP feature is the ability to create many objects from the same class. Most games are working with lots of sprites. In an OOP language it is very easy to create an array of objects while in ansi C the compiler doesn't even know what an object is.
But this problem can be solved with a normal struct easily. The c language allows to create an array of struct. And then the single variable struct can be send to a function and manipulated or shown on the screen. There is no need to use OOP programming for displaying 100 and more sprites on the screen. That means Ansi-C is more than sufficient to create all sort of simulation programs similar to what C++ has to offer.
From a technical perspective the C language can be used as a replacement for object oriented languages. The bottleneck are the existing tutorials which are not able to explain how to split source code into files. This has frustrated programmers who are preferring dedicated OOP languages.

No comments:

Post a Comment