This is the first of five articles on object oriented-programming.
- An Introduction to Object-Oriented Programming
- Thinking Object-Oriented
- Planning an Application
- Creating Objects
- Making Objects Reusable
An Introduction to Object-Oriented Programming will give a brief overview of why there is such a thing as object-oriented programming. Conceptualizing Systems will help us to better picture a system in terms of objects and how the relate to each other. Planning an Application will teach us the importance of planning and what we should do to plan. Creating Objects explains how the objects in our application are born, and how they should be structured. Making Objects Reusable covers some more advanced topics and allows us to reuse what we’ve already created in good ways. My hope is that these Monday morning articles will help others through the transition from procedural programming to object-oriented programming.
Why Does Object-Oriented Programming Exist?
Back in the olden-days, way back when dinosaurs were young and our parents roamed the earth, there was programming. Yes, hard to believe it was invented by that time. Back then, things were simpler. Maybe people had to walk uphill both ways to school then, but programs were smaller, less complex. Programming languages executed line by line. They had “go-to” statements that let them go to another line for execution, and that was nice, but they didn’t need a whole lot anyway.
Industrialization came along however. Change was happening. Programs became more complex. It was hard to wrap the mind around a complex program and write it line by line. Procedural programming was born. Procedures or functions allowed a programmer to more easily conceptualize a system. It allowed them to reuse code. It was easier to program.
Object-oriented programming (OOP) came about the same way. When programs became larger and consequently more complex, it became very difficult to wrap one’s mind around them. Object-oriented programming made programming a system easier. It made life simpler again.
Function, Function, Who’s Got the Function?
Many times procedural programming is referred to as “spaghetti code.” Why is it called spaghetti code? Because there are functions being called throughout the system and we can’t know where they are coming from. If we follow the flow of a procedural program, we can find ourselves looping in and out of functions and throughout scripts very much like a bowl of spaghetti. There is not (inherently) much structure. There is not much organization. Object-oriented programming becomes more like building blocks we can build with instead of spaghetti. It provides organization and we can know exactly where a given function is. This also makes it easier to know where a bug is coming from.
What Does Object-Oriented Programming Allow Us To Do?
Object-oriented programming allows us to reuse code. It gives us a “package” of related functions and their related variables that we can pass around or reuse in other systems. These functions and variables that are all related are called methods and properties in an object. Object-oriented programming can also enable the use of complicated code by everyone. It not only ports the methods and properties together, but also allows us to use them without figuring out how they work internally.
When Should We Use Object-Oriented Programming?
Object-oriented programming was conceived to allow us to create larger projects more simply. Thus, it is very helpful in our larger projects that we do. I can also be nice in smaller projects, especially once we get the hang of it. Small projects are easily done in procedural programming though, and even once we learn how to program object-oriented we may still find ourselves using procedural at times.
Is it ok to mix objects with our procedural code? Of course! Many feel that if they are going to program object-oriented that they need to jump into the deep end and just start swimming. That is just not true. It would be much better to start using objects bit by bit, more and more, than to just not use them at all because we feel that it is too much for us. Also, for small scripts it may just be easier to do them procedurally.
So What Is Object-Oriented Programming?
Object-oriented programming is a way to think. To conceptualize a system or a program. It is not the classes, syntax, encapsulation, or other big words that we will learn about in following articles. The true purpose of OOP is the empowering it gives us to see our programs in a new light, a higher view, a clearer picture. It is hard to go back once you’ve had a taste.