Page 1 of 1
OOP - Why use it?
Posted: Wed Jun 25, 2008 2:50 pm
by goodwinpro
In reading through tutorials and on-line training guides, I have slowly come to understand basically how OOP works and how to create classes, functions, objects, etc, and how they work together.
My problem is "why?". (And I really do want to know - I'm not just being a jerk) All the tutorials I've read give non-related examples. Like "here's a car"... "now lets give it wheels"... "now lets make a NEW car RED". I've seen tutorials about bears, TVs, cars, houses, people... but I don't think I've seen even one example of something that's actually useful like building a simple contact form.
So how does OOP benefit your average ordinary 5 to 10 page informational PHP website? Or does it? I can totally see how OOP would be necessary for video games, banks, or booking airline tickets, etc. But what about smaller applications?
I know it must be important or so many people wouldn't be so excited about it.
So what are some things you can build with OOP that would be useful for basic websites.
Re: OOP - Why use it?
Posted: Wed Jun 25, 2008 3:05 pm
by Eran
OOP does not benefit the website, it benefits the developer. The benefits are more structured design which leads to greater maintainability, increased code re-use through de-coupling and black-box approach and some useful language features such as inheritance, polymorphism and better scoping.
The major detraction of OOP is that it takes a while to learn to use it effectively. Its sometimes hard for programmers to find justification for going through the growing pains when they don't see immediate results.
If you are throughly familiar with the approach, it can be used effectively for any project scope.
Re: OOP - Why use it?
Posted: Wed Jun 25, 2008 9:58 pm
by Christopher
goodwinpro wrote:In reading through tutorials and on-line training guides, I have slowly come to understand basically how OOP works and how to create classes, functions, objects, etc, and how they work together.
Your statement, combined with your question, expresses one of the main problems with programming. You have learned about OOP and claim to basically understand it, but you don't know why you would use OOP. The simple answer is that you don't really "understand basically how OOP works" or you wouldn't ask the question. What you probably know is how to do Procedural programming using he class construct.
The problem with programming is that the intuitive way to program, which can take you a long ways, is Procedural. Programmers usually add Structured Programming on top of that when they hit certain problems -- and though not so intuitive it is a short leap understand and see the benefits. Unfortunately OOP is simply counter-intuitive. Like calculus and quantum mechanics, you and I would never come up with it. But a few very smart people did and we benefit from their ideas.
goodwinpro wrote:My problem is "why?". (And I really do want to know - I'm not just being a jerk) All the tutorials I've read give non-related examples. Like "here's a car"... "now lets give it wheels"... "now lets make a NEW car RED". I've seen tutorials about bears, TVs, cars, houses, people... but I don't think I've seen even one example of something that's actually useful like building a simple contact form.
Oh Great! Another jerk who doesn't get the Car object example!!!
goodwinpro wrote:So how does OOP benefit your average ordinary 5 to 10 page informational PHP website? Or does it? I can totally see how OOP would be necessary for video games, banks, or booking airline tickets, etc. But what about smaller applications?
I know it must be important or so many people wouldn't be so excited about it.
So what are some things you can build with OOP that would be useful for basic websites.
The fundamental difference between Procedural and OOP is that with Procedural you start with code and give it data to work, with OOP you start with data and give it code to work. It may seem a simple, or even meaningless, difference, but from that idea an amazing cascade of concepts flow. Probably the biggest is that you can easily pass code around, but there are a huge number of other things that grow out of the basic concept of OOP. There are new concepts like polymorphism that lead to some completely new practices. And there are things like unit testing where OOP simply takes it to a new level.
Thinking of data first is probably the most advanced way to design and develop software. Methodologies like Test Driven development and Domain Driven design are two of the best examples of this. I just give myself time to understand OOP concepts that I don't get. And I accept that there is a better way to program than what I am currently doing, so I keep searching...
Re: OOP - Why use it?
Posted: Wed Jun 25, 2008 10:13 pm
by Zoxive
Another thing is, spaghetti code.. With procedural pretty much all of the code is all jammed together and is very easy to get lost of what parts of code do what. (Quickly glancing) As already stated it helps the developer, and for me it helps me big time.
Also those code "examples" you are talking about, with the cars and etc.. Although they are not real world examples they help show you how the code is reusable, and most of time how you can extend the classes, and how to have the classes work together.
Re: OOP - Why use it?
Posted: Wed Jun 25, 2008 10:44 pm
by andre_c
Another big reason for me is modeling. It allows you to express business rules, actions, states, etc in a way that mirrors the real system that your working with. As an example, on a project management application, you would have objects such as Project, Client, Feature, Task, etc which would relate in code in a similar to how they relate in real life. The code that expresses their relationships and interactions can now be referenced all over the system. And since it resembles the domain, it is easier to understand, follow, and expand.
Keep in mind that the more a website strays from "basic", the more it benefits from well implemented object-oriented code.
Re: OOP - Why use it?
Posted: Wed Jun 25, 2008 10:48 pm
by WebbieDave
goodwinpro wrote:So how does OOP benefit your average ordinary 5 to 10 page informational PHP website?
It very well may not. OOP is not the most appropriate tool for every job.
Re: OOP - Why use it?
Posted: Thu Jun 26, 2008 11:49 am
by Kieran Huggins
I didn't see the appeal of OOP at first either. I think everyone uses it (improperly) at first and wonders "why all the bother".
As with anything, there's a time and a place for OOP. As you use it more I suspect you'll begin to appreciate the simplicity of it all.
That "lightbulb" moment is something you have to look forward to - stick it out, it's worth it!