Refactoring is a trick that managers play on us, to waste our time and break our spirits. Just because they can't wrap their tiny minds around large chunks of similar code doesn't mean that you are unable to wield these weapons. Sometimes, however, we are forced to do things we don't want to do. If you have to refactor: do it late and infrequently.

As a case example, take the code John is being forced to slice into mortal-sized chunks for managerial consumption. It's "classic ASP" and the decision to refactor was made at a critical tipping point: 4381 lines of code, most of it in a single procedure.

This, of course, is a major loss: first and foremost, the programmer's developmental history --- contained therein --- will be destroyed in the process. Like counting rings on a tree or looking at the bones archaeologists "dig up," we can deduce exactly how many concepts a given programmer understood at any given point in the code.

For instance, the following code appears early on.

  Function CheckString(s)
p = InStr(s, "'")
While p > 0
s = Mid(s, 1, p) & "'" & Mid(s, p + 1)
p = InStr(p + 2, s, "'")
CheckString = "'" & s & "'"
End Function


Notice the clever use of single-letter variables. Compare that snippet with the following, found only a few hundred lines later.

  LoopCount = 1
QSElementCount = Request.QueryString.Count
If QSElementCount > 0 Then
Session("QSElements") = Request.QueryString.Key(LoopCount) & _
" = " & Request.QueryString.Item(LoopCount)
End If
While LoopCount < QSElementCount
LoopCount = LoopCount + 1
Session("QSElements") = Session("QSElements") & "&"
Session("QSElements") = Session("QSElements") & _
If Request.QueryString.Item(LoopCount) <> "" Then
Session("QSElements") = Session("QSElements") & _
" = " & Request.QueryString.Item(LoopCount)
End If


Code historians speculate that the programmer, having used more than 26 variables, was forced by his environment to use more descriptive names. Also of interest is the use of Session variables, this one in particular because it is only referenced in one other place.

Response.Write "<input type=""hidden"" name=""QSElements"" value=""" & _
Replace(Session("QSElements"), " ", "") & """>"


At this point, the programmer has discovered Replace, unknown in the first snippet. Also, it is clear that he is beginning to attain enlightenment. This process is complete by the last bit of code in the source file, a statement from the Objectivist school of thought.

  If Session("Reprint") = "" Then
Session("Reprint") = ""
End If


All of this important history is being eroded by careless refactoring. For the record, the first two snippets above have been replaced by

  Function CheckString( vlu )
CheckString = "'" & replace(vlu,"'","''") & "'"
end function
  if request.querystring.count >0 then
Session("QSElements") = replace(request.querystring, "="," = ")
end if

whereas the two latter have been lost to the ages.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!