OOP and My Conflict

I learned C++ in the ear­ly 1990s and start­ed my jour­ney on the Object Orient Programming road although for like 15 years, that road was only a block long. Even still, it began a inter­nal bat­tle with myself that was just sim­mer­ing but now has start­ed to boil over.

I start­ed using php start­ing with ver­sion 3 cir­ca 1999 although I real­ly didn’t do too much with it until ver­sion 4 came out in 2000. Over the years I have devel­oped my own frame­work for doing all my web­sites and have built what I call the “site man­ag­er”, a CMS of sorts for cus­tom web­sites. It isn’t a “real” CMS like Joomla, Drupal, or WordPress since it doesn’t have a lot of read­er feed­back capa­bil­i­ties. On the oth­er hand, I have been able to use the code for quite diver­gent sites in use and look.

Anyhow, I am rewrit­ing my site man­ag­er (ver­sion 4) using OOP tech­niques for PHP 5. This is a seri­ous rewrite in that a lot of new code is being writ­ten to replace old code, unlike the pre­vi­ous 2 ver­sions which sim­ply added to or mod­i­fied exist­ing code. The old code was pri­mar­i­ly “pro­ce­dur­al” code with only a small bit of OOP code inter­spersed, often from using PEAR class­es such as DB/MDB2. Now I am attempt­ing to be as com­plete as I can with OOP in my code as PHP allows. And this is where I keep run­ning into my ever present con­flict with OOP pro­gram­ming in gen­er­al. I keep find­ing I can sim­ply copy my pro­ce­dur­al code (nor­mal­ly a col­lec­tion of func­tions) straight into a class, make a cou­ple edits such as adding $this-> to a lot of method calls and bam, I have a whole new class that was just a col­lec­tion of func­tions gath­ered into a sin­gle file. Now, I am guess­ing that I have writ­ten 50% new code sim­ply because it was eas­i­er than try­ing to con­vert the old, but in gen­er­al, a lot of my old pro­ce­dur­al code is find­ing it way into my OOP ( I am hav­ing a ter­ri­ble time not want­i­ng to make a ref­er­ence to Reeses, so there it is, and now I am hun­gry ).

SIDENOTE: As nice as the PEAR class­es are, and would have reduced the amount of rewrite I would have need­ed, I ran into a prob­lem where a web­site I did for a cus­tomer sud­den­ly stopped work­ing because a PEAR class was updat­ed on the serv­er and appar­ent­ly some­thing changed. I stopped trust­ing 3rd par­ty code and only use my own. I know, sil­ly me. I do know how to include PEAR libraries in my own stuff so that kind of thing wouldn’t hap­pen (and is what I start­ed to do), but I guess I am just a con­trol junkie and have a ten­den­cy to want to use my own code for most things.

OOP vs Procedural cod­ing issues (and remem­ber, I have been play­ing with OOP since the ear­ly 1990s), have always con­fused me in one way or anoth­er. Simply put, OOP to me is just a fan­cy way of doing pro­ce­dur­al code… I know, a gag­gle of OOP pro­gramers are on their way to Omaha to skin me alive for say­ing that. Wait a sec.

Let me explain. In my way of think­ing, we as humans are pro­ce­dur­al. We start here and end there. OOP con­cedes this need of time based actions with its inclu­sion of meth­ods (func­tions for those of you who pre­fer that term). Methods are almost always pro­ce­dur­al code in some way, if not always. Even the com­mon nam­ing of cer­tain types of meth­ods, name­ly, setSomething and getSomthing estab­lish pro­ce­dures where­by we change the state of a prop­er­ty of an object. Java goes so far as requir­ing the main method, which estab­lish­es the para­me­ters for what the object is and does. It is the one pro­ce­dure that rules them all.

To say it sim­ply, an object with­out time based actions is worth­less. And time based actions are pro­ce­dur­al, they start at some point, do things, and even­tu­al­ly end.

Ok, I can hear some OOP pro­gram­mers say­ing, “You just don’t get it.” And I think that is what I am say­ing in part but I am also say­ing I am being dri­ven crazy by those who insist that OOP has no pro­ce­dur­al code since to me, it does. It just does pro­ce­dur­al code much more ele­gant­ly. It pro­vides a struc­ture and dare I say it, the prop­er pro­ce­dures (ok, for you sil­ly guys, well estab­lished Patterns) to write code that can then be used over again, expand­ed upon, and in gen­er­al just work.

But I can also hear pro­ce­dur­al pro­gram­mers say­ing, “Why in the world would you then want to switch to that crazy world called OOP, its a waste of time and ‘every­one’ knows that OOP code runs slow­er espe­cial­ly with PHP?” Ok, that is an easy answer, there are a lot of good con­cepts in OOP that should be used, such as inher­i­tance, reuse, encap­su­la­tion etc. OOP makes these con­cepts a real­i­ty. For you PHP pro­gram­mers, 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 tak­ing a per­fect­ly work­ing code base and turn it into OOP based code. The book is short but to the point and may encour­age the rest of you hold outs to real­ly inves­ti­gate OOP final­ly in PHP.

One last thing, and maybe this will mol­li­fy some OOP peo­ple out there, I refused to do any OOP in PHP 4 since it was a lame excuse for OOP and didn’t fol­low very well sev­er­al of the impor­tant con­cepts that I believe makes OOP worth the has­sle. Yes, it took me a long time to decide to final­ly move all my code over to OOP even though I have been using PHP5 in pro­duc­tion prac­ti­cal­ly since the day it came out of beta (almost 4 years ago! good grief you guys still writ­ing in PHP4, give it up!). I just didn’t have the time to con­vert the code since I was too busy using it to cre­ate new web­sites. BTW: the sim­ple fact that I could reuse the same pro­ce­dur­al code for many web­sites with com­plete­ly diver­gent needs speaks to the fact that although I was writ­ing pure pro­ce­dur­al code, I was in my own way, fol­low­ing some of the OOP con­cepts includ­ing encap­su­la­tion, reuse etc. and it has great­ly made the con­ver­sion to OOP eas­i­er.

So, although I am neck deep into OOP now, I still keep ask­ing the sil­ly ques­tion that tor­ments my pro­gram­ming soul, “Isn’t this real­ly just fan­cy pro­ce­dur­al pro­gram­ming?”

Tagged on: , ,

Leave a Reply