Tuesday, May 29, 2012

Advancing The Craft

There's been a lot of talk lately about 501 developers and the confusion as to who they are and what to do about it.  My understanding of the original intention of a 501 developer isn't just someone who leaves at 5:01, but someone who makes no advances in their craft.  The time is just a symbol that they do not advance under any condition for any reason.  I usually leave at the same time as I want to go home and spend time with my family.  My children have after school activities that they need to get to and I want to watch them play.  There's nothing wrong with that.

So what is a 501 developer then?  To me it is someone who doesn't love their job or care enough about it to try and learn more.  They make no attempt to advance their craft.  I spend only a little bit of my time each day to learn more.  I don't do it a lot, as I don't want to get burned out and become bored with the very thing that I love doing.

There are two main points that I'm trying to get across.  One is if you don't love your job, then maybe you should be looking for something else to do after 501.  If you do love you job then you not a 501 developer.  If you love your job then you are probably learning or reading blogs like this one already.  The second point is how much time should you spend learning about the craft?  Well, logically, the more you love your job then the more likely it is you'll spend time trying to learn it.  However, if you spend too much time trying to learn then you're probably getting burnt out.  I feel that it doesn't really matter how much time you spend as you will eventually find a balance.

If you don't love what you do, then you should spend some time trying to figure out what you love to do.  If you love what you do, then you already know what you need to do.  If you ever find yourself at the point of not trying to learn or you do not really care about your craft, then you need to do some self reflection and soul searching to find the answer to the question: Do you love this job?

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.

Wednesday, May 16, 2012

Procedual Programming vs Object Oriented Programming - Part 2

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.

The Pros and Cons I found online were pretty standard for OOP.  There is one that is sort of contradictory.  Many people say it's easier to maintain and you can add or remove functionality faster.  Then they go onto say that it takes more development time to create an OOP application.  How can this be?  Well, it's true in that it takes more development time to get started (both design and creating the structure), but once that base structure has been completed it takes no time at all to add, remove, or alter functionality.

Let's say we have two car factories.  One factory is procedural programming.  They take this car and they add one thing at a time until it can drive off the lot.  It's not exactly fast, but it works.  Now the OOP factory is a new factory and you can't just start building cars.  You have to create an assembly line.  This takes some time and thought as you have to structure it correctly before you can start building cars.  Meanwhile the procedural programming factory has created 10 cars as the OOP factory hasn't even built one.  Once the OOP assembly line is completed; building cars becomes a snap and before you know it has built 20 cars while the procedural programming factory has only built 15.

The other thing I want to go over before I show you my list is that OOP enables you (the developer) to alter functionality with a lot less fear.  By this I mean that since everything is structured and the details are hidden, you don't have to worry about changing a detail that will alter everything else.
  • Easy to maintain as in you can add, edit, and alter functionality with less fear.  (This is assuming you understand OOP)
  • Reusability.
  • Easier and faster to add additional features.
  • Everything is structured and therefore, code is cleaner looking.  (No more 100+ line procedures)
  • CPU intensive (Performance may be affected).
  • Learning Curve (Not everyone can wrap their minds around OOP).
  • Harder to follow along as abstraction and inheritance can get confusing.

Tuesday, May 15, 2012

Procedual Programming vs Object Oriented Programming - Part 1

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 did a search earlier on the pros and cons of procedural programming.  I could only get a few results.  The few results I did get were contradicting themselves.  One of the pros stated that procedural programming resulted in portable source code.  One of the cons stated a change in the application means you'll have to rewrite much of the code.  Those two contradict each other.  I'm sure there are instances when you can copy/paste and everything will work out fine.  I can assure you, that 90% of the time you'll be doing a rewrite of the great portable source code.

Another pro to procedural programming that I found stated that it's easier to maintain as you can test a procedure in isolation.  While this sounds great in theory, this is also a contradiction.  Most procedural programs require a higher usage of public variables.  So when creating your tests, you'll have to account for every variable that the procedure uses.  I hope you're religious as you'll be praying that you don't have to test this 500 line behemoth procedure.  Maybe if the procedure was divided into many smaller procedures it would be better, but I wouldn't hold your breath.

Another pro for procedural programming I found stated that your code is more flexible as you can change a specific procedure that gets implemented across the program.  I'm pretty sure they meant a procedure that is not overloaded and that they don't touch the input/output of the procedure.  Otherwise, this pro is surely a con.

Let me explain my list of pros and cons for procedural programming.

  • Easier to follow along as you can step through everything.  Objects are rarely abstracted so each object you come across should make sense immediately.
  • As long as the input/output in your procedures don't change you can modify it fairly easily.  Testing may make this a con as you will need to ensure that this procedure relies on the absolute minimum requirements.
  • You can quickly create code and use existing procedures without requiring much thought.

  • Harder to maintain (if a team is making the application) as a change (or no change) in a global variable will trickle down through all procedures that use it.
  • An input/output change will more often than not require a rewrite of several procedures that lead up to and follow the procedure being changed.
  • An incorrectly handled exception can be devastating as any follow up procedures may be using incorrect or no data that it requires.  (This is the same for OOP, but in some instances OOP won't cause as much devastation - Covered more in part 3.)
  • A lack of some structure can create frustration because of a lack of understanding what the procedure is supposed to do.  (AddOrder(order) and SubmitOrder(order) has similar meanings.  One may mean to add an order to a cart while the other is to submit the order for execution.)

Procedural programming is great for really small teams (2 or less) that are creating really small applications.  The more team members and the larger the product will make the cons I mentioned earlier stick out farther and farther.  You can easily add functionality with procedural programming, but modifying functionality can either be easy or near impossible.  There's not much of an in between.

Tuesday, May 8, 2012

A Dispose Gone Bad

I'm going through some VB.NET source code and I keep seeing a common bad occurrence.  Below is a slightly modified example of the code I keep seeing.
Code Snippet
  2. Dim SqlDataAdapter As New SqlClient.SqlDataAdapter
  3. Dim sqlCommand As New SqlClient.SqlCommand
  4. Dim dt_dataadapter As New DataSet
  6. 'Set Parameter   
  7. With sqlCommand
  8.     .Connection = dataConnection
  9.     .CommandType = CommandType.StoredProcedure
  10.     .CommandText = "blah blah"
  11.     .Parameters.Add("@ID", SqlDbType.Int, 8, "ID").Value = 0
  12. End With
  13. SqlDataAdapter.SelectCommand = sqlCommand
  14. SqlDataAdapter.Fill(dt_dataadapter)
  16. If dt_dataadapter Is Nothing Then
  17.     dt_dataadapter.Dispose()
  18.     SqlDataAdapter.Dispose()
  19.     sqlCommand.Dispose()
  20.     MessageBox.Show("Failed retrieving 'blah blah'.")
  21.     Exit Sub
  22. End If
  23. If dt_dataadapter.Tables.Count = 0 Then
  24.     dt_dataadapter.Dispose()
  25.     SqlDataAdapter.Dispose()
  26.     sqlCommand.Dispose()
  27.     MessageBox.Show("Failed retrieving 'blah blah'.")
  28.     Exit Sub
  29. End If
  30. If dt_dataadapter.Tables(0).Rows.Count = 0 Then
  31.     dt_dataadapter.Dispose()
  32.     SqlDataAdapter.Dispose()
  33.     sqlCommand.Dispose()
  34.     MessageBox.Show("Failed retrieving 'blah blah'.")
  35.     Exit Sub
  36. End If

This same format is being used in multiple locations.  There's probably a lot your seeing wrong with this right now, but if not then that's why you should read the rest of this article.  Let me go through this step by step. 

The developer is using objects that use the interface IDispose.  The second thing you'll notice is there is not one try-catch in this entire section.  If the connection mysteriously disappears the program will terminate immediately.  The end user will have no idea what's going on.  Crash!

Now let's say it gets past that.  Maybe he/she finally put a try-catch around the Fill() function.  The dataset has an exception, but the catch just says a message and it continues on.  If the dt_dataadapter is nothing it then goes and disposes of the dt_dataadapter.  You can't dispose nothing.  So now this results in the application crashing.

Let's choose one more scenario.  Let's say this function is called and is surrounded in a Try-Catch.  Now the application doesn't crash, however the data adapter and the command is never released from memory.  Now you have a memory crisis on your hands.  If this happens enough then the application will crash.

The appropriate and best way to avoid everything that has happened above is by using the Using statement.  The example below is about 1,000 times better, easier to read, and this won't cause any memory leakage.
Code Snippet
  2. Using Command As New SqlClient.SqlCommand("blah, blah", dataConnection)
  3.     Command.Parameters.AddWithValue("@ID", 0)
  5.     Using Adapter As New SqlClient.SqlDataAdapter(Command)
  6.         Using Data As New DataSet
  7.             Try
  8.                 Adapter.Fill(Data)
  9.             Catch ex As Exception
  10.                 MessageBox.Show("Failed retrieving 'blah blah'.")
  11.                 Exit Sub
  12.             End Try
  14.             If Not Data Is Nothing AndAlso Data.Tables.Count > 0 AndAlso Data.Tables(0).Rows.Count > 0 Then
  15.                 ' Do something with the data here.
  16.             Else
  17.                 MessageBox.Show("Failed retrieving 'blah blah'.")
  18.                 Exit Sub
  19.             End If
  20.         End Using
  21.     End Using
  22. End Using

This works because whenever this method is completed it will automatically call the dispose for each object that uses the Using statement.

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

Monday, May 7, 2012

A Misconception Of Object Oriented Programming

I was reading another object oriented programming blog earlier and read in a comment that they didn't understand why anyone in their right mind would use OOP and said that it lacks real world objects.  He or she then began to say why with an example.  He or she said that you can have a person class, a worker class which inherits the person class, and then a student class that also inherits the person class.  He or she then began to state that now you cannot have a student who is also a worker.  They said it's impossible. 

Why is this impossible?  This is the exact reason to use object oriented programming.  The person class should have a PersonID which is a unique value.  Why can't a worker class and a student class point to the same person?  I think their problem was how they were perceiving the data.  Obviously, the worker can't be of the same type as the student.  When you grab data (from a database in my example) you would most likely would be grabbing a specific type.  You would either have tables that holds workers and students or you would have a Person sub table that would be able to hold additional or multiple types.  Below is an image of the second description in how it would look.

The PersonType table simply points a person to a type.  The type then points to a worker or a student.  It could someday point to hundreds of other types.  If you need to get all workers data then you can do so by ensuring the TypeID in the PersonType table has the description of "Worker" or whatever you want to use.  You would then create the instance of the worker class that inherits from the PersonID.  The beauty of this being object oriented is now if you make a change to the person in the worker table, it will automatically be updated to the person in the student table.

Again, I believe they were having trouble imagining this in their head.  Yes, you can't have two objects that are of different types as one type.  You're not ever supposed to.  A Ferrari is not an F-150.  They can't be and aren't supposed to be.  A worker is not a student.  A student is not a worker.  A person who works CAN be a person who is a student and a person who is a student CAN be a person who is a worker.  I don't give grades to workers and I don't think any college would plan on paying any student a salary.

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.

Tuesday, May 1, 2012


Interfaces are one of the most powerful entities in software development.  Couple an interface with a dictionary and you have a very powerful application with extensibility and maintains the SOLID principle.

You may come to a point where you have to load data from a file.  No big deal so far.  But what if the data can come to you in multiple file formats.  Many developers still to this day would then setup a switch statement and depending on the file extension would then define how the file gets loaded.
Code Snippet
  1. Dim extension As String = IO.Path.GetExtension(filename)
  3.         Select Case extension
  4.             Case ".csv"
  5.                 ' Load csv file.
  6.             Case ".txt"
  7.                 ' Load a text file.
  8.             Case ".xls"
  9.                 ' Load a spreadsheet file.
  10.         End Select

This works, but now you have three different subroutines that load a file in a class that is meant to have the data and not load the data.  This violates the single responsibility principle.  So instead we can create a dictionary that will be able to determine what file will get loaded.
Code Snippet
  2. Private fileExtensions As Dictionary(Of IFileLoad.FileExtensions, Lazy(Of IFileLoad))

The interface IFileLoad code is below.
Code Snippet
  1. Public Interface IFileLoad
  2.     Enum FileExtensions As Integer
  3.         CSV = 0
  4.         TXT = 1
  5.         XLS = 2
  6.     End Enum
  8.     Function ImportFile(filename As String) As List(Of IOrder)
  9. End Interface

Add the dictionary items so that they will load up the correct classes that can implement the IFileLoad interface.  A side note is now if you have to export data then you have a class ready to implement a new export interface.  The single responsibility then becomes input/output of files.
Code Snippet
  2. fileExtensions.Add(IFileLoad.FileExtensions.CSV, New Lazy(Of IFileLoad)(Function() New CSVImport))
  3. fileExtensions.Add(IFileLoad.FileExtensions.CSV, New Lazy(Of IFileLoad)(Function() New TXTImport))
  4. fileExtensions.Add(IFileLoad.FileExtensions.CSV, New Lazy(Of IFileLoad)(Function() New XLSImport))

The IOrder interface is then used because it won't matter if the order has additional detail, we just need the basics of an order.  This allows the application to become extensible.
Code Snippet
  1. Public Interface IOrder
  2.     Property OrderID As Integer
  3.     Property OrderNumber As String
  4.     Property OrderType As String
  5.     Property Price As Double
  6. End Interface

Finally, we then get the file extension that will return the list of our orders.
Code Snippet
  1. Dim extension As String = IO.Path.GetExtension(filename)
  2.         Dim importfile As IFileLoad = fileExtensions(extension.TrimStart(".")).Value
  3.         Dim orders As List(Of IOrder) = importfile.ImportFile(filename)

This is so extremely powerful and maintains the single responsibility principle while using dependency inversion.  Two key components of SOLID.

Common Developer Principles

There are two main principles that I usually adhere to.  SOLID and DRY are the two that I preach about the most.  They make things logical and simple.

As I was looking through some source code I came upon this section: (I altered the connection strings.)

Code Snippet
  1. Sub LoadConnection()
  2.         g_intUserID = 0
  3.         ADOModule.g_Connection = New SqlClient.SqlConnection
  4.         Select Case True
  5.             Case Demo
  6.                 ADOModule.SetSQLServerContentText("server", "database", "password", "username", "display", g_intTimeout)
  7.             Case Live
  8.                 If g_ConnectUsingIPAddress Then
  9.                     ADOModule.SetSQLServerContentText("ipaddress", "database", "password", "username", "display", g_intTimeout)
  10.                 Else
  11.                     ADOModule.SetSQLServerContentText("server", "database", "password", "username", "display", g_intTimeout)
  12.                 End If
  13.             Case BackUp
  14.                 If g_ConnectUsingIPAddress Then
  15.                     ADOModule.SetSQLServerContentText("server", "database", "password", "username", "display", g_intTimeout)
  16.                 Else
  17.                     ADOModule.SetSQLServerContentText("server", "database", "password", "username", "display", g_intTimeout)
  18.                 End If
  19.             Case TEST
  20.                 ADOModule.SetSQLServerContentText("server", "database", "password", "username", "display", g_intTimeout)
  21.             Case Else
  22.                 End
  23.         End Select
  24.         If Not ADOModule.ConnectToSQLServer(ADOModule.g_Connection) Then
  25.             End
  26.         End If
  27.     End Sub

This is in two different locations on the application.  There's no reason for this.  At all!  When you add a connection you now have to add this in two different locations.  This can be easily fixed by using dependency inversion which is part of SOLID.  It first starts off with needing an interface.

Code Snippet
  1. Public Interface IDbConnection
  2.     Inherits ICloneable
  4.     Enum ConnectionTo As Integer
  5.         Live = 0
  6.         Backup = 1
  7.         Test = 2
  8.         Demo = 3
  9.     End Enum
  11.     ReadOnly Property IPAddress() As String
  12.     ReadOnly Property Server() As String
  13.     ReadOnly Property Database() As String
  14.     ReadOnly Property Username() As String
  15.     ReadOnly Property Password() As String
  16.     ReadOnly Property Timeout() As Integer
  17.     ReadOnly Property Subject() As String
  18.     ReadOnly Property Display() As String
  20.     Function ConnectToSQLServer() As Boolean
  21.     Function GetSqlConnection() As SqlClient.SqlConnection
  22. End Interface

This interface inherits the ICloneable and IDisposable interfaces.  The reason behind this is now any class that implements my IDbConnection interface (Different namespace than Microsofts) is that they must now implement an ICloneable and IDisposable.  The ICloneable is used to create a new instance that will have the exact same connection string so I can take advantage of connection pooling.  It uses IDispose to dispose of the actual connection.  I also like putting in the enumeration inside the interface as it's easier to manage since if you need to add a connection you would more then likely be looking up this interface and so you would add it here instead of having to search a main module or a main form to add to the enumeration.

Now that we have the connection interface you will need to create 4 new classes that implement this new IDbConnection interface.  A test, backup, demo, and live classes will be created.  You will then need to create a private Dictionary(Of IDbConnection.ConnectionTo, Lazy(Of IDbConnection)).  In the form load event add the following.

Code Snippet
  2. connections = New Dictionary(Of IDbConnection.ConnectionTo, Lazy(Of IDbConnection))
  4. connections.Add(IDbConnection.ConnectionTo.Live, New Lazy(Of IDbConnection)(Function() New LiveConnection))
  5. connections.Add(IDbConnection.ConnectionTo.Test, New Lazy(Of IDbConnection)(Function() New TestConnection))
  6. connections.Add(IDbConnection.ConnectionTo.Demo, New Lazy(Of IDbConnection)(Function() New DemoConnection))
  7. connections.Add(IDbConnection.ConnectionTo.Backup, New Lazy(Of IDbConnection)(Function() New BackupConnection))

Now you can assign the g_connection and the rest of the connections don't take up your memory.

Code Snippet
  2. Dim g_connection As IDbConnection = connections(IDbConnection.ConnectionTo.Live).Value

If there is a change to anything now you just have to change it in one location.  The rest of the connections take up no extra memory like before and then entire thing is easier to maintain.