My refactoring heuristics (3)
“Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior preserving transformations. Each transformation (called a ‘refactoring’) does little, but a sequence of transformations can produce a significant restructuring. Since each refactoring is small, it’s less likely to go wrong. The system is also kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring. “
Test Automation / VBScript / Refactoring
Subject: Code Block
Heuristic: Floating Point Comparison
Before
ArgA = ValueA1/ValueA2 ArgB = ValueB1/ValueB2 If ArgA = ArgB Then ' End If
Check business requirements and system requirements. Is $0.0001 equal to $0.00009 ?
After
'Define range based on business or system requirements DeltaRange = 0.00001 ' ArgA = ValueA1/ValueA2 ArgB = ValueB1/ValueB2 If Abs(ArgA-ArgB) < DeltaRange Then ' End If
Heuristic: String Comparison
Before
If StrA = StrB Then ' End If
Is your comparison case-sensitive or not? Do you want to exclude heading and trailing space characters?
After
strA = UCase(Trim(strA)) strB = UCase(Trim(strB)) If StrA = StrB Then ' End If
Heuristic: String Operations
Before
If UCase(strUsername) = "ADMIN" Then ' End If If UCase(strUsername) = "CONTRIBUTOR" Then ' End If If UCase(strUsername) = "VISITOR" Then ' End If
String operations are performance-costly. Avoid / clean up unnecessary calls.
After
strUsername = UCase(strUsername) If strUsername = "ADMIN" Then ' End If If strUsername = "CONTRIBUTOR" Then ' End If If strUsername = "VISITOR" Then ' End If
Subject: Object Class
Heuristic: Constructor
Before
Class MyClass Public MyProperty End Class ' Public Function InitMyClass() Dim objMyClass Set objMyClass = new MyClass objMyClass.MyProperty = TRUE Set InitMyClass = objMyClass End Function
Encapsulate default initialization within class constructor which is called automatically.
After
Class MyClass Public MyProperty ' Private Sub Class_Initialize() MyProperty = TRUE End Sub End Class ' Public Function InitMyClass() Set InitMyClass = new MyClass End Function
Heuristic: Destructor
Before
Class MyClass Public objMyProperty ' ' End Class '
Define destructors to explicitly release objects used as class properties.
After
Class MyClass Public objMyProperty ' Private Sub Class_Terminate() Set objMyProperty= Nothing End Sub End Class '