Procedural and Object Oriented Programming

*Disclosure: This post may contain affiliate links, meaning, at no cost to you I may get some coffee money if you click through and make a purchase.

Introduction

Now that we have our Visual Studio Development environment setup and have some background in software (check out my previous posts) let’s start looking at Procedural Programming and Object Oriented Programming (OOP). During this post I want us to take a quick look at the history of the two main methods of software development, that is Procedural and Object Oriented, the differences between the two and then delve deeper into OOP. I started my introduction to programming with procedural before moving into OOP using C/C++ and as I will be focusing on using C# for most of my posts we’ll be taking a far larger look at OOP than procedural. So let’s get started…

Procedural Programming

Procedural is where we have variables, that is a container we use to hold our data, and procedures/functions, this is a defined section of code that performs ideally a single task (I say ideally because this isn’t always the case, I’ll be producing a post on some programming principles where we’ll cover these in more details) based on some information it may require and may or may not give something back, lets see an example:

In the case for the variable, our data the system will hold, which would be considered it’s ‘state’, we’d normally find the data held in variables (check out What is a Program? for more info) such as

int age;
string name;

The example above shows two variables,

  • An int called age
    • An int is a whole number, it cannot have any decimal places.
  • A string called name
    • A string holds characters, that is letters, punctuation, and other symbols

So with age we can store any whole number and if we want to get to that value we use the name ‘age‘ to do so, the same applies to name, we can store someone’s name in the variable and if we want to get the value of name we just use the variable name to get access.

For both of these variables we’ve given them useful names and can assume they are related to a person in some fashion.

Quite straightforward for variables, now let’s take a gander at functions, which would be considered the systems ‘behaviour’:

int add(int x, int y)
{
    return x + y;
}
  • The first word ‘int‘ is the data type that will be returned from this function
    • Returned means at some point, normally the end, the function gives data back using the ‘return’ keyword to identify what holds that data.
  • The second word is the name of the function ‘add‘, callers to this function will use this name to tell the system that is the function they want to use.
  • The opening parenthesis ‘(‘ is used to identify the beginning of the values the function expects to receive, this is called a ‘parameter
  • Now, we need both the ‘int‘ and the ‘x‘ for this bit, the ‘int x‘ section tells you we want an int called ‘x
    • Think variable.
  • The use of ‘,’ is simply to separate parameters so if the function expected 3 parameters it would be:
int add(int x, int y, int z)
{
    return x + y + z;
}
  • The opening curly brace ‘{‘ is used to define the start of our scope, scope is simply the start boundary for our function and the closing curly brace ‘}’ defines the end of our scope.
  • The content of our function, which is very small, is found on line 4
    • All we do here is add x and y together then return that new value as an int, quite simple.

Let’s take a quick look at a very simple program (in C++):

// So this is our function, we expect to be given two values x and y
// and we'll give back a new value which is the result of adding x and y
int add(int x, int y)
{
    return x + y;
}

// As mentioned in a previous post, this 'main' method is quite important 
// and is the starting point for a lot of programming languages, in this case C++
// We'll not be looking at that here though
int main(string[] args)
{
    // Our first variable of type int called starting_balance and it has been assigned a value of 100
    int starting_balance = 100;

    // Our second variable of type int called deposit_amount and it has been assigned a value of 50
    int deposit_amount = 50;

    // Not here is our third variable called end_balance but it is not assigned an amount directly instead we are 
    // calling the add method and passing the starting_balance and deposit_amount variables to the function.
    // Which we expect the add function to return another int to us, and it is that value we are assigning to 
    // end_balance (which is 150)
    int end_balance = add(starting_balance, deposit_amount);

    return 0;
}

The above is not a very useful program mind you but hopefully proves the point.

So that is an exceptionally quick and shallow overview of Procedural Programming but gives us some useful information and I hope shows we can develop anything we want really. My background has been more focused on Object-Oriented so let’s move on to that.

Object-Oriented Programming

Now then let’s check out Object Oriented Programming (OO or OOP)…

In our look at Procedural Programming, the state and behaviour do not really have a strong relationship, in the world of OOP that is not the case. The Object in OO refers to an entity within the OO domain and that object contains both the state and the behaviour, that is the variables and the functions, which in this context are generally called attributes and methods respectively. An object is a self-contained representation of ‘something’, such as a person, shape, car, or anything else, that has both state and behaviour.

The reasoning here is how do we prevent anyone from changing the state within a procedural program? It’s a bit difficult, so by wrapping state and behaviour up in a nice object, which is defined in something called a class, we can control access and manipulation to that state, we call this Encapsulation (and it is one of the ‘Pillars of Object-Oriented Programming’) because we’ve ‘encapsulated’ both state and behaviour within a single container. The next stage is the use of data hiding, another principal in OO, here we restrict how that state is changed and only allow specific ways of changing the state. That would be by the use of those methods mentioned earlier, that way we can ensure only changes we allow can be made to our state. The really nice thing about this is by restricting access to an objects state we prevent any accidental (or intentional) corruption of that state. We can, and should, hide a lot of the internals of an object from the outside world by use of access modifiers, that is we only provide very specific methods to change an objects state and we hide everything else. The easiest way I find people understand this is by thinking of a car, now not everyone fully understands how the internals of a car work, for example how exactly a combustion engine works, and because of the design of a car, you really don’t need to know this. All we, the driver, need to know how to do is to make the vehicle move that is switching the car on, steering it, accelerating, braking, changing gear, adding fuel, etc, you only need to know how to operate it you don’t need to worry about anything else. So as programmers we can do the same, we can provide users of our objects with simple methods and hide anyway all of the complexity.

So there are a few terms in the previous paragraph that need some explaining, (right?):

  • Attribute:
    • An attribute is a variable held within a class, it should not be accessible to the outside world
  • Method:
    • A method is a function held within a class, it can be accessible to the outside world or hidden
  • Class:
    • A class is simply a blueprint for an object, we use classes to define attributes and methods within a container
    • Another good analogy is a blueprint for a house, we use the blueprint to define how to build a house but it is not the house itself
  • Object:
    • Using the above analogy an object IS the house, it is what we have created from the class, the blueprint
  • Access Modifiers:
    • Access Modifiers are keywords we assign to our attributes and methods, this allows us to control access
    • For now, think of there being just two access modifiers:
      • Private: This means only the class has access to the attribute or method
      • Public: This means any entity can access the attribute or method held in a class

I think it will be worth looking at an example (in C#):

// Here we use the keyword class to identify we are creating the blueprint
// We have called the class Person, similar to how we create variables; 
// start with the type, in this case its a class, then give it a name, 'Person'.
public class Person
{
  // So here we are defining 3 attributes, notice the access modifiers.
  // The first is an int called age, we give it an initial value of 0
  private int age = 0;
  // The second is a string called name
  private string name;
  // The third is a string called message
  private string message;

  // Here is a method we have set to public, this means the method can be 
  // access by any holder of a Person object.
  public SetNameAndAge(int newAge, string newName)
  {
    age = newAge;
    name = newName;

    // UpdateMessage isn't available to anything outside of the scope of the class
    UpdateMessage();
  }

  // This is our first private function
  // The type 'void' means we do not return anything from the method
  private void UpdateMessage()
  {
    // So we update the message variable to the following
    // The use of quotes defines a string
    message = "Hello, " + name + ". You are " << age " year(s) old.";
  }
}

This is a very simple class but I hope it helps to show some of the above content in an actual example, check out my previous post as well about What is a Program? to add some additional value.

Why Object Oriented?

Ok so we’ve had a very quick look into OO, but why would we want to use it instead of saying Procedural. I’ve touched in it very slightly above but because of the Four Pillars of Objected Oriented Programming. They are:

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

we’ll be looking at the Four pillars exclusively in another post, so keep an eye out for that.

I hope you’ve found this post a little bit useful and if you have any questions or there’s something I haven’t explained very well please feel free to get in touch.

I welcome feedback, especially at this very early stage, so any improvements you think I can make please get in touch either in a comment below or send me a message and I’ll get back to you.

Aaron

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment