[jihad] oop vs procedural

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

McGruff wrote:It's a great thing about php that you can jump right in and start learning to code but programming well means using the most powerful tools available: OOP and unit testing.
"Powerful" is an opinion, and you have repeated that mantra WAY too many times to have an open mind to people of the opposite opinion.

Personally, I think unit testing is about 300x more powerful/useful than OOP - and no, one does not mandate the other.
McGruff wrote: Once you get a few steps beyond embedded echo calls, you have to start thinking about script design.
The arrogance here is strong - the implication is that I neither have experience, nor have I done any design. On the contrary, I've programmed for over a decade, and have coded in PHP for 3 years. I've maintained over 100,000 lines of code in that time, deployed on hundreds of sites, serving tens of thousands of users - so I would say that I've gone more than a few steps beyond embedded echo calls.

The sad part is that you cant see that someone with knowledge, intelligence and experience could have a different opinion about OOP.
McGruff wrote: If programming in the global scope is one-dimensional, using functions adds a second dimension and OOP is 3D.
If close-minded opinions are one-dimensional, being open to people having different opinions is the ninth-dimensional string theory.

OOP can be useful (or a hinderance).
OOP can make things easier (or harder!) to maintain.
OOP can make things faster (or slower!) depending on the programmer.
OOP, like procedural code, can be anything from horrible to fantastic.

Its very nature does not bestow upon it the magical world-solving abilities it would need to have to justify your proselytizing it repeatedly.
McGruff wrote: I've often heard people say that using functions / structural programming is an alternative to OOP but, unless you severely restrict the problems under consideration to very simple tasks, I don't think this holds up.
My experience says otherwise, as does millions of applications (that still run) that were written before OOP was a popular concept.
McGruff wrote: The big problem for budding php-ers is that, at first glance, OOP looks unecessarily complicated and "inefficient".
Careful choice of terms there. My opinion of it is a bit different: "OOP is complicated - until you understand it, and then its a little more complicated than procedural code".
McGruff wrote: There's also quite a steep learning curve to get over before you can begin to appreciate why this is not true.
To be clear - that which requires understanding is by definition more complicated.
McGruff wrote: It's therefore necessary to make a point of debunking the usual OOP canards when they surface and to encourage people to make the step up.
And much like a hammer doesnt do much good for a screw, it is deeply disgusting to hear it parroted as the swiss army knife solution for every problem.

All programmers will, on a long enough timeline, run into OOP. Pushing them headlong into the fray early is the equivalent of saying "Vi" is a great text editor for someone that used WORD.
McGruff wrote: Bad advice impedes people's learning process; good advice helps them move forward.
Telling someone to learn the most complex portions of PHP on a board that is generally populated with less-experienced users is the equivalent of taking a boy that cant swim and telling him to swim the atlantic - "Trust me, when you are done, you'll be able to swim ANYWHERE!".

True enough comment.. If you can survive the swim.
McGruff wrote: This forum is a place to learn how to program well and I'd be selling users short if I didn't do this.
And again I say - you can program effectively, robustly, powerfully, and do it without a LICK of OOP - Computer science did so for *decades* without a problem. PHP did it for several years. Its not a requirement, nor is it even a causal relationship - the mere use of OOP does not improve a program. The *proper* use of OOP *can* - just like procedural code.
McGruff wrote: Incidentally, a big part of the learning hurdle which new coders sometimes miss is that OOP talks more to the programmer rather than to the machine.
I completely disagree with this, and we've butted heads about it before. OOP is *NOT* written for economy of expression. It is written to be a well-designed expression of what you want.

The difference between "Meet me for dinner", and a 10 line sonnet about it.

While the sonnet may be easier for YOU to read, and certainly easier to expand into a novel, it is *not* easier for "most programmers" to read - because most programmers are new, and havent learned OOP.

Further, if POORLY written, that OOP can be a hundred times more complex to understand than procedural code.

Abstraction requires more understanding - its more complex. Of course, much like Sonnets can contain nuances impossible to express in a text message, OOP (if done right) can in fact be extremely elegant and concise.
McGruff wrote: It doesn't really do anything you couldn't do by other means but it does make code easier to read and maintain.
I can show you OOP code that would curl your toes, and procedural code that someone with no programming experience could immediately understand.

Like you said: OOP doesnt really do anything you couldn't do by other means! (You should have just stopped there!)
McGruff wrote: With OOP, you can create entities with rich meaning: abstracting archetypal concepts, encapsulating chunks of related code into discrete, swappable modules, or create a common language between programmer and client.
Sonnets instead of text messages! Ahhh the richness, the depth, the.. lack of simplicity.
McGruff wrote: The article/rant we've been talking about may have been intended to deal with databases but in fact goes well beyond this to make a rather ridiculous attack on OOP.
While many of his attacks - plural - are beyond the pale, some of them have a very solid level of accuracy.
McGruff wrote: The guy is talking out his as* there and I think it's important to say so. OOP programmers will sigh and think "here we go again". Do we make our own blanket statements? Well yes because, to the best of our knowledge, the earth is round, not flat.
Ah, but in truth - its neither - its Ovaloid. You'd do well not to see the world in black and white, much like the flat-earth-ists.

OOP (and all PHP code for that matter) depends on the programmer, the problem set, and the goals.
McGruff wrote: Perhaps we should set up a coding challenge sometime so that we can compare alternative solutions to some defined problems? At some point, we have to start talking real code.
Feel free, and have fun - I have enough experience under my belt that I feel no need to prove myself. You feel that OOP *is* "the most powerful part of PHP". I've experienced that, and also experienced the opposite.

I'd just really prefer not to hear a non-stop, dead-horse-beating OOP fanclub message in practically every thread.
McGruff wrote: PS: I should apologise to the original poster since it was me who sparked off the OOP "jihaad". I can split this off to a new topic if you'd prefer to get back to discussing your own class designs.
Thats probably an excellent idea.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

When I said:

"Once you get a few steps beyond embedded echo calls, you have to start thinking about script design"

.. I wasn't commenting about your abilities in any way. Please don't take offence. I was simply expressing my belief that the benefits of OOP kick in very early as you move upwards from the simplest of coding problems.

At some point, these OOP debates become a bit of a pantomime "oh no it isn't!" "oh yes it is!" exchange and the only way to resolve that is to start talking real code, ie to compare the advantages and disadvantages of different solutions to a problem. It doesn't help to get irritated when people express contradictory ideas. It's better to explore the differences on their technical merits and see where we get to, leaving a record for other users who might learn something from the topic. In this way, we can try to move the discussion forwards. I could be plain wrong but I can't accept accusations of arrogance, or of having a closed mind.

Debate is healthy. Proposing a theory and exposing it to peer review is how our knowledge advances. That inevitably involves "conflict" but it's an intellectual conflict not a personal one. Bottom line is you can say what you want but you have to be able to back it up, if challenged. In truth, the OOP "theory" was validated long ago.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Roja, I would recommend you not waste your time on this. You are promoting a practical approach to solving programming problems using the many good programming practices available. It's funny because I think you (and I) are what would be constdered in PHP to be OOP programmers. But ulitmately you are trying to communicate an attitude. And that attitude will never be accepted by those who consider OOP to be _The Way_ rather than simply another good programming practice.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

McGruff wrote: .. I wasn't commenting about your abilities in any way. Please don't take offence. I was simply expressing my belief that the benefits of OOP kick in very early as you move upwards from the simplest of coding problems.
And my coding history, whether the intended target of the statement or not, has the opposite belief and experience.

You defined OOP as complex, and I agree. Its not easy to learn, and it can be a serious impediment to other goals.
McGruff wrote: At some point, these OOP debates become a bit of a pantomime "oh no it isn't!" "oh yes it is!" exchange and the only way to resolve that is to start talking real code, ie to compare the advantages and disadvantages of different solutions to a problem.
Ah, but thats just it - I've done so already in my experience, and don't need to resolve anything! I find advantages to OOP. I also find advantages to Procedural. The one place I see the largest advantage is the exact point you've already conceded - that its not simple, obvious, or even reasonably understandable for the new programmer.

There are two positions here - OOP is always good, and OOP can be good or bad. That you feel you can prove your opinion true is amusing - it is an opinion, which cannot be proven.

At best, you can hope to show specific examples which highlight your point of view, and hope to disprove the opposite. Doing both still would not prove that OOP is always good, and disprove that OOP can be good or bad - it would simply show that you could show specific examples of each.
McGruff wrote: It doesn't help to get irritated when people express contradictory ideas.
Expressing contradictory ideas is one thing. The constant repeated refrain with few objections gets tiring - and worse, the few times I've spoken up in opposition, you've taken the time to argue each point.

Thats not a community of discussion - thats an organized pattern of harrassment for people of a viewpoint different from yours.

I'll leave it to you to understand which is likely to irritate.
McGruff wrote: It's better to explore the differences on their technical merits and see where we get to, leaving a record for other users who might learn something from the topic.
The web is littered with dozens of such efforts. An excellent 20 page response to a premiere book on OOP, complete with code and examples can be found here: http://www.geocities.com/tablizer/meyer1.htm

I dont bring it up to belabor the point, or to back my opinion - its a simple example of the many already-accomplished discussions and rebuttals to OOP-as-perfection that are also numerous.

I find no need to repeat what greater minds have already taken on and accomplished, especially when I've already done so in my experience as well - I continue to maintain multiple apps, some heavily oop, some heavily procedural. In every case, in my programming experience, there has been good OOP code, bad OOP code, good procedural code, and bad procedural code.
McGruff wrote: In this way, we can try to move the discussion forwards.
Evidence and argument are ways to prove *fact*. Here we have a difference of *opinion*. Opinion cannot be proven.
McGruff wrote: I could be plain wrong but I can't accept accusations of arrogance, or of having a closed mind.
If you have an open mind, cite examples where you've posted that procedural code would be superior to OOP. I've seen no such dichotomy or flexibility, and in fact just the opposite - a stubborn (and vocal) resistance to anyone posting commentary questioning the perfection of OOP.
McGruff wrote: Debate is healthy.
In this thread, I've repeatedly made both concessions and clarifications showing some level of agreement with you - you havent done the same. I have considerable debate experience - and these forums up until now have not encouraged healthy debate.
McGruff wrote: Bottom line is you can say what you want but you have to be able to back it up, if challenged.
Here's the difference - you repeat your position, and your offer is to argue with those who disagree.

I repeat my position, state the points I agree with you on, offer a suggestion on how to be more open-minded, and you offer to argue with me in code, instead of words.

Which is reasonable?
McGruff wrote:In truth, the OOP "theory" was validated long ago.
OOP was validated as a useful concept - which I've never disagreed with.

Your continued argument that "anything over 100 lines" automatically will become better with the use of OOP is far from validated - in truth.

I'll quote Alexander Viro - kernel hacker, incredibly experienced programmer, and far greater mind than mine, who seems to have reached the same conclusion:

"OOP is a nice tool. However, it's a tool that has incredible potential of
shooting one's foot"

Mixed bag, indeed.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

This is not what I call an intelligent, polite discussion - and not for the first time. Your claims about harassment are ridiculous. This is an advanced forum after all and a topic which was started to discuss class design. If you can't respect opinions which differ from your own or respond in a reasonable manner please don't post here.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

arborint wrote:Roja, I would recommend you not waste your time on this. You are promoting a practical approach to solving programming problems using the many good programming practices available. It's funny because I think you (and I) are what would be constdered in PHP to be OOP programmers. But ulitmately you are trying to communicate an attitude. And that attitude will never be accepted by those who consider OOP to be _The Way_ rather than simply another good programming practice.
It appears you are correct - Thanks for the advice, I will do so.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Right. Here I come in as a bystander with the intend to moderate.
McGruff wrote:This is not what I call an intelligent, polite discussion - and not for the first time. Your claims about harassment are ridiculous. This is an advanced forum after all and a topic which was started to discuss class design. If you can't respect opinions which differ from your own or respond in a reasonable manner please don't post here.
I think you've taken the Roja's mentioning of "harrassment" too personal.

What my understanding of Roja's point is, is that he simply says: "OOP is one of two ways." and asks you to acknowledge that.
Your position, McG, is "OOP is the only way after a certain threshold of complexity."

You position, McG, is much more "evangelical" than Roja's who sees benefits in both sides. I fail to understand how you could reach the conclusion that this was not an intelligent discussion. We all know that sometimes forum discussions can become a wee bit personal. That changes nothing about the fact that this discussion is straight to the point and intelligent.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

patrikG wrote: You position, McG, is much more "evangelical" than Roja's who sees benefits in both sides. I fail to understand how you could reach the conclusion that this was not an intelligent discussion. We all know that sometimes forum discussions can become a wee bit personal. That changes nothing about the fact that this discussion is straight to the point and intelligent.
Thank you patrikG.. you expressed it far better than I did.

Thats what my intention was, and what my phrasing was meant to convey. If I failed, I apologize. I agree 100% with patrikG's summary - its far better than my attempts. I'll leave this discussion, as I think the salient points have been made, and to avoid further miscommunication.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

What we have here is trolling, pure and simple.

I initially tried to placate Roja and have explained what is an acceptable pattern of discussion ie you can say what you like but you must respect contradictory views and you must expect to be asked to back up your case if challenged. Exploring differences in a polite, intelligent discussion is good; exploring inflated egos is a bore.

Insulting people who dare to differ is not acceptable. I'm referring specifically to accusations made by Roja about arrogance, closed minds, harassment, and comments that he is "deeply disgusted". A vigorous intellectual debate is the wrong place to be if you are disgusted by other people's views.

Likewise, a stream of pejorative remarks about OOP fanatics defending a cultish religion is extremely rude and inflamatory. This is not the first time I've had to deal with this from Roja. We're all human: losing the rag once - and hopefully apologising - is forgivable but repeated behaviour of this kind amounts to trolling - particularly when, by ressurecting an old argument best left alone, Roja has shown little aptitude for resolving conflict but rather a taste for prolonging it.

When I logged on tonight I expected to be faced with yet another lengthy rant and possibly a decision to suspend posting privileges. That is a formal warning.

Patrick: evangelical? This is what I said:

"IMO, if you want to program well that means learning to use the most powerful tools available: OOP and unit testing.

"Once you get a few steps beyond embedded echo calls, you have to start thinking about script design.

"...the benefits of OOP kick in very early as you move upwards from the simplest of coding problems

That is a valid opinion, moderately expressed. If we can put Roja's persecution complex to one side for a moment, I did not in fact say that OOP is the only way. If you read back you'll see that I did say you can achieve the same ends with any old tat: the computer doesn't care. What I am saying is that, beyond a certain level of complexity, OOP is best practice and that the benefits relate to the programmer not the machine.
jl
Forum Commoner
Posts: 53
Joined: Tue Nov 09, 2004 12:05 am

Post by jl »

I'm quite interested in this debate (I'm optimisticaly referring to this thread as a debate :) ), and I went off yesterday to have a quick google of 'procedural vs object oriented'.

I'm inclined to lean towards McGruff's point of view, that after a certain point of complexity OO has organizational benefits which are so large as to make OO virtually compulsory for large projects. Although I want to sit on Roja's fence and I can appreciate the ideals being expressed, I just don't think that opinion holds in real world situations. I recognise though that particular types of application (as described in article linked below) such as small applications, and ones optimised for speed, are particularly suited to procedural.

I won't debate that any further, but would like to pick up on McGruff's point that the proof is in the pudding - i.e. lets look at some real world examples to move the debate into a positive space, and what I was quite suprised to find while googling was that there are two major PHP apps that are procedural!! osCommerce, and phpMyAdmin!! I couldn't quite believe this, and I personally think it would be fascinating to see how these projects managed to survive in a procedural state, and how they are managed and constructured this way! And.. the next obvious question.. would there be major benefits (or even losses!?) in transposing these two applications to OO, other than the quite considerable time it would most likely take.

For reference, the article I read was http://www.zend.com/zend/art/oo-proc.php , and for clarification I have not reviewed the code of osCommerce or phpMyAdmin yet (although I have hacked osC a bit, and I remember it wasn't fun).
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

First jl (?) concerns about speed are usually a waste of time - assuming you're not doing anything really daft. I do run stuff through Xdebug or benchmark with ab every so often but it is rare that anything significant turns up.

http://phplens.com/lens/php-book/optimi ... ng-php.php
http://www.xdebug.org/
http://httpd.apache.org/docs/programs/ab.html

The argument that procedural is faster than OOP is not necessarily true. It could be that an OOP soution might lead to less code to parse overall for example. Opcode compiling can be a significant part of the execution time. Whatever, the real gain with OOP is that it is possible to optimise in the first place. With a more modular design it's easier to identify bottlenecks and easier to swap bits in and out.

And that's the nub really. The real challenge is to write flexible, maintainable code not to try to squeeze another millisecond out of the script. Everything you write will go through changes during its lifecycle - not to mention while it is actually being written. OOP (and unit testing) facilitate the refactoring process.

Good design means separating all the different threads in a script rather than writing a tangled mess of spaghetti. Each class, and each method, does just one thing. If you try doing this with functions you'll find that you'll quickly get pushed towards OOP. Passing vars around can get awkward for example - and see here.

Another important advantage of being able to define objects with rich meaning is that you can create a common language between client and programmer. Communication - between programnmers as well as between client and programmer - is heavily emphasised in modern methodologies like XP (extreme programming). If you're working in a team OOP allows different people to work on different parts of the app. Again, unit testing helps by providing a source of documentation and the ability to keep the application functional while changes are being made.

The one disadvantage of OOP is that it will take a big investment in time to learn. But if you're serious about learning to program that's the way to go.

I can't comment on Oscommerce or phpMyadmin. I do, for my sins, have to work with phpBB on this forum though. I find it almost unreadable and I would love to refactor it into OOP to make it more managable. It feels slightly ungrateful to criticise a free, open source program. To be fair, it's probably been written more to allow easy hacking rather than as an example of state-of-the-art design. And it does work.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Here are the statements and assumptions in McGruff's comments:

- Concerns about speed are usually a waste of time

- Passing vars around can get awkward

- It is only possible to write flexible, maintainable code with OOP

- Procedural programming does not facilitate the refactoring process.

- It is not possible to separate "all the different threads in a script" using procedural code.

- Procedural programming produces a "tangled mess of spaghetti"

- In OOP each class, and each method, does just one thing, but in procedural programming each function or library can't do just one thing.

- In OOP you can define objects with rich meaning is that you can create a common language between client and programmer, but in procedural programming you can't define functions, data structures and libraries that create a common language between client and programmer.

- OOP allows different people to work on different parts of the app, but procedural programming does not allow different people to work on different parts of the app.

- unit testing provides many benefits in OOP but is not possible in procedural programming.


And then there is this comment "one disadvantage of OOP is that it will take a big investment in time to learn." This is a strange comment because I don't recall OOP practices taking any more time to learn that procedural/structured practices. OOP is actually pretty easy to do once you get past the esoteric jargon and code a little. I think a more accurate statement is that good software design and programming practices take a big investment in time to learn.

Finally, regarding Oscommerce, phpMyadmin, phpBB, and many other successful PHP apps., it is interesting that McGruff didn't say, "Hey that procedural programming sure produces a bunch of great apps!" No, he wants to refactor them and grudgingly admits that they are in fact designed to be easily understood, modified and extended.

I am a big advocate of OOP and use it for much of what I implement. If I had one wish it would be that the OOP advocates would bring it down to Earth. It's really pretty easy to learn and as straightforward as structured programming practices.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Arborint: I'd prefer that you didn't misquote me. In case you hadn't noticed, I have a very low tolerance for any attempt to get personal on these forums.

Just one point out of many: you claim I said that you can only unit-test OOP. Can you show me where? Since I recently wrote a unit test for you specifically so that you could test a function rather than a class it is difficult to view your post as being made in good faith.

In fact you are half-right: it is rather difficult to unit test in procedural code without well-defined units to test.

There is an obvious difference between someone with an axe to grind trawling through a post to find sniping material (I have seen complaints about your attitude on other forums, remember) and someone who genuinely wants to have a polite discussion.

This comment: "To be fair, it's probably been written more to allow easy hacking rather than as an example of state-of-the-art design" might be legitimately misinterpreted. What I am saying - or rather wondering - is that phpBB may have been written to allow less experienced programmers to mod the program. An OOP version would be beyond them but, if you understand OOP and assuming it was well-written, it would certainly be much easier to maintain.

This topic had (finally) begun to address the issue at hand rather than a disgruntled predilection for argument. Please, no more noise posts.

Incidentally you interrupted me in the middle of a further post about phpMyadmin which you might care to read.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I had a quick look at phpMyadmin (2.4.0) tonight: it's not good. Not only are they using global variables everywhere, they deliberately declare everything in the GPC arrays as globals. That is shocking. In effect, register globals is turned on permanently regardless of your server set up. Any undefined vars or indexes in the script would be wide open to attack. Perhaps there aren't any of these vulnerabilities but there is always a risk that a careless programmer may introduce some at the next update.

From a design standpoint, the presence of global vars indicates an app with no design at all. Global vars are a debugging nightmare and make it impossible to unit test. With OOP, vars are safely locked up in classes alongside the functions which manipulate them.

This is kindergarten code - as also seen in the free mixing of html with (non-presentation) php. There's not much point in looking further.

And yet, it is also one of the most widely used and useful php apps. It tells the machine what to do perfectly adequately but for the programmer, it's a plate of spaghetti with extra parmesan cheese.

The zend article you mentioned (I think) praised this app as a good example of speedy procedural code. It's a moot point whether an OOP version would in fact be slower. Whatever, it's a safe bet that any differences are going to be insignificant in percentage terms in an application which interacts heavily with a db.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

McGruff wrote:I had a quick look at phpMyadmin (2.4.0) tonight: it's not good.
Not only isn't it good, it's ages old. You'd better update it to recent version if you're using it on any production server. phpMyAdmin (versions 2.5.5-pl1 and prior) suffered from directory traversal vulnerability. For some reason this vulnerability isn't mentioned on their Security page.

As for phpMyAdmin's security in general, in fact there were not much vulnerabilities (which I'm aware of), and only one of them was somewhat related to their 'auto-register_globals' approach.
Post Reply