Monday, May 7, 2012

Simplifying An Application with Object Oriented Programming

There are still a high number of professional developers that do not use object oriented programming.  They claim that there method procedural programming is sufficient and has no side effects and is easier to use.  Some may even believe that procedural programming is object oriented programming.   The difference between the two is very subtle, but the results are not.

I altered an application that used procedural programming and changed it to an object oriented program.  The application would load orders from a file and then compare those orders from a database.  If the order was found in one and not the other then it would show that order.  The problem with this is the orders from the file have to be modified a little bit to ensure that the data is able to correctly match up with the databases data.  The file gets loaded into a datatable object just like the orders from the database.  However, the fields aren't the exact same.  The name of the commodity on one doesn't match the name of the commodity on the other.  An option order comes from the file with a field "OPTION" and from the database it's a 1.  Also, the data from the database comes in a different column and so on. 

So the goal is to match them off, but to make it match it off in the easiest way possible.  With the procedural programming it would work, however there would be column movements just before they get matched off.  Some of these were coming as the field number and others were movements with column names.  It really ended up becoming a huge hassle and a mess to make any corrections.  A movement of one column made all of the other columns move and so it was kind of like a Voodoo programmer stepped in and made it work.

I'm not that smart of a guy to keep track of all of these moving pieces.  So in comes object oriented programming.  The first thing I did was to create an interface called IPrelimOrder.  This interface was nothing, but a collection of properties. 
Code Snippet
  1. Public Interface IPrelimOrder
  2.     Inherits IComparable(Of IPrelimOrder)
  4.     <DisplayName("Trade Date")>
  5.     ReadOnly Property TradeDate As String
  6.     <DisplayName("Commodity")>
  7.     ReadOnly Property Commodity As String
  8.     <Browsable(False)>
  9.     ReadOnly Property CommodityConversion As String
  10.     ReadOnly Property Contract As String
  11.     Property Price As String
  12.     ReadOnly Property Buys As Integer
  13.     ReadOnly Property Sells As Integer
  14.     <DisplayName("Option Type")>
  15.     ReadOnly Property OptionType As String
  16.     Property Strike As Double
  17.     <DisplayName("Account")>
  18.     ReadOnly Property AccountNumber As String
  19.     <DisplayName("Source")>
  20.     ReadOnly Property DataSource As String
  21.     <DisplayName("Ticket #")>
  22.     ReadOnly Property TicketNumber As String
  23.     Property Checked As Boolean
  24.     <Browsable(False)>
  25.     Property DataRow As DataRow
  26.     <Browsable(False)>
  27.     ReadOnly Property OMNI As String
  28.     <Browsable(False)>
  29.     ReadOnly Property FirmID As String
  30. End Interface

Then I created a class called DatabaseOrder and FileOrder that implemented my newly created IPrelimOrder.  No more column moves.  The other benefit to doing it this way is now I can store all of this data into a list(Of IPrelimOrder).  If I need to know whether this order came from the file or from the database I simply check the type.  I no longer have to guess and move things around and pray it doesn't create another bug.  When I get the contract name from a file, I can do a quick query to get the correct name.  I don't have to worry about getting the correct name and then moving the column to a different spot to only find out it got the name of the wrong column which threw an exception.

Later on I do internal queries on this same list(Of IPrelimOrder) where I only want to lookup orders from the file.  I can do the following.
Code Snippet
  1. Orders.OfType("FileOrder")

You can separate the two using procedural programming, but it's so much simpler this way. 

If you have any questions or problems or see something wrong, please leave a message.

No comments:

Post a Comment