October 21, 2022

Modular programming with python

 

Sometimes it was asked how to implement object oriented programming in low level languages like C and assembly. A possible idea would be to improve the existing struct datatype with functions which are pointers. Then the user has to allocate memory and can use objects in C. But the overall pipeline is very complicated and there is an option available to simplify the idea drastically.
The idea is to dismiss object oriented programming at all and prefer modular programming. Modular programming is a complexity reduction technique which is available in most existing programming languages. It can be used in python and in C and pascal as well. The idea is that a single file is equal to a module. [1] The file contains of variables and functions and comes close to the concept of a class. in the written sourcecode a program written with modules and one with classes are looking nearly the same. A module allows to group items in the sourcecode into cluster and maintain the code separately.
Most object oriented programs are in reality normal modules distributed over many files. The only reason why OOP is the dominant programming paradigm and modular programming has become a niche is because of the existing programming tutorials. The self understanding of most books is, that a programming language consists of statements and functions and this enough to solve a problem. The problem is, that without classes aka modules it is impossible to solve larger problems. If the code is longer than 500 lines of code there is a clear need to split it into sub entities. Unfortunately this strategy is not implemented directly in a programming language but it has to do with project management and compiler preprocessors. It can be s
From the perspective of modular programming it is surprising to see, how similar programming works in different languages. In contrast to a common myth the programming languages Pascal, Python, C++, C and , Assembly and Forth have one thing in common. They are supporting all the concept of units. A unit is – again – a single file which can be included in other files and allows to split the code into logical groups. Similar to OOP it allows to group variables and methods into the same instance.
To understand why modular programming is not very common it is important to take a look back into the didactic of programming. In the 1980s the concept of structured programming was mentioned frequently. Structured programming means to divide the sourcecode into functions which can be realized with C and pascal. Advantage is, that each function can be maintained separately which allows to writer longer programs. What was ignored in the 1980s is, that structured programming alone is useless because if a file contains of around 10 functions it will become hard to maintain anymore. So the limit is, around 500 lines of code which can be managed with structured programming alone.
In the 1990s the concept of object orientation was promoted as the logical next step after structured programming. The idea was to encapsulate the code in object libraries which allows an unlimited code size. Nobody has questioned the idea at all or compared it with modular programming which provides a similar feature.
In theory, OOP is superior over normal modular programming. But in the reality, most programs won't profit from these additional advantages. Nearly all projects which are smaller than 100k lines of code can be realized with modular programming very well.
[1] https://python-course.eu/python-tutorial/modules-and-modular-programming.php