Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMayur Pande
Courses Plus Student 11,711 PointsWhen to use OOP over procedural coding
How would I distinguish between using OOP or procedural coding? I am currently starting this course as I want to start building MVC pattern sites and eventually as Alena Holligan said contribute to open source software.
But I am confused as when to use OOP over procedural code. Will this become apparent to me over time, or is it to do with the task given?
3 Answers
Alena Holligan
Treehouse TeacherObject-Oriented Programming
Commonly used because it allows developers to work on the same project without colliding with one another as easily. The video covered many of the advantages of OOP already, but let me outline
Advantages include:
- Modular: Provides a clear modular structure for programs which makes it good for defining abstract datatypes where implementation details are hidden and the unit has a clearly defined interface.
- Scaleable: Adding developers to a project often easier because the don't have to understand the entire code base, just the section they're working on. Adding Hardware resources can be more cost efective because you can have different resources for each modular piece.
- Maintainable: Makes it easy to maintain and modify existing code as new objects can be created with small differences to existing ones.
- Extensible: Provides a good framework for extending a project through libraries where these components can be easily adapted and modified by the programmer. This is particularly useful for developing graphical user interfaces, or GUIs.
- Reusable: Each module works independently of the surrounding module. This allows you take take one section, such as the user login, and use it for other projects.
Disadvantages include:
- The real world refuses to divide up into neat classes and subclasses.
- Sometimes several objects will interact in complex ways - maybe even ways we didn't necessarily anticipate when writing the program.
- Can require more code and be more complicated for smaller projects or project in which there are very few repeated tasks.
- Decreased performance. This is one of the most heated debates. Although a well designed procedural site CAN slightly out perform a well designed object-oriented site, there are many factors to consider and this should not be your main concern.
OOP is often the best use when:
- You have multiple programmers who donโt need to understand each component.
- There is a lot of code that could be shared and reused.
- The project is anticipated to change often and be added to over time.
- Different sections can benefit from different resources like datasource or hardware.
You probably don't what to use it when you have several developers who need to share implementation specifics, for example if you were writing a keyboard driver, you wouldn't want to break it into fragments which would hide implementation specifics from a developer working on the driver.
Procedural Programming
Revolves around keeping code as concise as possible, and focuses on a very specific end result.
Advantages include:
- It is written in a step-by-step function, smaller programs written this way are very easy to follow.
- Easy to maintain, as each procedure or function can be debugged in isolation
- Since it is written for a very specific purpose the code often gets you extremely efficient and high-performance applications.
- Code can be more flexible as you can change a specific procedure that gets implemented across the program
Disadvantages include:
- Procedural coding tends to get very difficult to maintain the larger the code gets.
- As programs grow in size, the temptation arises to start using more and more global variable. This can introduce sometimes-devastating results, particularly in a large program worked on by multiple programmers.
- Portions of the code are so interdependent that the code in one application will not be useable in another, meaning despite being somewhat similar the code for one program will not able to be carried to a new one, which OOP can do.
- Procedural code is difficult to relate with real world objects.
- Modifying one part of the code requires modification of the entire code
- As code grows, it gets harder to understand and modify
- Hard to apply code from one program to another
Procedural Programming is often the best choice when:
- There is a complex operation which includes dependencies between operations and a need for clear visibility of different application states ('SQL loading', 'SQL loaded', 'Network online', 'No audio hardware', etc). This is usually appropriate for application startup and shutdown.
- The program is very unique and few elements were shared.
- The program is static and not expected to change much over time.
- None or only a few features are expected to be added to the project over time.
You probably don't what to use it when there are many simple independent tasks to perform or to manage user interface.
Summary
In short, when it comes to short programs, Procedural Programming can be faster and easier both to maintain and to read.
When it comes to large and complex modular projects, being worked on by multiple programmers or even teams, using OOP lets you compartmentalize and organize, again making it faster and easier both to maintain and to read.
PS: There is also the debate about whether a program is object-oriented just because it uses objects... @ircmaxell has a good post
Simon Coates
28,694 PointsPHP was a late arrival to OOP and as such the community still has a tolerance for procedural code. In PHP, you get strong opinions both ways and there's still a lot of procedural code around. For small scripts, it may not matter much as OOP is meant to deal with complexity, and you may not have anything that jumps out as an object. For more discussion see here, here and here
Jeremy Castanza
12,081 PointsTo answer your question, it becomes apparent over time. You'll probably start off learning procedural because the learning curve is easier. Starting out programming, it's much easier to think in programming in terms of Step 1, Step 2, etc. As your application grows though, it's that point where you're writing Step 52, you realize that it would be a lot easier if you weren't having to rewrite stuff that you've already written and could just call a module, class, object, etc. And/or if you make changes and you're having to do a find/replace on your various files rather than updating a single line.
Mayur Pande
Courses Plus Student 11,711 PointsMayur Pande
Courses Plus Student 11,711 PointsThanks, very thorough explanation!
As I said in my original question I want to start building MVC pattern coding from now on.
However the sites I am building at the moment I guess are pretty trivial so I doubt they would benefit from OOP.
Do you have any suggestions on things I could work on to practice my OOP skills?
Alena Holligan
Treehouse TeacherAlena Holligan
Treehouse TeacherYou CAN use OOP with any site, and practicing is a perfectly valid reason to choose that style for your project :)