July 08, 2021

How to avoid pointers in programming

 

Pointers are everywhere. They are used for creating C code, C++ has them and even the assembly language needs pointers. The reason why pointers are there is because of technical reasons. They are mapping variables in the sourcecode to a physical memory address.
It is some sort of inside joke if C++ programmers are told to avoid pointers. This recommendation can't be realized. What programmers are doing instead is to create variables on the stack (which is not recommended). They are using shared pointeres (no obvious advantage over the classical raw pointers) and they are using the well known * and & operators which produces core dump messages all the time and the code becomes unreadable.
The only language not using pointers is python and perhaps this explains why the language has become so famous. How can it be, that Python doesn't need pointers? Because the python interpreter is using pointers internally and hides it from the programmer.
Perhaps it makes sense to compare the edges in pointer programmers. On a low level layer it is possible to write a program in the assembly language which is using pointers for getting access to the arrays in the memory. A simple hello world program will need 100 lines of code and needs additional comments to understand what all the move, push and call statements are doing.
On the other hand, a python program can do the same task in only 5 lines of code but has the advantage that it will run much slower. The C++ language is located somewhere in between.
From the history of programming language it can be shown how pointers are handled over the years. In the beginning there was only assembly and C. Assembly is great for writhing small amount of efficient code and C makes sense for writing larger software like operating systems. The only problem is, that both languages aren't fulfilling the requirement of modern programmers. The reason is that programmers are not interested in writing a single C application but modern software development is about writing thousands of software projects.
The surprising situation is, that even programmers newbies are doing so. They want to create not a single game, but the average newbie likes to write 5 different game, a serious gui app plus some web applicatations in under a year and he has no knowledge about programming at all. The C programming language can't fulfill these expections. It was designed only for writing low amount of projects.
Since the 1990s the C++ language was invented which can - in theory – fulfill the expectation of writing high level applications. but only the python language has fulfilled the demand for modern programming languages in the reality. Because python combines an easy syntax with object oriented features. Instead of advocating the usage of python it makes sense to ask in general what modern programming is about.
An interesting but seldom used example for a modern programming language is groovy. Groovy is the result of many layers which are put together. It will need an operating system, a java VM, some gui libraries and then the groovy code can be executed. Similar to python the amount of needed codelines for creating a game is low. And no pointers are needed beause it is handled by the underlying architecture.
The promise was to compare two very different programming approaches. The concrete question if assembly language or groovy is the better programming language. In terms of easier to use the groovy language wins the comparison. Even newbies are able to write their first program within minutes. The main idea is, that all the complicated problems are hidden from the user in underlying layers. Groovy puts the programmer in a sandbox and he doesn't care about performance, memory management or compiler options.
An unsolved problem of high level programming languages like python was in the past that the execution speed is low. In case of groovy the problem was solved because groovy can be compiled into native java code.
A typical sign for a modern programming is the reduced amount of needed codelines for creating something. In most cases the user can include a library and call a method which needs 2 lines of code and is pretty easy to read. To provide such high level functionality, many underlying software is needed which is an operating system and lots of in between virtual machines. For running a simple python or groovy example, the user has install around 10 GB of software before.
So the question is, how was software developed in the 1980s in which the typical 8bit computer was equipped with 40 kb of RAM? The answer is that at this time, a single programmer hasn't created 100s of apps but only 1. And in most cases he was part of a team so the programmer has created only a function in an assembly program.
Let us investigate what todays programmers are trying to archive. A typical entry level project for a groovy newbie is to rewrite 30 existing arcade games in groovy. That means, a single programmer is doing in a month the same task done by hundred of assembly programmers in the past. And because of this highly difficult problem, modern programmers are searching for programming languages which needs a little amount of written codelines.
There is an interesting situation available for programming languages popularity. Old school languages likes Ada, C++ or Assembly language are loosing it's popularity while modern beginner friendly languages likes javascript, python and java have found acceptance. This is more than only a fashion but it has to do with changing demands.
Let us make an example. Suppose the idea is to write 20 different GUI applications and use Assembly as the programming languages. it won't work. It wil take years until the newbie has learned the language and writing all the code will take decades. The combintation “writing 20 desktop apps and the assembly language” doesn't fit well to each other. What the user can do is to reformulate it's goal. For example he can try to program only a single program which is adding two numbers and then the assembly language is great.