Object Oriented Programming Is Your Foundation

Methodology Add comments

Object Oriented Programming is your foundation

This is the first part in a mini series on building a solid foundation of principles and practices that will enable you to write better code.

Object Oriented Programming is going to be the foundation of everything we talk about from here on out. OOP provides huge power and flexibility later on down the line when we discuss techniques to help in the maintainability of our code.

As a side note, you will find that I tote maintainability as a hugely important factor. To me, maintenence is the biggest comsumption of development resources whether it be through bug fixes or adding new features. Anything that can make code easier to maintain long term is, in my book, worth exploring.

I am well aware that OOP has had some strong opposition and criticism including Richard Stallman (see his comments). Perhaps OOP is not for everyone but it has great value to me.

If you feel that OOP is not necessary or is a waste of time then I present to you two options:

  • Stop reading this series and continue to ignore an opportunity that might improve your coding skills
  • Reevaluate your opinon of OOP by reading the Wikipedia page linked above. Then continue this series.

Why Object Oriented Programming?

There are a lot of reasons to use Object Oriented Programming.

  • Code resuse. I try and follow the princliple of Don’t Repeat Yourself (DRY) as much as possible. OOP helps by easily allowing you to create reuseable components
  • Extensibility. When you can easily implement or extend your classes it makes your code more flexible and robust.
  • Maintenence. When your code is modeled after real life objects and concepts and your code is flexible it is easier to maintain. There is nothing worse than trying to make changes to poorly designed code.

The thing I like most about OOP is the ability to mimic the business objects in code as they are in real life. To me this makes the code far more readable and easier to maintain. Let’s look at a simple example (I am using C#).

public class Pet {
	private string name;
	private string species;
	private string favoriteFood;

	public Pet(string name, string species, string favoriteFood) {
		this.name = name;
		this.species = species;
		this.favoriteFood = favoriteFood;
	}

	public string Name {
		get { return name; }
	}

	public string Species {
		get { return species; }
	}

	public string FavoriteFood {
		get { return favoriteFood; }
	}

	public virtual string Speak() {
		return "My name is " + this.name;
	}
}

The above code, being modeled after a real life object, a pet, is easy to understand and should be intuitive as to it’s use. Another great thing about our above class is that we can easily inherit from it to create a similar, yet more specific, class.

public class Cat : Pet {
	public Cat(string name) : base(name, "cat", "mice") {

	}

	public override string Speak() {
		return "meow";
	}
}

See how easy that was? If in the future our program requires all Pets to have new features we just update out Pet class and all classes that inherit from Pet get the new features as well. If you haven’t guessed I like things with low maintenence costs.

That’s about it. I know I didn’t go into a lot of detail explaining all the aspects of OOP. That is just to much for this post. It could easily be a lengthy series of it’s own. I would strongly encourage you to do some reading on OOP if you need to know more.

kick it on DotNetKicks.com

Popularity: 8% [?]

If you liked this article consider subscribing to my free rss article feed to automatically get new articles in your feed reader.

3 Responses to “Object Oriented Programming Is Your Foundation”

  1. Mike Borozdin Says:

    Agreed. OOP is the foundation now. I’m even surprised by this post, because it seems to be really obvious.

    As for Mr Stallman, he seems to be have been a critic of everything for the last 20-30 years, but I cannot name a product made by him… So, who really cares ;-)?!

  2. Justin Says:

    Well, I would have thought it was painfully obvious that the world uses OOP but our company recently hired a programmer, though writes very good OOP code, admits he thinks it is a waste of time and doesn’t see how it is better than procedural programming. Then again, he has been living in UNIX only and PERL for the last 10 years until getting hired by us (a .Net shop, don’t ask…I don’t do the hiring :D)

  3. Geek Daily » Exception Handling The Right Way Says:

    […] Part 1 - Object Oriented Programming Is Your Foundation […]

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Login