Archive

You are currently browsing the Miles Per Hour blog archives for the day Thursday, February 12th, 2009.

Feb

12

When to be OOP

By mike@mike-miles.com

In case you don’t know (or didnt realize) web languages (like php, javascript, ect…) have evolved a long way from their first verisons (duh, mike).  With that eveloution they have developed into languages just as complex as standard application languages (java, c++, ect). With the complexity comes the wonderful availability for OOP programming.

The thing is most web programmers are not OOP programmers, the standard for the web has been to be a functional programmer (just writing functions) so a straight up web developer might not be very familiar with OOP programming. OOP programming (creating objects and classes) can be a great resource when writing complex applications.  This post is not about how to create an OOP web application (though, writing one might be handy).  I wanted to write a post that describes my thought process of when to be OOP, because there are times when it’ll benefit you as a developer and times when it’s just unnecessary overhead.

Rule of thumb – functions

Now we all know that functions are a nessecity, reduces redundent code, automates things, makes your life easier (Side note:  I had a few friends in one CS course during college who went the whole semester never writing functions just straight up code. The class was a bioinformatics course using perl so I have no idea why they wanted to do this.) I have a general rule of thumb before writing a function.  For me to place code into a function it’s got to meet at least one of these requirments;

  1. Will I end up writing this code more then twice
  2. Is this code going to be utterly complex, and long (will break into multiple functions)
  3. Can I use this anywhere else

If the code matches one of those then I’ll put it into a function.  If not I might even still put it in a function.  The key is reusability.

Rule of thumb – classes/objects

Now my rule of thumb for creating a class and doing some OOP is almost like my rule of thumb for creating functions, except it deals with functions not just code;

  1. Will I need to reuse these functions multiple times? (and together)
  2. Would it benefit to prevent user from accessing these functions (using OOP ‘private’)
  3. Could I use these functions together in the future

So if a group of functions meets any of those requirements, I’ll rewrite them to be a class.  For example, in PHP I have a general Email class that I can put into any project that needs e-mail capabilities.  and to use it I just need to include it and invoke the object.

It makes it simple for me to add e-mail to any project, and reduces the code I would have to rewrite.  This increases my time for further development.

OOP in web development is also a great plus because you can keep classes and objects independent from each project, allowing you to build up a library that you can use in any project (again reusability!).  this reduces the time you have to take to rebuild something you’ve done in a previous project and gives you more time to produce richer, fuller web applications.