Procedural and Object Oriented Programming


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

  // 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.

Setting up a Visual Studio 2017 Environment


Hey all, so today I’m going to give you a quick run through for setting up a development environment using Visual Studio 2017 Community on Windows 10. There will be a tutorial on how to do this on a Linux environment with a different IDE but for now as I will be focusing on Visual Studio for development.

Not much else to say there, let’s get into it…


Up to date Windows 10

The first thing we want to do is ensure that our Windows 10 development machine is up to date.

  • This can be found in Settings -> Update & Security -> Windows Update

Download Visual Studio 2017

Now we are happy our machine is up to date next we want to download the Visual Studio 2017 installer. So the next thing we want to do is download Visual Studio 2017

  • There are other versions available for those with the required Key or Subscription level, they are:
      • Visual Studio 2017 Professional
    • Visual Studio 2017 Enterprise

We will only be looking at Community Edition, but the steps will be the same.

Install Visual Studio 2017 Community Edition

  • Once you’ve downloaded Visual Studio lets install it

  • Double click on the installer
      • You’ll find it in your Downloads folder normally set to ‘C:\Users\<Your Username>\Downloads’
    • You may need to approve the installation via the User Account Control authorisation window. I assume as you are here you’re happy to install Visual Studio 2017, so click ‘Yes’

  • Next up will be to review Microsoft’s Privacy Statement and Software Licence Terms
    • Pretty standard stuff.
    • Click ‘Yes’

  • The Installer will then fetch and install some additional dependencies

  • So, now we get to configure our Development Environment. For our purposes I’m only going to choose those components we MAY want to use in the future, that is components I intend to use in any posts I make. You can, of course, install any components you wish outside of my choices. So we will want:
      • .NET desktop development
      • Desktop development with C++
      • Universal Windows Platform development
      • ASP.NET and web development
      • Azure development
      • Python development
      • Data storage and processing
      • Mobile development with .NET
      • Visual Studio extension development
    • .NET Core cross-platform development

NOTE: You will need to ensure you have enough space to install the components. Please see the red box the above image it shows the ‘Total space required’ for your configuration.

    • You can change where you want the installation to go, see the green box in the above image
      • In some cases, some of the components will have a set installation path which you cannot change.
  • That’s it, for now at least, the Visual Studio Installer will now install Visual Studio, this will take a little while. Could be worth grabbing a brew or something and if you haven’t done so already check out some of my other posts.

Start Visual Studio 2017 Community Edition

Great, so Visual Studio should now be installed for you, I recommend you restart your machine but it’s not necessary. Let’s open Visual Studio now then, there’s a couple of ways to do this:

  • Start Menu
      • Open the Start Menu using the Windows key
      • Scroll to ‘V’ named apps
    • You will see ‘Visual Studio 2017’, click it.
    • Desktop Shortcut
        • Navigate to your desktop, press Windows Key + D
      • Double click, ‘Visual Studio 2017’
  • Search bar
      • Open the Start Menu by pressing the ‘Windows Key’
      • Type ‘Visual Studio 2017’
    • Visual Studio 2017 will now be in the Start Menu Best match options

There will be some configuration we need to do as part of our first startup

  • You’ll be asked to sign in to your Microsoft account if you have one.
    • If you don’t, I’d recommend you get one and sign into it.
    • If you do, sign in
      • Especially if you have an MSDN subscription
  • Next up and nearing the end, some simple IDE configuring
    • Leave Development Settings as ‘General’
      • Though feel free to look into this in more detail and selection and option more suitable for your plans
    • Then choose your Color Theme

We are done, Visual Studio  2017 will now be installed and you can take a look round if you want.

Hopefully, my steps were useful and you managed to install it. If you have any problems feel free to get in touch and I’ll help in any way I can (though I imagine you’ll likely google any issues).

Next up then we’ll make a very simple C# application, should be out shortly.