October 26, 2021

Introduction into object oriented programming

 

Before the powerful OOP style can be explained there is need to take a step back and analyze how programs were created before the invention of Object oriented programming. The idea was that there functions which are accept a parameter and then the function returns something to the main program. The sourcecode for a simple calculator app is shown.
# without class
def init(mylist):
  mylist=[2,4,6]
  return mylist
def add(mylist):
  result=sum(mylist)
  return result
def avg(mylist):
  result=add(mylist)/len(mylist)
  return result
  
def main():
  a=[]
  a=init(a)
  s=add(a)
  av=avg(a)
  print(a,s,av)

main()
Each of the three functions take a list as input and returns a value or a list as output. The idea is the main program communicates with the subfunctions with the help of the parameter.
In the example only a single parameter was used but the concept can be extended so that the function header take 5 and more parameters as input. It is not very hard to guess that this programming style works technically great but it is hard to read. The alternative is object oriented programming. OOP means that no parameters have to be send to the function but the function knows in advance which variables are needed.
The resulting oop syntax is very easy to read and it contains mostly of a statements like “object.dosomething()”. All the needed parameters are stored in the object already as class variables.
Somebody may argue that the different notation wouldn't result into a faster binary code. And he is right, OOP is mainly something for the programmer but not for the computer. In most cases OOP oriented compilers are harder to create and will produce slower binary code. But the technique allows to create larger programs.
Even if the OOP has felt a bit out of fashion since the 1990s it remains the leading programming paradigm. Most of code is written in this style and even C code was rewritten in an OOP style. It is a very influential programming style. From a historical perspective OOP were introduced in the mid 1990s to the main stream programmer. In that period most of modern OOP languages like java, C++ and python were created. It is very difficult to find a larger project which is not using the OOP programming style.
The interesting point is that the OOP concept can be extended drasticaly. It is possible to store in an object another object. This allows to increase the amount of codelines further. At the end the program wil need 10k and more lines of code which is occupied by lots of classes. From a technical perspective such a program is not very hard to hard to compile or to execute. A program which has 10k lines of code fits into 400 kilobyte of disc space. The resulting binary file will need also around 400 kb of RAM. Compared to the average RAM of a computer this is very little amount of space. But creating all these codelies takes a long time. A single programmer will need many months until the program was created.