3

In most discussions of OOP it's said that the advantage is re-usability.. You put in some extra work to define your classes, and it saves you time later in being able to create many instances and extensions of those objects.

A corrolary of this seems to be that you shouldn't switch from procedural to OOP programming until the tradeoff of writing up everything into objects is equivelant to the time you'll save.

In general, when is a good time to switch from procedural to OOP programming? Are there any signs/characteristics you generally look for to know your project needs to make that switch?

6
  • 2
    OOP is not "the only way", you can still have well-organised reusable code which is not OOP. But indeed OOP is kind of natural and makes things easier.
    – Kos
    Dec 13, 2010 at 22:36
  • PHP is a hybrid language. If you build applications procedural only or all-OOP, then you are using it wrong and very likely generate substandard APIs.
    – mario
    Dec 13, 2010 at 22:54
  • @mario: that's interesting. Is there any reading material on that available?
    – Jonah
    Dec 13, 2010 at 23:19
  • @JonahBron: There's an objectified article here: devzone.zend.com/article/1236#Heading20 - and SO has a few interesting dupes on the issue stackoverflow.com/questions/1530868/… - IIRC there was also a good one on programmers SE
    – mario
    Dec 13, 2010 at 23:46
  • 1
    @Tortoise :) My views on OOP have changed significantly since then; nowadays I consider OOP a way of reasoning about programs rather than of writing them.
    – Kos
    Apr 30, 2013 at 6:13

7 Answers 7

4

I'm assuming this question is from the standpoint/paradigm of being a beginner. Once a programmer has experience writing object-oriented code, you can certainly author a project from the beginning using this architecture. In fact, I'd argue that a top-down approach can save you huge amounts of time on larger projects.

For the bottom-up scenario you outline, though, I'd say you'd have to feel it out. Reference this wikipedia article for more information about the different approaches, generically speaking.

Specific to PHP, I'd say you could use this approach for a migration:

  1. Take as much code as you can (ie: related functions) and place them into include files.
  2. Create a container class for that file. You can start with just using all the functions by calling them in a static manner, or even using a static (singleton) class.
  3. Gradually convert to an instance paradigm instead of the global data / static function one that is the badness of procedural programming.

This process is a great way to learn the ins and outs of OO, and in the end you will see the benefits. It will also teach you my initial point: that it takes a lot longer to convert something into OO than it does to start with a semblance of good (high-order) design from the beginning.

3
  • I'm not sure I agree with you. I've seen to many wannabe-OO code created the exact way you described it. Unfortunately, it doesn't make one understand the main principles behind OOP. The main one being to understand what an object is, what it can do, what it can't/shouldn't do, etc. There is way more to OO than code reuse. Also, I don't agree that procedural programming is bad. Every OO program also has procedural code somewhere. Most of the times, OO depends on procedural code to work (i.e.: PHP is coded in C, so is Java).
    – netcoder
    Dec 13, 2010 at 23:09
  • I've worked on one project like that, as well.. it was more a matter that it wasn't adequately transformed than that it wasn't headed in the right direction. GOTO isn't "always" bad, either - so say some. Procedural programming may have a place, I'd accept that. Neither have yet to find a way into the projects I lead, though :)
    – zanlok
    Dec 13, 2010 at 23:21
  • p.s. doing more C# lately than anything else, and after the advantages of a strongly typed system are becoming apparent, and the larger-architecture model as well, most would probably agree OO became the wave of the future for a reason?
    – zanlok
    Dec 13, 2010 at 23:24
3

If it's not a very very simple application, now is the time. In fact, it's arguable that you should always program OOly, because it will be harder when you want to extend your program in the future.

1
  • 2
    Gotta agree with this. If you're just dealing with 10 line batch scripts or the equivalent, sure, just use a few lines of procedural code and get the job done, but for anything that you intend on using for a while, that others are likely to have to maintain, or that's of any significant size (anything more than 100 or so lines of code), I'd say you're better off investing in oop.
    – DarinH
    Dec 13, 2010 at 22:38
1

I think it depends on the context. For graphics applications using an existing OOP framework, the tradeoff is instantaneous -- you'd have to go out of your way to write procedural GUI code in some contexts.

However, if you're doing raw data processing and not interoperating with any OOP framework, maybe you'd find that OOP never makes sense.

1

It may be very time-consuming to switch to OOP withing a project. I doubt it would be profitable, because it requires a lot of coding, a hell of a lot of testing, and then a LOT of refactoring. The whole concept of OOP is different from PP.

So I would recommend not to switch within the project, but start using OOP for new projects as soon as possible. When you feel comfortable, you can start thinking of an OOP design for your existing project(s) and gradually implement features in OOP. It will be a lot of work, though, and it will probably feel like rewriting the entire project.

0

I would look elsewhere for signs you need to switch. For all the hype--and I'm a big supporter of OOP--code reuse is often only marginally better with OOP languages.

OOP is simple another tool to help organize your code, like functions in the past. It's a great and useful tool. But the main benefits are making it easier to write and maintain your code.

If it were me and moving to OOP required almost a complete rewrite, I'd hold off until some more material benefits of the switch became apparent. If your code works, I don't know why you'd rewrite it.

2
  • 2
    "code reuse is often only marginally better with OOP languages"; My perception is that it make it very easy. Especially moving libraries between frameworks.
    – Jonah
    Dec 13, 2010 at 23:18
  • Yes, it certainly can make it easier. It also provides tools for extending existing code. It's just that, in practice, I don't really see people reusing more code. I've worked with a lot of people (recently) who have huge libraries that they reuse without any OOP. It's just an observation. Your mileage may vary. Dec 14, 2010 at 0:10
0

It depends on the task, but having done both, here's what I would think about:

  1. do you feel the work requires modularity? the ability to manage similar or dissimilar things from a central place? are there many repeating elements? will quick development or administration changes be important?

  2. do you feel the problem you are attacking is predictable and repetitive? is the task best served by following steps to solve or by applying algorithms?

If more like 1, then go for OOP, otherwise if it's more like 2, then go for a procedure approach.

When in doubt, use what you're comfortable with.

0

It is rarely a good thing to change the programming style of an ongoing project.

You can always apply OO principles to procedural code if you want more clear-cut responsabilities among your entities.

check for instance this very interesting book on OO coding in ANSI-C

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.