Search This Blog

Saturday, May 23, 2020

Why is C still in use even though we have C++? Is there anything that C can do but C++ can't or maybe something that is easier to do in C rather than C++?


Maybe you should read Linus Torvald's diatribe on C++ and why it is not used in Git or the Linux kernel. Or look at something hot today, like Python. There is no C++ in the Python interpreter code, only C. In SciPy we do have some C++, but in order of precedence it is the least desired language; we prefer to keep things in Python or C if we can. This is not a coincidence.

  1. C is tiny and small. C++ is tremendously complex. Anyone can learn C, but for each skilled C++ programmer there are nine who think they know C++ but do not. If the product is a team effort, they can cause tremendous problems. The one that actually knows C++ will have to babysit everyone else and argue against nine morons why their code has bugs in it. If you work on a team, stick to C. This is the main reason C is still preferred over C++.
  2. The correctness of C can be proven. If you make software that lives depend upon, you must either use static analysis tools to prove the correctness of C or use Ada. C++ is not an option. The list of errors that can be present in C code is small and well understood. For C++ the list is enormously large, and many of the ways C++ code can fail are not even described and well understood.
  3. C has no exception handling (though there are goto and longjmp, which can be abused to the same effect). No exception handling means the flow of execution is the order of statements you see. If you allocate a resource your program will not suddenly jump into an exception handler before it reaches the deallocation. Say you unlock a door, enter a room and turn on the lights. With C, you turn the lights off and lock the door when you leave. With C++, you enter the room but are suddenly teleported somewhere else, and the lights are left on and the door is still open. In C++ you will need strict rules for design patterns like RAII to deal with this problem. Now, go back to point 1 and consider those unskilled C++ programmers. They will, e.g., not stick to RAII pedantically, and thus cause bugs that are very hard to detect.
  4. Back to exceptions, say that you have written a class library. Now some moron decides to inherit a class and overload some virtual method. After all, this is how classes are intended to be used. Unfortunately, the overloaded method throws an exception the class library did not expect to get. Now this exception enters into the library code, which might not handle it correctly. What happens next? Your guess is as good as mine.
  5. There is a way of writing C that is called "object-oriented C". While C is not an object oriented language as such, anything you can do with classes you can do with structs, static functions, pointers, and function pointers. You can see examples of this coding style in the Linux kernel, in stdio.h FILE object, in the CPython interpreter, in GTK and GNOME, among many other places. This is how most C is written in the industry, and if you use this coding style you do not really need C++. Unfortunately, it is not well described in textbooks on C, so the tools of the trade are passed down from engineer to engineer. Once you have learned it, you will understand that there is actually no need for C++.

No comments:

Post a Comment

CON NGƯỜI và TINH THẦN

CON NGƯỜI và TINH THẦN 1 –  " Nếu có một gã trọc phú hàng to súng lớn, có thể mua biệt thự alibaba cho em ở, xe lếch xù cho em đi, nạp ...