![]() |
Why OOP?
The killer question. It is a question I have asked myself for years. I started out programming in PHP, then I moved to Python. I've been doing quite well for myself with Python for a number of years, even to the point where I am teaching it in my Applied Programming class at the college where I teach. However, I've never been able to get a full grasp on OOP, and I keep going back to the question of why it matters?
I think I have already heard all of the answers, but what I do works fine procedurally. What I would really like to see is a real world example of what makes OOP better. I've scoured the internet for such an example, but on all of the pro-OOP hits I just get the same list of why I should use it. Is there someone out there who has written the same program procedurally and OO, highlighting the differences between the two - and the pros and cons? I really don't want to teach my students OOP, firstly if I do not fully understand it, and if I am not fully behind it. Thanks, Nisim |
This might sound harsh, but if you don't fully understand OOP, then maybe you should not be teaching a programming class. If it's a very basic programming class then maybe you don't need to touch on OOP, but if anything over the very basics it really needs to be taught. There are many, many advantages to writing programs in OOP and I am sure you have heard them all. But to fully understand why, you really need to work on a big project. If you are only writing really small applications a lot of the advantages for OOP to present them self, but once you get into huge projects they quickly become visible. Start writing a fairly big application that involves multiple tiers and modules and you will quickly see why OOP is so popular.
EDIT: I didn't really realize this was under the scripting forum. Most of the scripts I write are just for simple tasks where OOP would be pointless. All my statements above were more towards application programming and not really scripting (which is not really my area of expertise). |
I understand what you are saying. It did not come across harshly. The course is Intro to Programming, the college just calls it Applied Programming, so I do not need to teach OOP. However, I've been considering introducing it near the end, but I want to understand it's purpose before I tell my students about it. Like I've said, I have been coding procedurally for years, and there are industry professionals who have never touched OOP. I actually like the concept of OOP, but I have yet to find a real world example of it, at least for the types of projects I've been doing.
For example, the project my students are doing currently is a text adventure game, which can be done completely with functions. Is this something that could be done more efficiently with OOP? If so, how and why? Like I said, I'm mainly trying to grasp its use myself. If the college comes and asks me to teach a higher level programming class, I would have to turn them down unless I began to grasp this. If they forced me into it, I would probably teach it with a language that is OO from the ground up, like Java or Ruby. But then it would turn into one of those learn by teaching type of things. |
OK, the text adventure game, NPCs can be represented by objects of different classes.
Basically yes you could do it procedurally, but if later on you wanted to expand the games o it would use a graphical interface you could just expand on the classes you already wrote and wouldn't have to start from scratch. |
There's a good reason your lawnmower is made of objects. There's a good reason you mow your lawn procedurally. Perhaps you should do some independent thinking along those lines.
|
Well look at it this way, in your adventure game you will have characters, how are you going to represent these characters, how are you going to represent/store the attributes and behaviour of these characters.
You can put all this into a class and then create an instance of that class as each person is an instance Human class, now you might want to classify the characters into man and woman, so you use your existing Human class and just add new attributes to it. Now if your students have a similar project in the future that involves person they can re-use this same class. I’m just in my second week of my OOP module so there’s properly more to it than that. |
The primary use of OOP I would think would be abstraction and code reuse. Having a self contained and tested object to use in other projects is better than building it from the ground up each time.
The use of OOP should be dictated by the project itself, common sense will tell you when to use it vs using a procedural approach. An analogy is in order... A car is made up of several objects: engine, tires, etc... those objects were predefined by their creator. Do you care what the engine consists of, do you care what materials compose the tire? As long as it cranks and gets you where you are going, does any of the underlying objects matter? More than likely not. Thanks to the concept of abstraction, you are able to effortlessly go to the store and pick up groceries. You don't have to tweak this or that, or check this subsystem or connect wires... you turn the key on the ignition and drive away. Good thing the car object was predefined... if you had build it from the ground up everytime you wanted to go to the store for groceries, you just may starve before you get it off the ground when fixing the same problems that you had before. Reuse of a tried and tested object prevents you from spinning your wheels on past issues. A car dealership, could extend the existing car object with prices, colors, quantity, miles per gallon, etc... for use in their dealership management program. Technically, anything you do in OOP can be done procedurally. It matters 3 years from now when you are tasked with modifying that program because the sales prices aren't being adjusted correctly. Well, you can rule out the car object and all of the other non-related objects and focus on the finanical portion. If the financial transaction object was tried and tested, you can rule it out and just focus on the bit of code that interfaces with that object. If written procedurally, you would be sitting their with a mound of functions to trip through while trying to remember how they all played a role in the overall program. Well, I've rambled enough... hope that made sense. |
There's a link to a little blurb, "Why OOP? My View", in my signature.
|
because.... why not ?
|
I know this is an old thread. You can look at a class as just another way to combine things that belong together. There are additional benefits like inheritance, overloading, and instance management. Python, in contrast to Java or C#, allows you to go partial or all the way with OOP.
|
| All times are GMT -5. The time now is 4:02 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC