Summary

Since a picture can say a thousand words, let us look at a routine written in good and bad style. First, the bad, or even the ugly.

Function GetString(s1 As String, s2 As String, n)
Dim t1, t2, i, t3 As Long
t1 = 1
If (n > 1) Then
  For i = 1 To n - 1
    t1 = InStr(t1, s1, s2): If t1 = 0 Then Exit Function
    t1 = t1 + Len(s2)
  Next
End If
If t1 > Len(s1) Then Exit Function
t3 = InStr(t1, s1, s2)
If t3 = 0 Then t2 = Len(s1) Else t2 = t3 - 1
GetString = Mid(s1, t1, t2 - t1 + 1)
End Function

The only nice thing I can say about this routine is that it works, but how? It has at least the following stylistic and logic problems:

  • Three of the four local variables (t1, t2, and i), and one of the arguments (n), use the default data type (possibly Variant). ...

Get Visual Basic® Style Guide, The now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.