WEReveal

OOP and My Conflict

I learned C++ in the early 1990s and started my journey on the Object Orient Programming road although for like 15 years, that road was only a block long. Even still, it began a internal battle with myself that was just simmering but now has started to boil over.

I started using php starting with version 3 circa 1999 although I really didn’t do too much with it until version 4 came out in 2000. Over the years I have developed my own framework for doing all my websites and have built what I call the “site manager”, a CMS of sorts for custom websites. It isn’t a “real” CMS like Joomla, Drupal, or WordPress since it doesn’t have a lot of reader feedback capabilities. On the other hand, I have been able to use the code for quite divergent sites in use and look.

Anyhow, I am rewriting my site manager (version 4) using OOP techniques for PHP 5. This is a serious rewrite in that a lot of new code is being written to replace old code, unlike the previous 2 versions which simply added to or modified existing code. The old code was primarily “procedural” code with only a small bit of OOP code interspersed, often from using PEAR classes such as DB/MDB2. Now I am attempting to be as complete as I can with OOP in my code as PHP allows. And this is where I keep running into my ever present conflict with OOP programming in general. I keep finding I can simply copy my procedural code (normally a collection of functions) straight into a class, make a couple edits such as adding $this-> to a lot of method calls and bam, I have a whole new class that was just a collection of functions gathered into a single file. Now, I am guessing that I have written 50% new code simply because it was easier than trying to convert the old, but in general, a lot of my old procedural code is finding it way into my OOP ( I am having a terrible time not wanting to make a reference to Reeses, so there it is, and now I am hungry ).

SIDENOTE: As nice as the PEAR classes are, and would have reduced the amount of rewrite I would have needed, I ran into a problem where a website I did for a customer suddenly stopped working because a PEAR class was updated on the server and apparently something changed. I stopped trusting 3rd party code and only use my own. I know, silly me. I do know how to include PEAR libraries in my own stuff so that kind of thing wouldn’t happen (and is what I started to do), but I guess I am just a control junkie and have a tendency to want to use my own code for most things.

OOP vs Procedural coding issues (and remember, I have been playing with OOP since the early 1990s), have always confused me in one way or another. Simply put, OOP to me is just a fancy way of doing procedural code… I know, a gaggle of OOP programers are on their way to Omaha to skin me alive for saying that. Wait a sec.

Let me explain. In my way of thinking, we as humans are procedural. We start here and end there. OOP concedes this need of time based actions with its inclusion of methods (functions for those of you who prefer that term). Methods are almost always procedural code in some way, if not always. Even the common naming of certain types of methods, namely, setSomething and getSomthing establish procedures whereby we change the state of a property of an object. Java goes so far as requiring the main method, which establishes the parameters for what the object is and does. It is the one procedure that rules them all.

To say it simply, an object without time based actions is worthless. And time based actions are procedural, they start at some point, do things, and eventually end.

Ok, I can hear some OOP programmers saying, “You just don’t get it.” And I think that is what I am saying in part but I am also saying I am being driven crazy by those who insist that OOP has no procedural code since to me, it does. It just does procedural code much more elegantly. It provides a structure and dare I say it, the proper procedures (ok, for you silly guys, well established Patterns) to write code that can then be used over again, expanded upon, and in general just work.

But I can also hear procedural programmers saying, “Why in the world would you then want to switch to that crazy world called OOP, its a waste of time and ‘everyone’ knows that OOP code runs slower especially with PHP?” Ok, that is an easy answer, there are a lot of good concepts in OOP that should be used, such as inheritance, reuse, encapsulation etc. OOP makes these concepts a reality. For you PHP programmers, check out the book Object-Oriented PHP by Peter Lavin. That book was the final straw that got me to start this crazy project of taking a perfectly working code base and turn it into OOP based code. The book is short but to the point and may encourage the rest of you hold outs to really investigate OOP finally in PHP.

One last thing, and maybe this will mollify some OOP people out there, I refused to do any OOP in PHP 4 since it was a lame excuse for OOP and didn’t follow very well several of the important concepts that I believe makes OOP worth the hassle. Yes, it took me a long time to decide to finally move all my code over to OOP even though I have been using PHP5 in production practically since the day it came out of beta (almost 4 years ago! good grief you guys still writing in PHP4, give it up!). I just didn’t have the time to convert the code since I was too busy using it to create new websites. BTW: the simple fact that I could reuse the same procedural code for many websites with completely divergent needs speaks to the fact that although I was writing pure procedural code, I was in my own way, following some of the OOP concepts including encapsulation, reuse etc. and it has greatly made the conversion to OOP easier.

So, although I am neck deep into OOP now, I still keep asking the silly question that torments my programming soul, “Isn’t this really just fancy procedural programming?”

Tagged on: , ,

Leave a Reply