Is C++ obsolete?

The good news is, that the question was answered before in detail, https://www.quora.com/Is-C++-obsolete They are giving some examples in which C++ is used today for example Webbrowsers, operating systems, Facebook’s database backend, game engines, videoeditors and the URL comes to the conclustion:

“In this respect, we can say that C++ is actually the most relevant and important language and technology today.”

Nobody of the other participants in the debate can bring a counter argument in. The only community I know, which argue differently is Forth. They would say, that C++ is a subset of Forth because Forth has object oriented features too …

Advertisements

Java 10 is out, but it can’t replace C++

https://www.infoq.com/news/2018/02/first-look-jdk10-rc1 introduces the latest iteration of the Java programming language. It is a very well written ecosystem and the object-oriented feature are useful for writing large scale apps. But, Java 10 is not able to compete with C++. It is not possible to write lowlevel operating system libraries in Java, so it will be only the second language. Do we need two general purpose object-oriented languages? Do we need separate tags on stackoverflow for Java and C++? It is a rhetorical question.

I’m not a C++ fanboy. I think the language has many problems, for example the old C elements in the language are a big problem. But inventing a new language is only possible if this language can replace C/C++. Java is not in this position, so until something is invented which is better then C++ it remains the first choice for programming software. Instead of trying out Java 10, i can recommend the Forth language. Even Forth can’t be used for real programs, a C++ programmer can learn a lot from the programming style. Forth programs are very good example of factored code. That means the normal subroutine gets no parameter and uses no class variables. This ideal is not reachable in vanilla C++ but it shows what the goal is. In the ideal case the main function contains nothing than:

main() {
__input
__calculate
__output
}

Some C++ advantages

In the past, the C++ language is often called difficult to learn. But if we observe the development in the last 10 years from 2007 until now, lots has changed. Today, C++ is the best programming language available. Here are some nice features.

1. data structures: With the keyword std::vector it is possible to create a dynamic array object. That is comparable to a database which is created in-memory. We can easily add new objects, search and delete them. And every item is stored with a class template, that means, the data can be extended with methods.

2. performance: Yes, it is a fact, that C++ is the fastest language available. With the compiler switch -O2 together with -s (stripdown) the most efficient binary file can be generated.

3. Threading. It is possible to create many threads at the same time for running the gui, the game engine and the physics all in parallel.

4. Libraries: C++ is well supported by all kind of external libraries, from gaming, graphics, scientific up to XML, network and database access.

5. openstandard. C++ is not controlled by a single company but is widely used by the industry under Windows, Linux, Mac and others. Opensource compilers are available.

6. huge community. If the beginner has a question he will find answers in many online-forums.

7. compatible to C. The c programming language is not so powerful like C++ but it is widely used in system programming. All c programs can be used together with C++.

8. No alternative: Programming languages like C# have some of the advantages of C++, but not all. Today, no other language can replace C++. It is the most powerful and easy to learn language ever invented. If C++ is to complicated the next more simpler tool would be a spreadsheet like LibreOffice calc. Python is not easier to learn, it makes the things more complicated.

Teaching C++ at the university

As classical educational programming language are widely known Haskel, LISP and nowadays Java and Python. The are introduced in computer courses to the student, and everybody know, that this learning environment is artificial. That means, Haskel is not used in the industry for real projects, it is only educated on the campus.

With a bit search on Google Scholar there are some interesting alternatives to this idea. C++ is partly used as a teaching language. The surprising precondition is, that C++ is apart from the above mentioned languages a practical programming language. That means, it is discussed on Stackoverflow and many real life projects are done with it. According to the papers, it is possible to use C++ for more than only to write software. It can be used as an educational programming language. :That means, the aim is not to program software with it, instead the aim is to explain beginners how they can learn to write software.

The challenge is how to fit the complex C++ language into a teaching experience. The problem is, that most programming courses are not longer than 5 months, and in this time the student should learn the language. The usual approach is to teach not only C++ but together with the object oriented paradigm which separates the world in classes. I’m in doubt if this will work, because OOP is one of the most complex programming techniques ever invented. Most books about the topic are hard to understand, so my advice is to use C++ for teachihg procedural programming only. That means, to strip down the language into a smaller one and leave out the details. Is it possible to write simple procedural programs with G++ and clang++? Yes, it is very pleasant. The only what the beginner has to do is not using the class or template statement. Instead he programs in C++ like in C. That means his program consists of some functions, and a main function and perhaps a struct as a global variable. It is even possible to write a graphical application with that setup …

From the perspective of a computercourse it might be hard to accept that leaving out the object oriented paradigma is a good idea. Because without any class, it is not possible to write larger programs, and perhaps the students will not have understand what C++ is. On the other hand is the difference between procedural programming and OOP not as big, as some books are explaining. In reality, there is not a real difference. The reason is, that a procedural programs works in a linear fashion, that the first command in main() is executed, and then the second, while the OOP paradigm works in the same way. The only difference is, that in OOP the structs are more powerful. But ignoring this is not a real problem. If someone needs really OOP, he can extend a given struct easily to a class. It is mainly a question of the correct typo.

What we see in literature today is, that the programming language are separated into two groups. On the one side, we have classical educational languages like Forth, Pascal and LISP. While on the other side we see industry standards like C++ and PHP. The difference is, that C++ and PHP are usually evaluated with their ability to program large applications in real life and their raw performance, while teaching languages like Pascal and Forth are classified to their value in a computer programming course. I think it is time to change the criteria a bit. A first it makes sense to say that Pascal is a poor choice as a real programming language because it is very slow while on the other hand, C++ is a good choice as a teaching language, because it is supporting the learning experience of the beginner. I would go a step further and call C++ the ideal combination between a practical language for writing real software and a subject for programming courses. I do not think, that the language is too complex or bad documented. There are a lot of papers out there which explain how to use it in a learning environment. The success has to do with the right curriculum, a good teacher and interesting projects.

On the other hand, the performance of C++ is heavily overrated. It is true, that the language is the fastest programming language with the ability to program even on assembly level. But this advantage has not the top priority in the teaching situation. It is the same, if the beginner can run a program in 10 seconds or in 60 seconds. And i would go a step further and recommend to use as default the sleep command inside C++ to reduce the execution speed, so that the learner can see slowly how the for-loop is counting upwards.

In the Windows environment the “sleep” command is available while in SFML an equal command is available. The idea behind is, to not using the complete power of the language or the hardware, but to drive the system in a beginner mode for understanding better what loops are and how the if statement works. The sleep statement makes C++ more predictable. The language is transformed into a cute snail which is easy to handle.

Lowlevel C++ programming

In a standard C++ book the hello world example is used as an introductionary example how easy C++ programming is in reality. And indeed the sourcecode itself is easy, but it is possible to add some detail information. The code::blocks IDE has the ability to view the registers and the assembly sourcecode in the debug mode. And now, the program is a bit more complicated. That means, we are not seeing the C++ code itself, but left on the screen the current situation on the hardware-level. We can go in singlestep mode step-by-step through the code and see what happens with the processor registers. It is a bit like Assembly programming on the Commodore 64 but with more information.

Who needs such feature? I don’t know. For a normal hello world program which puts out two strings on the screen it makes absolutely no sense for investigating the assembly program. But in theory, every program which is given by the sourcecode can be executed in such a way for finding hidden problems. A good example is perhaps the current version of the Lyx software, which crashes often. It may be interesting to execute the program in debug mode for reproducing the error.

The interesting aspect is, that this has nothing to do with classical C++ programming. Because the syntax of the language is less important. Instead it is some housekeeping tasks around the programming which is called debugging and tracing. I wouldn’t call it typical C++ programming, i would simply call it programming in general. That means to run a program in singlestep mode and observe the processor registers is nothing special for C++, other languages like Java, C, C# or Forth can be treated with the same intention.

How to program object-oriented?

The topic was explained in a former blogpost, but repetition is always good. What is OOP? At first, OOP is an advanced form of procedural programming. It is not what is totally know, but more an improvement of existing techniques. For learning OOP it is the best to start with procedural programming, for example in C or in Python. The idea is, that the programmer writes down some commands which are executed by the computer. For doing so, he has some basic functions for example, if, for, variables and a way for defining functions. A well written C program consists of some functions which are containing commands. Now the user can execute one of the functions, for example, “init()”, or “show()”.

What most programmers are talking about is the algorithm, which means how to reach a certain goals with the basic commands. For example it is possible to arrange the commands in a way, that they will print out the primenumbers up to 100, or they can search in a textfile for a pattern.

So far, we know what procedural programming is. But what is OOP? At first, it is nothing complete new, and it nothing to do with the real world, that everything is an object. No, OOP is simply a way for not only writing 5 functions in a C program but 50. The idea is to group the functions in higher classes. A class is like a program in the program, it has some functions and some global variables, called class attributes. Programming object-oriented means only to use one additional keyword (class) for grouping existing C-functions into higher classes. Everything else from procedural programming for example, the if statement or the idea that the programmer writes down an algorithm is the same.

Some C-programmers doesn’t like object-oriented programming very much. And they are right. Because the same feature can be realized with normal C language. An easy idea is to store the C-functions into separate files. So class1 would be file1.c and so forth. But in general using OOP in daily programming helps a lot, because it is more luxury.

At the end, I want to talk about the weakness in object-oriented programming. The problem is, that OOP alone do not answer how to program itself. For example, if someone wants to program a primenumber algorithm OOP alone will not help him. What the user needs is an algorithm. OOP is only an option for implementing an existing algorithm into run-able sourcecode. That means, if a programmer sees on stackoverflow a prime-number-implementation in Turbo-pascal, C or lua, he can easy create the appropriate C++ implementation of it. But if he do not know how the algorithm looks like is can’t program the sourcecode.

The reason why object-oriented programming is used so often in reality has to do, that it stores an algorithm into fixed sourcecode. That means, if the sourcecode is available the program can be executed on any computer worldwide. If the sourcecode is written the problem is solved. Object-oriented programming in general and C++ in detail is an universal language and quasi-standard in programming applications. The reason is, that C++ can be executed direct on a machine, even by people who have not written the sourcecode.