Thursday, May 17, 2012

Procedual Programming vs Object Oriented Programming - Part 3

This is a topic every developer must deal with at some point in their life. I broke this article down into several parts. The first part is about procedural programming, the second part is about object oriented programming, and the last part is the summary.

I've gone over the pros and cons for both procedural programming and object oriented programming over the last couple of days.  This part is basically a summary and an included story on why it is probably better to use OOP.

In the beginning, there was an email program that sent out statements to customers.  The application structure was a procedural program.  Basically, a list was returned with the customer name, account number.  Another procedure returned the email addresses for each account.  The last procedure was to create the report that is then saved to the hard disk to be attached to the email and then sent out.  There was a mistake made earlier in the application where the account ID had to be validated.  That validation function then timed out and it just returned the last used Account ID.

So just to make sure you understand everything, the program was going through each account and at a certain point earlier in the application the same account ID would be listed for each account.  Now this trickled down and when the application was building the report it was using the wrong account ID.  So essentially, almost everyone ended up with the same wrong report.  It had their names on it and account numbers, but the information was completely wrong.

Now the reason OOP can prevent this from happening is that the account ID would not be displayed outside of the objects' class and it would always be initialized with 0.  In procedural programming many variables are more public than they should be.  This really isn't a problem with procedural programming as it should've been initialized to 0 before it attempted to get the next one, but it does open you up to making these types of mistakes.  OOP hides variables better and initializes variables when a new object is created.  With procedural programming you can reset variables, but it creates more spaghetti looking code than is necessary.

In the end, unless the application is really small and your team is super small, you're probably better off using OOP.  However, if you don't understand OOP then I do recomend that you start off with some spikes before you start coding any production code.

No comments:

Post a Comment