Skip to content


Object Oriented Thinking for the Procedural Programmer

In procedural programming we typically tell the system what to do in a line by line sequence of events. The data being used was globally available to enable various functions to act on it. Many developers today are using a mix of procedural and object oriented techniques. While they may be using an object oriented language and object oriented libraries or tools, the actual code tends to be rather procedural.

Many tutorials use “Hello World” as a sample application to teach. Interestingly most of these examples also use this mixed mode.

Look at a simple example

public function sayHello():String
{
	var returnVariable:String="";

	var name:String="Bob";

	returnVariable= "Hello " + name;

	return returnVariable;
}

This is really a simple top down procedural approach. So what is Object Oriented programming then. First let’s find out what an object is

What is an Object

You deal with objects every day in the real world. Your coffee mug is an object, your mouse is an object, heck you’re even an object. What does this have to do with programming? When we think in OO for our development we need to think in the same way we do in the real world, about objects. Lets take a few items from the real world and use them in OO programming. First look at your coffee mug. It has some data points or attributes about it such as color, capacity, temperature, available space etc. Some of those elements are fixed and shouldn’t be changed such as color and capacity. There are other attributes that can change such as available space.

Let look at a procedural example

public function showMug():String
{
	var returnVariable:String="";

	var mugColor:String="White";
	var mugCapacity:String="14oz";
	var mugContents:String="";
	var mugTempurature:String="";
	var mugAvailableSpace:String="";

	//Fill mug with coffee
	mugContents = "12oz Coffee";
	mugTemperature = "Hot";
	mugAvailableSpace = "0";

	// Check out our mug
	returnVariable=returnVariable + "Color: " + mugColor + "\n";
	returnVariable=returnVariable + "Capacity: " + mugCapacity + "\n";
	returnVariable=returnVariable + "Contents: " + mugContents + "\n";
	returnVariable=returnVariable + "Temperature: " + mugTempurature + "\n";
	returnVariable=returnVariable + "Available Space: " + mugAvailableSpace + "\n";

	return returnVariable;
}

Pretty straight forward. There are a few challenges though. How do you ensure I can’t put in more than the capacity. What’s to keep line of code from changing the temperature, color, or capacity. And finally  how do you deal with more than one mug. Yes you can code for a lot of these issues procedurally but the code gets complex and really you can’t enforce the rules you put in place since the objects are global.  Object Oriented programming allows you to do this.

In an OO model we would have a mug, or multiple mugs, each with its own current status. Once we got our mug, nothing would be able to change the color or capacity. While the temperature may change over time, this should be determined by the mug not some other code. For example I have a thermal mug which keeps my coffee hot for 2 hours. But I also have a regular ceramic mug that only keeps it warm for 20 minutes. The mug really determines how the temperature changes.

So lets make a mug

This mug and all objects really is just an isolated bit of code that can be reused. There are some rules for Objects but we won’t worry about them now.

package
{
	public class Mug
	{
		// define the various attributes but don't set them
		// Mark them private so they can't be updated by anyone but this mug
		private var _mugColor:String="White";
		private var _mugCapacity:String="14oz";
		private var _mugContents:String="";
		private var _mugTempurature:String="";
		private var _mugAvailableSpace:String="";

		// create an initialization function or constructor
		// that will be used to setup some attribute data
		public function Mug(color:String, capacity:String)
		{
			_mugColor=color;
			_mugCapacity=capacity;
			_mugTempurature="HOT!";
		}

		//enable the contents to be set
		public function set mugContents(value:String):void
		{
			_mugContents = value;
		}

		// enable everyone to see what our attributes are
		public function get mugColor():String
		{
			return _mugColor;
		}

		public function get mugCapacity():String
		{
			return _mugCapacity;
		}

		public function get mugContents():String
		{
			return _mugContents;
		}

		public function get mugTempurature():String
		{
			return _mugTempurature;
		}

		public function get mugAvailableSpace():String
		{
			return _mugAvailableSpace;
		}

	}
}

Now lets use it

public function showMug():String
{
	var returnVariable:String="";

	var mug1:Mug=new Mug("White","14oz");

	//Fill mug with coffee
	mug1.mugContents = "Coffee";

	// Check out our mug
	returnVariable=returnVariable + "Color: " +  mug1.mugColor + "\n";
	returnVariable=returnVariable + "Capacity: " + mug1.mugCapacity + "\n";
	returnVariable=returnVariable + "Contents: " + mug1.mugContents + "\n";
	returnVariable=returnVariable + "Temperature: " + mug1.mugTempurature + "\n";
	returnVariable=returnVariable + "Available Space: " + mug1.mugAvailableSpace + "\n";

	return returnVariable;
}

At first it appears we just added some structure to our code and split this into two files. If you look closer you’ll see that we can’t change the color or capacity after we get our mug.

Lets make this into a coffee shop and use lots of mugs.

public function showMug():String
{
	var returnVariable:String="";

	var mug1:Mug=new Mug("White","14oz");

	//Fill mug with coffee
	mug1.mugContents = "Coffee";

	var mug2:Mug = new Mug("Silver", "8oz");
	mug1.setContents("Tea");

	var mug31:Mug = new Mug("Red", "6oz");
	mug1.setContents("Chai");

	var mug4:Mug = new Mug("Blue", "12oz");
	mug1.setContents("Hot Chocolate");

	var mug5:Mug = new Mug("Black", "10oz");
	mug1.setContents("Milk")

	outputMug(mug1);
	outputMug(mug2);
	outputMug(mug3);
	outputMug(mug4);
	outputMug(mug5);

	return returnVariable;
}

private function outputMug(mug:Mug):String {
	var returnVariable:String="";

	returnVariable=returnVariable + "Color: " +  mug1.mugColor + "\n";
	returnVariable=returnVariable + "Capacity: " + mug1.mugCapacity + "\n";
	returnVariable=returnVariable + "Contents: " + mug1.mugContents + "\n";
	returnVariable=returnVariable + "Temperature: " + mug1.mugTempurature + "\n";
	returnVariable=returnVariable + "Available Space: " + mug1.mugAvailableSpace + "\n";

	return returnVariable;
}

Not much extra code and lost of reuse.

This has been a very brief intro to Object oriented programming. There is a lot more to OO, but taking these first steps will reduce the amount of code you need to write as well as increasing the quality of your application.

Posted in Uncategorized.

Tagged with , , , .


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Dan Koscielak says

    This is a great example! Very easy to understand and visualize the difference between OO & procedural. Please go further! There are so many resources out there, but very few are this easy to follow and understand.



Some HTML is OK

or, reply to this post via trackback.


Get Adobe Flash playerPlugin by wpburn.com wordpress themes