Lazy Programming Model

The reason we are good programmers is because, by and large, we are lazy. We seek out ways to automate boring database stuff, to generate code and even entire applications. Our Add-Ins menu in VB has at least 3-4 addins and not because some third-party app installed it - we installed it. If this describes you, then you get Rapid Applications Development. If this describes you, then skip this article.

If this does NOT describe you, what you are about to read will make you a good programmer, a fast programmer and it will definetely make you lazy. You will become the "go to" guy - the guy (or gal) the management goes to when something needs to be done now. When there is a crisis, the conversation will go like this: "You're sending {Insert you name}? That's all you had to say!" (Just imagine Samuel Jackson say it, but crank up, or maybe you better lower the sound). It kills me that some programmers never explore past the default UI. Some of them have never seen the Options dialog box, some have never heard of the word AddIn. Those programmers will always stay at the junior level.

So let's start automating our work. Are still writing the following code by hand?


Private mstrName As String

Public Property Get Name() As String
   Name = mstrName
End Property

Public Property Let Name(ByVal strName As String)
   mstrName = strName
End Property

I really feel like reaching out and hitting you. There are about a million add-ins to help you with generating this type of code. One of those is called VB 6 Class Builder and actually comes with VB6. It will do a fairly good job spitting this code out. If you want something better, go to www.pdsa.com, the click on Tips & Tricks and download PDSA Class/Proc Builder. It is simple, but excellent.

Are you still writing by hand the wrappers around stored procedures? Code, like this:


Set Cmd = New ADODB.Command
Set Cmd.ActiveConnection = goConn
Cmd.CommandText = "Insert_BBCredit"
Cmd.CommandType = adCmdStoredProc
Set params = Cmd.Parameters

'Define stored procedure parameters and append to command.
params.Append Cmd.CreateParameter("@deal_id",adInteger, adParamInput, 0)
params.Append Cmd.CreateParameter("@tranche",adChar, adParamInput, 10)
params.Append Cmd.CreateParameter("@trans_date",adDBTimeStamp, adParamInput, 0)
params.Append Cmd.CreateParameter("@user_id",adChar, adParamInput, 10)
params.Append Cmd.CreateParameter("@amt_borrowed",adCurrency, adParamInput, 0)
params.Append Cmd.CreateParameter("@note_type",adChar, adParamInput, 4)
params.Append Cmd.CreateParameter("@credit_type",adChar, adParamInput, 3)

If you have to write wrappers around stored procs a lot, well, then download our own ADO Stored Procedure Code Wizard. Adopted initially from the code released by Microsoft, it was tweaked for usability issues and had Sybase support added. This add-in will generate all the wrappers for the stored proc of your choice on any database that is within your reach. If you are a database programmer, this is a must have.

Have you ever tried to create a collection of objects? Well, the intrinsic Collection object isn't very friendly for this and the resulting code will have trouble being called from ASP. Let's say that you have a class called Class1 with one public property called Name. Now imaging the following code:


dim oCollection as New Collection
dim oC1 as Class1
dim oC2 as Class1

set oC1 = New Class1
set oC2 = New Class1

oCollection.Add oC1, "instance1"
oCollection.Add oC2, "instance2"

Print oCollection("instance1").Name

When typing the last line, you will not get the Intellisense popup after the last period. Why? Because the all oCollection object knows is that you added some type of object to it. It doesn't know exactly what, so it can't resolve it. That is why you need a better "Collection" with well-defined structures, so that you can take full advantage of Intellisense. My favorite add-in to do that is called appropriately Collection Class Master. Written by VBPJ's own Francesco Balena, it does all that and more. Get it here. Then check out his web site for more useful add-ins.

How about generating your own code, peculiar to your situation? Chances are, you are a programmer working with databases. How many times have you written code to open a recordset from a table and read in the data into a class designed to handle it? Too many. Well, why not create a generator where you can select the name of the table and it generates a class for you, then generates a function to retrieve the data into that class. I have one such generator that I use on regular basis. Saves ton of time.

How about error handling. It is almost always the same.


Private Function GetWhatever() as Boolean
    dim oRs as ADODB.Recordset
    On Error GoTo errHandle

    '...Code


    GetWhatever = True
exitPoint:
    'clean up
    set oRs = Nothing
	
    Exit Function
errHandle:
    mstrLastError = GetErrorDescription()
    GoTo exitPoint
End Function

Well, for God's sake - generate it. Or get the error handling template into the a code repository add-in such as our excellent Snippet Master and then just paste it into your app.
OK, let's move on to the scourge of all programmers - parsing and creating fixed lengths files. Because the specs for these files usually come in an Excel spreadsheet, they look like a table:

Field NameStartLength
Name120
Address2150


I usually just generate the code to read or write these files straight out of the spec. It is certainly faster to generate it then to mindlessly write sString = sString & mid$(sContents, 21, 50) over and over again. I read the Excel spreadsheet into a recordset, then simply scroll through the recordset creating the code that I need and paste it into my app. You may have to tweak the spreadsheet so that it can be read in properly. The code to loop through the recordset and generate "code" looks like this (it WILL be different in your case):


Do While Not oRs.EOF
    sCode = sCode & "sString = sString & _ 
        Mid$(sField, " & oRs.Fields("Start") & "," & oRs.Fields("Length") & ")"
    sCode = sCode & vbCrLf

    oRs.MoveNext
Loop
Another area where you speed up your coding is thorough the use of the keyboard shortcuts. Read our famous article on Stupid Keyboard Tricks and give yourself a healthy boost. And finally, a quick list of other ways to increase your programming prowess: Anyway, if you know of any other ways to become a better programmer, let us know.