March 22, 2020

The upraising of the C++ language

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.

March 11, 2020

C++ is obsolete

The major problem with the C++ language is the complicated pointer syntax. In contrast to modern OOP languages like Java and C#, C++ requires pointers at many situations. A look into existing larger C++ programs shows, that pointers are used together with classes very often. Not because the programmer doesn't know how to do it better, but he is using the fast programming technique available. That means, it's not possible to program in C++ in a different way. Even the latest iteration C++20 requires that the programmer prefers “point->x” over “point.x”.

The good news is, that the pointers in C++ are making more sense, if the same technique is realized in plain C. A hello world program which is using OOP plus pointers in the C language is given next:

// main.c
//------------------------
#include "stdio.h" // wrong brackets
#include "point.c"
int main()
{
  printf("main\n");
  point_run();
}

// point.c
//------------------------
typedef struct {
  int x;
  int y;
} Point;
void set(Point* p, int x, int y) {
  printf("set\n");
  p->x=x;
  p->y=y;
};
void show(Point* p) {
  printf("show %d %d\n",p->x,p->y);
};
void point_run() {
  Point p;
  set(&p,10,5);
  show(&p);  
}

It looks very similar to a C++ program except that no dedicated class statement was needed. At the same time, the programmer is storing the sourcecode in different files and splits the overall project into smaller programs which makes it easy to maintain the code.

A look into existing C repositories at github will show, that this sort of style is used by most programmers. Sometimes not in this direct clean form because the file length is longer, and more than a single struct is given in the text. But, if the programmer likes he can program in C similar to Python.

The assumption is, that we the C language everything is fine, it's not possible to replace the provided sourcecode with something which has more performance or can be written more elegant. Even if somebody rewrites the code in C# or Python it will look the same. The core idea of Python is:

- put every class in a new file which is less than 100 lines of code

- aggregate classes to more complex classes

- comment the code, create a documentation for the API

This is in short the best practice method to create modern object oriented code. The paradox is, that the old-school C language supports this idea very well. The syntax is a bit different from normal object languages. Because the function is available globally and a method needs a pointer to a struct before the class variable can be edited. But this is only a syntax decision.

What i want to explain is, that modern languages like C++, C#, and Java have struggled in replacing C code with something which can be maintained better. That means, all the projects from the past which were created in plain C but not in C++ are future ready. Nobody will rewrite C code with something which is easier to maintain, because C is the king already.

One important reason to prefer C over C++ is because the pointers in C are making sense. In the sourcecode, the pointer is the only option to copy the struct into a function. The result is, that all the C routines will look the same. It's not possible to write the code different. This is important for newbies which need a clear advice how to create a program.

Sourcecode browser



In the screenshot the geany IDE is shown together with the sourcecode. The frame on the left is very interesting. Geany has parsed all the structs and the functions from the file. If a single file is smaller than 100 lines of code, and if geany shows very well which datatypes and functions are defined in the file, the programmer has everything he needs. It's the same sort of overview provided in object oriented languages like Python or Java.

What exactly was the problem with C, why it's not used anymore? It is used, many thousands of games at github are using the tool. The only thing what is wrong are the programming books about C++, Java and C#. They are explaining to the user, that C is outdated and the user beliefs so.

Object oriented programming in C

Instead of using a dedicated OOP language like Python or Java, it's possible to create even in C an object oriented project. Some features are missing but in general it's possible for doing so. The only difference is, that the fucntion call doesn't need the full path to the module, but it can be called direct.

// file: main.c
// -------------------------------
#include "stdio.h"
#include "point.c"

int main()
{
  printf("main\n");
  point_show();
}


// file: point.c
// -------------------------------
typedef struct {
  int x;
  int y;
} Point;

void set(Point* p, int x, int y) {
  printf("set\n");
  p->x=x;
  p->y=y;
};
void show(Point* p) {
  printf("show %d %d\n",p->x,p->y);
};
void point_show() {
  Point p;
  set(&p,10,5);
  show(&p);  
}

The interesting question is, if such a programming style works for practical application. In the github archive there are many examples for games written not in C++ but in normal C. Many of them are using this style. The subparts of the game are stored in dedicated files which contains of a struct definition for the data and a list of functions for the C code. What these modules are doing is to to call their own functions, similar to the concept of classes in other programming languages.

Understanding the inner working is a bit harder than mastering Python OOP, because in a C project, pointers are needed for everything. But suppose the idea is not to use Python, C++, C# and Java, then this kind of programming style is a here to stay. It allows to write semi-object oriented software which scales very well. It's not a coincidence that most of real programs which are available out of the box in Unix and Linux operating systems were written in the C language but not in Java and not in C++. The reason is, that the advantage of dedicated OOP languages over C is small.

Let us take a closer look into the sourcecode. In the main file, only a simple call to a module is available. All the details of the Point module are hidden in the external file. In the point.c file, the datastructure is stored together with the functions in the same file. If the programmer takes care, that the maximum length of the file remains under 100 lines of code, it's not very complicated to maintain and bugfix the code. If the C code is rewritten in Python or C++ it will look nearly the same. That means, the overall project is divided into classes which are responsible for subparts of the project.

The assumption is, that writing larger projects in C can be realized with the same productivity like in Java or Python. That means, the C programmer won't miss OOP features, because most of them can be replicated with the C language. This makes it hard to convince a C programmer to convert the existing code into a different language. Basically spoken, the C language has a bright future and will be used very often.

Programming language statistics

The well known Tiobe index doesn't reflect programming languages in reality. There is a gap available that in computer education, Java and C++ is very important but in reality nobody is using these languages. A more realistic picture is counting the lines of code, https://www.openhub.net/languages?query=&sort=code

According to the Openhub directory the most used languages in the wild are:

1. C with 9.4 billion Lines of code
2. Javascript 4 billion LoC
3. XML 2.9
4. C++ 2.6
5. Java 2.4
6. HTML 1.4
7. PHP 0.9 billion
8. Go 0.6 billion loc
9. Python 0.7 billion
10. CSS 0.5 billion

In another older statistics, the C language outperforms also C++ easily in amount of written codelines. The sourcecode of the Debian operating system was measured in the year 2005 which contains of 105 million lines of code overall. 65% of them were written in C and only 12% were written in C++.[1]

There are some points against the C language. Most AAA videogames are not writtein pure C but the normal C++ language is used. And many developers are explaining that C is dead and they are prefereing C++ because videogames need object-oriented features. But, a closer look into the C++ sourcecode will show, that nearly all the game engines and the games written on top of the engines are using pointers in the C++ classes. It's not possible to avoid pointers in C++ because this ensures the maximum performance. What the programmers are writing in the code is not C++ but they are programming in C with pointers and using only the class statement and sometimes the templates of the C++ compiler.

Let us make a thought experiment. What will happen, if a larger computergame is reprogrammed in C? THe sourcecode will look nearly the same. That means, lots of pointers are needed to draw the sprites and call foreign modules. The difference between C a program which is using static functions to get access to structs and a C++ program which is using dedicated classes is low.

[1] Amor, Juan José, Gregorio Robles, and Jesus Gonzalez-Barahona. "Measuring woody: The size of debian 3.0." arXiv preprint cs/0506067 (2005).

Understanding pointers and references in C++

The C++ programming language is hard to understand. In contrast to modern OOP languages like Java, C++ knows many difference variables like normal variables, pointers, references and many more. Even the official manual isn't able to explain the details. The good news is, that a simple look into a C manual will help a lot to get the details.

But let us go a step backward: there are two different programming languages available: C and C++. Most software written in a Linux distribution wasn't created in C++ but in the normal C language. C++ is only teached in the books but seldom used in reality. The obvious difference has to do with object oriented programming. A while a go a stackoverflow users has asked how to realize classes in C:

https://stackoverflow.com/questions/1403890/how-do-you-implement-a-class-in-c

The answer was, that in a struct function pointers needs to be created. And yes, this explanation makes sense. And it explains why C++ is hard to grasp. Because C++ is doing the same but struggles in explaining the reason why. Suppose, somebody likes to program a state of the art program for the console or for the GUI, then the plain C language is the optimal choice. The funny thing is, that even the programmer is asked to declare function pointers the sourcecode is easy to read. If the plan is to not using pointers at all, the python language is a good attempt for software prototyping. It provides classes without pointers and is documented very well.

Instead of arguing against C++ we should ask why the language isn't used in reality more frequently.

But let us go back to the stackoverflow post with the function. A struct is a standard datatype in the C language. It allows to combine different variables into a new one. Extending a struct with functions is the logical next step towards advanced software. The work hypothesis is, that this kind of OOP technique is not an example for bad programming style, but it's the recommended way in programming modern software. The next interesting aspect is, that 95% of C++ programs have the same syntax. That means, pointers are used everywhere. The difference is that a C program which contains of structs and function pointers makes sense while the C++ doesn't makes sense for the newbie.

In the basic version, no function pointers are used, but the struct is provided as a pointer to a normal function:

#include "stdio.h"
typedef struct {
  int x;
  int y;
} Point;
void set(Point* p, int x, int y) {
  printf("set\n");
  p->x=x;
  p->y=y;
};
void show(Point* p) {
  printf("show %d %d\n",p->x,p->y);
};
int main()
{
  Point p;
  set(&p,10,5);
  show(&p);
}

In another stackoverflow post it was explained how to improve the struct with a function pointer. https://stackoverflow.com/questions/17052443/c-function-inside-struct But the answer says, that this is seldom used in reality. That means, the standard way of emulating OOP features in C is to define the function outside the struct but in the same .c file and call the function with a pointer of the struct instance.

March 09, 2020

How Linux will take over the business world

Microsoft is in the comfortable position, that 99% of all business oriented PCs are running under this operating system. From a technical point of view, these machines can be replaced with Linux software. And here is the road ahead. The first thing to do is to replace existing Microsoft SQL Servers with Linux systems. For applications like webserver, fileserver, printserver and user directory the Open Source systems are working stable. The next step is convert former desktop PC client into Linux stations. This can be realized with a terminal server. A terminal server has the idea to put all the database into the server, which includes the sql database itself, the middleware and also the frontend.

The citrix terminal server was mentioned already. The idea is, that the multi-user database is programmed on deployed on the server and the normal users are connecting with the Citirx server.

In an Linux environment a state of the art terminal server is the Gnome boxes software for the client. This software allows the user to connect to a remote desktop with the spice protocoll. On the server side the qemu/kvm software is installed which runs a desktop application. In the screenshot the Debian operating system was installed in a virtual machine and the Libreoffice calc program was started to enter a table. The user has to connect with the gnome boxes software to the remote qemu system.



This allows to convert an existing local table into a multi-user network ready table which can be used by many users at the same time. The idea is, that apart from the gnome boxes software, the user has no additional programs installed. He can use a normal Windows 10 PC and the Libreoffice suite is installed on the at the server in the virtual machine. Apart from Libreoffice calc any sort of database frontend can be run on the server. For example Java program or a python tkinter software.

Creating a middleware API with Python

The advantage of Python is, that it can be used for many things. It's used for creating GUI prototypes, testing out new algorithm, as a replacement for bash scripts or to program games. Python can even be used for creating the business logic in a database application and this should be described in the following blogpost.

The business logic is sometimes called a middleware API because it connects the frontend with the SQL backend. It can be visualized with a class diagram in the UML notation. The classes are realized with SQL tables which are connected to an Entity relationship diagram.

An UML class diagram explains very well what middleware is about. It provides a high level API to the outside world and it realizes the technical details with methods and an underlying database. The good news is, that Python has built in object oriented features. It can be used for creating classes and then the classes are filled with data.

An example is available online under the term “Northwind database”. Northwind is the example database introduced by the MS-Access software which contains the table for a fictional company. From the perspective of a relational database the ER-diagram is important but from the perspective of a middleware API the items are equal to classes.

Explaining what a middleware is can be realized by give the details of how a backend and how a frontend works. A backend is equal to a SQL database for example the sqlite software. SQLite communicates with the outside world with SQL statements. On the other hand a frontend for a database contains of a form generator which is able to draw windows on the screen. The user is allowed to press on buttons and gets the information he needs. Between the frontend and the backend there is a gap. That means, it's not possible to convert the output of the sqlite software direct into graphical information. This inbetween layer is the UML class diagram which describes the business logic from an abstract perspective.

According to Stackoverflow the simplest form of storing a python object into a file is the pickle module, https://stackoverflow.com/questions/4529815/saving-an-object-data-persistence But the pickle module will create a binary file which can't be used in external applications. The more elaborated form of storing python objects to a file is to convert it into the json format:

import json
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
  def getjson(self):
    return {
      'name': self.name, 
      'age': self.age,
    }
    
p=Person("peter",30)
print(p.getjson())

March 07, 2020

Small introduction into the Kexi software

The amount of tutorials about the Kexi-project is very low. The official forum has around 200 postings, and even youtube provides less than 5 videos about the topic. At the same time, there is a need for an Open Source desktop database, and Kexi is one of the most promising examples in that direction. The sad news for the beginning is, that the project is in early stadium. It can be called an alpha version which doesn't provide anything. Even MS-Access 2.0 has more features to offer and this program was realized 25 years ago.

But it makes no sense to be too critical. Because all the advanced existing database applications are close source projects and so called web databases on top of PHP and Ajax are very complicated to use and can't replace a desktop database. So i'd like to introduce Kexi to the newbies with some screenshots and explain what the developers have programmed so far.

The program is available in Fedora and Debian as well. According to the official description its an “integrated database environment for the Calligra Suite” and an “visual database applications builder” [1] The program has only a small size. The kexi sourcecode consists of 150k lines of code which is around 4 MB for the program.



The main menu looks exactly like a small project. The user is asked to create tables, forms and reports. The menu bar on top of the screen have no purpose, all the features have to do with tables, forms and reports. At first the user can create a new table. I have done so and the table stores the firstname and the last name of fictional students. Additional a primary key was defined which is incremented in the auto mode.



In the next screen the form editor is shown. Which is a minimalist one. There are some widgets available which can be dropped with the mouse into the form The most important one is the text field which holds the data of the underlying table. The data source can be provided right the properties menu. In contrast to the Gambas software (which is a visual basic clone) the connection between the database table and the form is working great. The user is able to see the data in the form.



The last item in the list is the report generator. This module has less features than the minimalist form generator but the user is able to create simple reports. He can drag and drop text fields, specify the underlying data source and with the printing driver of the Linux operating system it's possible to create a pdf document.



More features are not available. The official Kexi handbook explains mostly what is missing in the project and indeed the software is in an early stage. But compared to other software open source projects, Kexi is my personal favorite. It has to two main advantages. First one is, that the project is going into the right direction. The project goal is to program in C++ and integrated database software which contains of tables, forms, reports and scripting features. The second advantage is, that the code written so far is stable and the user can create a small but working database.

Let us make a simple thought experiment. Suppose enough manpower is provided for this project, and the amount of codelines grows by the factor 10 from today 150k to 1.5 million. Additionally some example .kexi files are provided and the forum gets more traffic. The prediction is, that Kexi will become one of the most interesting Open Source projects since decades. The reason is, that the normal user can do a lot of things with a database RAD tool. The options are endless. If such a software is programmed as open source with open standards in mind it can change the software industry.

I would guess that the next milestone for the project is to bring kexi on the same level like the outdated MS-access 2.0 software. I would guess that today kexi provides around 20% of the features from Access 2.0. Most of the interesting features are missing. But from a technical perspective it's possible to improve the software.

Importing CSV

Under the tab “external data” the user can import existing csv data. I have tested out the feature with an example csv file. [2] It works great. The csv file is converted into a kexi table.

disadvantages

Some major problems are visible with the software. The first one is, that after clicking with the right mouse button on a table and select “Export table as csv” the kexi program crashes without warning. It's not possible to export the table at all. This makes it hard to use the data for external purposes. The second problem is, that kexi has some preinstalled plugins which includes an sqlite driver, but it's not possible to connect to a sql lite database.

It's not clear what exactly the reason is why the important features are not working, but suppose the connection to a sqlite database works, and suppose the export filter gets improved, then the normal user would be able to use Kexi for some smaller databases. It has some advantages over LibreOffice calc to create a dedicated database, and create queries with the SQL language. As far as i can see the sourcecode is available so it would be possible either to contribute to the existing kexi project or fork the project and start a clone.

sources

[1] Debian kexi package, https://packages.debian.org/search?keywords=kexi

[2] CSV sample data, https://people.sc.fsu.edu/~jburkardt/data/csv/csv.html

Building a minimalist database from scratch

From a historic point of view, many database management system were developed over the years: dbase was used in the 1980s, MS-Access in the early 1990s, since the 2000s MS-Access used in combination with SQL server and since 2010 the situation has become very complex, because many companies are experimenting with Java, Linux, C# and PHP.

The idea is to throw away everything and reinvent a database from scratch with Open Source software. A good starting point is the sqlite software which is available out of the box in all Linux distributions. The problem is, that sqlite is only a small part of an overall database management system. To make things more comfortable a middle layer and a frontend is needed:

backend sqlite -> middleware python -> frontend python

Programming a python frontend is not very complicated. It has to do with drawing windows on the screen the wxwidget library and add some animations plus sounds to make the game more pleasant. The more complicated part is the middleware. To introduce the business logic layer we have to analyze who to interact with a normal sqlite database.

Suppose the user has started the python3 interpreter and is connected to the sqlite database. What the user can do is to submit an SQL statement. He has to type in the SQL request into the command line and gets the feedback from the database. A naive assumption is that the interaction can be improved with the help of a GUI frontend. On the longrun not a GUI frontend is needed but a middlelayer. A business layer is working on the textual layer and allows the user to enter high level textual commands. Instead of typing in:

SELECT * FROM Customers WHERE First_Name='John'

The user enters the command:

middleware.showcustomer(“john”)

The mdidleware program code converts the high level statement into low level SQL commands which are submitted to the sqlite database. The interesting point is, that no GUI interface is needed and the user can interact with the database very comfortable.