tags:

views:

362

answers:

6

There is a project written in PHP that is just simply all procedural ... step by step calling DB functions, processing, and printing out the output. And then it is changed to totally object oriented -- there is a singleton for the App object, and various functions are invoked through the App object.

Someone claims that the memory usage or footprint on the server will be less. Would that be the case? I thought procedural often just uses the bare minimal, while Object oriented programming with various design patterns usually instantiates more things than needed, or simply have objects around while procedural usually just have the bare minimal.

So will changing all code to object oriented actually make the memory usage on the server smaller?

+6  A: 

It will probably make it more, but there's really no way to tell. If your code actually improves by doing it in an OOP way, then it may be less. There is no direct correlation between memory used and the presence of object oriented ness. Maybe in general, object oriented takes more memory, but only if both sets of code are written equally well, and that's almost never the case.

Is there a reason this application is being upgraded to be objected oriented? You know it's not one or the other, you can mix and match pieces... OOP is not a silver bullet.

Mark Canlas
+2  A: 

Your question sounds like this to me: We have a unmaintainable code-base, will rewriting it in Java make it faster?

First, you need to identify the problem: is your code unreadable and/or difficult to maintain or is your program's runtime footprint too big? These are two completely different problems.

If you have a code base which is difficult to maintain (it sounds like you do), you'll not solve that by rewriting the code using another paradigm. Why is the code unreadable? Are the developers writing unreadable code, then rewriting it in Java or C# will just give you object oriented unreadable code. In that case you need to improve the skills of your developers (hire better ones, reeducate the existing ones, etc.).

If you have a problem with memory footprint, then you should approach that as any other performance problem. First measure, then do some optimization (rewrite some piece of code to make it more memory efficient), and then measure again. Measuring before and after is important to make sure that you're not making things worse. And believe me, even very good coders do that mistake.

JesperE
Actually, I'd say that while you can of course fuck up no matter what you use, object oriented code is relatively easier to keep maintainable, pretty much at all skill levels, mainly by reducing the need for global variables and/or huge method signatures.
Michael Borgwardt
+1  A: 

The answer is both yes and no.

By switching to a more object oriented architecture you could reduce the memory footprint, but if and only if the code is more efficient.

Straight PHP without any function calls or object calls is rather quick. As soon as you start making function calls or object calls things start to go much slower.

From A Python Vs Ruby Vs. Python Benchmark an OO strategy for doing an incremental loop took 3.7079 seconds, function oriented took 4.3501 seconds, and using neither took 0.6164 seconds. Doing a simple "Hello World" app with OO vs. Procedural Vs. Neither yeilded a 4.1248 seconds vs. 3.7656 vs 0.9309 seconds.

So depending on what your code is doing and how it's doing it, objects could be both faster or slower, more memory intensive or less memory intensive, or neither.

Zachary Spencer
+1  A: 

And then it is changed to totally object oriented -- there is a singleton for the App object, and various functions are invoked through the App object.

That actually sounds like pretty much the opposite of "totally object oriented". This indicates a level of understanding of what "object oriented" means that will most likely NOT result in a reduced memory footprint.

In general, the design will have a far greater influence on the memory footprint than the programming paradigm, though it is probably true that on average, object oriented apps tend to have a bigger footprint.

Michael Borgwardt
+1  A: 

Memory usage isn't the advantage of OO. The advantage of OO is that the code will become more maintainable. If you're not having performance issues there's no need to go looking for ways to be more efficient.

Benedict Cohen
+1  A: 

In addition to the other comments, I would like to say that OO helps reduce redundancy... which might make your code more efficient, and reduce memory footprint. Of course, OO has more overhead, but should work out more efficient in larger projects.

Antony Carthy