Home
Coding Horror
Compatible
Line Numbers

Don´t try this
at Home
A Constant Source of Amazement

chunks
I think I'm gonna hurl...

CONSTANTS LET US WRITE CLEARER CODE by replacing raw numbers and text strings with meaningful names. For numbers, there are at least three ways to do this in VB: the `Const´ statement, enumerated types and read-only properties. Whichever way we choose, constants help us to write programs that are easier to read, and hence easier to understand.

The uses of constants may be blindingly obvious, but some people just don´t get it. I´ve seen a number of bewildered programmers define constants called `ZERO´, but the programmer whose code appears here here displays even more confusion. First he unwisely uses the same `vb´ prefix as constants in the VBA library, and then he attempts to distinguish his constants from the VBA ones by adding his name to the prefix.

But there´s worse to come. With the prefixes in hand (or out of hand, depending on how you look at it), our programmer then goes on to choose completely token names for his constants before inexplicably commenting each use of a constant with the actual value. This code bears all the signs of adhering to the letter of a coding standard without evidence of the slightest comprehension.

Constants From Hell
This code was written by an `experienced´ VB contract programmer who was probably earning in the region of £40 per hour. I haven´t modified it in any way except to change the programmer´s name to `dave´. This guy has more to worry about than his constant names...

Sub ProcessInputValidate(Optional ByVal nKeyAscii As Integer)

Dim nCheckIt As Integer, sMsg As String
    Const davevbMinusOne As Integer = -1
    Const davevbZero As Integer = 0
    Const davevbOne As Integer = 1
    Const davevbTwo As Integer = 2
    Const davevbThree As Integer = 3
    Const davevbFour As Integer = 4

    'initialise this
    nCheckIt = davevbMinusOne '-1

    If Len(Trim$(txtOrderNo)) = 0 Then
        nCheckIt = davevbFour '4
        sMsg = "Please enter the Item Number"
        GoSub CheckValidation
    ElseIf Len(txtOrderNo) < 8 Then
        nCheckIt = davevbFour '4
        sMsg = "Item Number should  be 8-digits in length"
        GoSub CheckValidation
    End If

    'Do a bit of Validation here
    If Not VerifyRequiredField(txtInput(0)) Then
        nCheckIt = davevbZero '0
        sMsg = "Please enter Department Code "
    ElseIf Not VerifyRequiredField(txtInput(1)) Then
        nCheckIt = davevbOne '1
        sMsg = "Please enter Product Code "
    ElseIf Not VerifyRequiredField(txtInput(2)) Then
        nCheckIt = davevbTwo '2
        sMsg = "Unit Size needs to be entered"
    ElseIf Not VerifyRequiredField(txtInput(3)) Then
        If Not IsNumeric(txtInput(3)) And Len(Trim$(txtInput(3))) <> 0 Then
            sMsg = "Please enter only numbers in this box"
            nCheckIt = davevbThree '3
        ElseIf Len(Trim$(txtInput(3))) = 0 Then
            sMsg = "Please enter Number of Unit(s) required"
            nCheckIt = davevbThree '3
        Else
            txtInput(3) = txtInput(3)
        End If

    End If

CheckValidation:

    If Not (nCheckIt = davevbMinusOne) Then '-1
        MsgBox sMsg, vbInformation, "Data entry Error"
        If nCheckIt = davevbFour Then '4
            txtOrderNo.SetFocus
        Else
            txtInput(nCheckIt).SetFocus
        End If
        GoSub Sub_Exit
    End If

    'When it reaches here all validation accepted
    If cmdAddToColn.Caption = "&Amend Detail" Then
        'We are going to Amend Details
        AddAndRefresh ("AMENDTC")
    Else
        'We are going to Add details, this actually uses the default values
        Call AddAndRefresh
    End If
    'This will be maintaining the Amend/Update button, setting it to the default
    If cmdAddToColn.ForeColor = davevbBlue Or cmdAddToColn.Caption = "&Amend Detail" Then
        cmdAddToColn.ForeColor = vbBlack: cmdAddToColn.Caption = "&Add Detail"
        cmdAmend.Enabled = True
    End If

Sub_Exit:

    Exit Sub
Sub_Exit_Err:

End Sub

 

Key Spinner

© 1998 - 2009 Mark Hurst. All rights reserved.   Updated March 01, 2009                             sign the guest book