Paradigms of Programming

Object orientated programmiing (OOP) and functional programming (FP). I'd heard both terms often enough to wonder what each involved. I knew that OOP in Python involved classes. I have not been using classes. I use functions though. Must be FP I'm doing.

No! Stop! Not the case at all. Functional programming is very different. Specialised. Math based. Declarative. Turns out it's structured programming that has been my default style. General purpose. Got more of an idea now how to structure code into a programme.


Procedural Orientated Programming (POP)

  • AKA procedural or imperative programming style.
  • A traditional procedural program is organised to take input data, process it and produces result. Programming is centralised around logic rather than data.
  • The data (stored in variables) is passed to a defined function which in turn performs some action and modifies it or creates new data.
  • The style may be thought of as a list of instructions which gets executed in an orderly manner defined by control flow statements and functions.
  • In POP write your code the way you would write an essay: from top to bottom.
  • There is no connection, no relationship between the data being used and the procedures which use them. One procedure could alter a data structure and then a seemingly unrelated procedure could also alter it.

Back in the eighties I was introduced to BASIC using my dad's Commodore 64, a friends ZX Spectrum and a BBC Micro. BASIC was very much POP. Code lines were numbered as ’10, 20, 30’ etc. If I wanted the program to repeat something done earlier, then the command ‘GOTO [line number]’ or 'GOTOSUB [line number]' to get it to jump back, do the thing and then continue to progress as before.

As a POP code base gets larger it can become complicated to navigate. With commands that jump between points it gets tricky to follow the flow. Finding errors when things go wrong may be a challenge. Code like this is sometimes called 'spaghetti code’.

Structured Programming

Structured Programming came about to overcome this problem. With the paradigm came the ability to reduce if not elminate the necessity of employing the GOTO statement.

  • Structured Programming is AKA Modular Programming
  • Structured Proramming is a discipline or subset of Procedural Programming.
  • It aims to improve the quality and clarity of the code. The execution of the program becomes more logical and readable. Structured programming involves 4 elements: sequence, iteration, selection, and subroutines.
  • Sequence: As for POP, code is written in sequential order top to bottom.
  • Selection: Such as if…elif…else statements. The execution of the subsequent code depends upon the selection statement.
  • Iteration: Execute a block of code again and again with the help of loop statements.
  • Subroutines: Programs are devided into a set of functions. Each of these functions performs a subtask. Having each function represent a specific functionality can make it easier to test and debug the code.

The focus of procedure orientated and structured programming is to break a programming task into a collection of variables, data structures, and subroutines. Small programs and scripts tend to be easier to develop using a simple procedural or structured programming approach.

Structured programming is a logical programming method that is considered a precursor to object-oriented programming (OOP).

Object Orientated Programming (OOP)

  • Object oriented programming uses 'objects' AKA 'instances' to to represent real-world scenarios and/or objects.
  • An object has states and behaviours.

    • Car, Person, Team etc. could all be objects.
    • States represent attributes or data of an object.

      attributes:
      
      make = 'Toyota'
      age = 21
      size = 'activity'       
      
  • Methods (functions in structured programming) represent the behaviours of objects.

  • An object is defined by a class. A class describes how an object is designed, i.e. which attributes and methods it has.

  • A class should not be confused with an object.

  • Objects may interact with other objects.

As for structured programming there are four main principles in OOP.

  • Encapsulation – attributes and methods are bound into a single unit - an object/instance of a class.
  • Inheritance – allows a class to use properties and methods of an already existing class. This makes the code reuseable and quicker to modify if needed.
  • Polymorphism – allows an object to perform in multiple ways. Methods and properties of the parent class can be modified as required. New methods and attributes may be added. Class attributes may be overwritten per instance. Objects are powerful!
  • Abstraction – hides the internal details and displays only the functionalities (of the instance) to the user – Moreover, abstract classes and interfaces (CLI, GUI) help to achieve abstraction.

Interest leading to this learning was prompted by a project I am working on. An allocations / shift coordinations app. I was stuck. Virtues of OOP had come into my awareness. Reading up a bit on structured programming and OOP seemed a good thing to do. This has led me to think an OOP approach may be the way to go and something I should learn.

Progress is being made with understanding OOP. Code has been refactored. The problem I was stuck on got solved using an instance method of an object created with the built in function iter() Allocate two staff to a task if a conditon is met else just one by default.

    if patient[1] == '2:1':
        print(next(staff_iter), staff_iter.__next__())
    else:
        print(next(staff_iter))

You'll only receive email when they publish something new.

More from Alexander