|
![]() Code Cleanup: Before and AfterNothing is worse than inheriting someone else's code which uses a different indentation style, no naming convention, etc. Often, it's incredibly difficult to read which makes fixing problems or adding features more challenging than it should be. Fortunately, the Code Cleanup feature of Total Visual CodeTools lets you clean up existing VB6 and VBA code so that its more readable and improved. Whether its simply adding Option Explicit to modules that lack it so you can get a compiled state, or adding error handling to every procedure that lacks it, Total Visual CodeTools will help you enhance your application quickly and consistently.
Code Cleanup can be applied to your current procedure, current module, a subset of modules, or everything in your project. Select the objects, set the options, then launch it. You can preview the changes before replacing your code. Before CleanupHere's an example of code in module modUtilities before it's cleaned up. You'll notice the lack of consistency with indentations, spacing, variable naming, etc. Function AddRows(CustId As Long, retval As Integer) As Boolean
Dim Northwind As DAO.Database
Dim Customers As DAO.Recordset
Dim LastName1, LastName2, LastName3 As String
LastName1 = "Jones"
LastName2 = "Smith"
LastName3 = "Gates"
Set Northwind = DBEngine.Workspaces(0).OpenDatabase("c:\nwind.mdb")
Set Customers = Northwind.OpenRecordset("customers")
Customers.FindFirst ("CustomerID=" & CustId)
If Customers.Fields(0).Value = LastName1 Then AddRows = True
Select Case Customers.Fields(1).Value
Case 1: retval = 12
Case 2: retval = 13
Case 3: retval = 14
End Select
Customers.Close
Northwind.Close
Set Northwind = Nothing
End Function
Function NullConverter(inValue As Variant) As Variant
If IsNull(inValue) Then NullConverter = "" Else NullConverter = inValue
End FunctionAfter CleanupThe text in yellow explains what changed: Add your module comment structure with a complete procedure list:
' Module : modUtilities ' Description: ' Procedures : AddRows(plngCustId As Long, pintRetval As Integer) As Boolean ' NullConverter(pvarInValue As Variant) As Variant ' Modified : ' 05/23 LC Cleaned with Total Visual CodeTools ' -------------------------------------------------- Add Option Explicit to each module that lacks it: Option Explicit Apply naming conventions to the procedure declaration with parameter prefix: Function AddRows(plngCustId As Long, pintRetval As Integer) As Boolean Add procedure comments with all parameters, the return value and dates: ' Comments: ' Params : plngCustId ' pintRetval ' Returns : Boolean ' Modified: 06/01 djh ' Add Error Handling Enabler: On Error GoTo PROC_ERR Apply naming conventions to variable names based on data type: Dim dbsNorthwind As DAO.Database Dim rstCustomers As DAO.Recordset Split multiple Dims in the same line to separate lines: Dim varLastName1 Dim varLastName2 Dim strLastName3 As String Standardize indentation and line spacing: varLastName1 = "Jones" varLastName2 = "Smith" strLastName3 = "Gates" Set dbsNorthwind = DBEngine.Workspaces(0).OpenDatabase("c:\nwind.mb") Set rstCustomers = dbsNorthwind.OpenRecordset("customers") rstCustomers.FindFirst ("CustomerID=" & plngCustId) Fix single-line If statements to add End If: If rstCustomers.Fields(0).Value = varLastName1 Then AddRows = True End If Eliminate extra blank lines: Fix colon-separated statements and standardized indentation: Select Case rstCustomers.Fields(1).Value Case 1 pintRetval = 12 Case 2 pintRetval = 13 Case 3 pintRetval = 14 End Select rstCustomers.Close dbsNorthwind.Close Set dbsNorthwind = Nothing Add Error Handling Exit and Handler Points: PROC_EXIT: Exit Function PROC_ERR: MsgBox Err.Description Resume PROC_EXIT End Function Standardize to one line between procedures: Function NullConverter(pvarInValue As Variant) As Variant ' Comments: ' Params : pvarInValue ' Returns : Variant ' Modified: 06/01 djh On Error GoTo PROC_ERR Split single-line If statements into multiple lines: If IsNull(pvarInValue) Then NullConverter = "" Else NullConverter = pvarInValue End If PROC_EXIT: Exit Function PROC_ERR: MsgBox Err.Description Resume PROC_EXIT End Function |