Steve F. works at a large insurance company as a .NET programmer was excited to finally get a chance to use his skills and rewrite a VB6 modules. Unfortunately, since no one really understood how the existing module worked or even all that it did, the spec was basically to make sure the new component acted like the old one.

After a few painful weeks of analysis, Steve finally uncovered how the system did its thing. It was architected to store business rules in the database in pseudo-XML ("pseudo" enough that a regular XML DOM/Parser would not work). After XML was pulled from the database, strings were replaced and extracted in order to build a large VBScript string. The component then instantiated a Scripting Runtime and executed the script.

The code followed the architecture well: convoluted and unmaintainable. Today's example is a heavily stripped down version of the "heart and soul" of the system: ProcessRule(). The method spanned thousands of lines, so you can imagine the difficulty in condensing it. Removed was the VBScript building/executing code and all of the "business specific" logic. The rest is verbatim, including the variable names and recursive rule processing logic.

Private Sub ProcessRule(nType As Long, nRuleIndex As Long, ByRef sLogic As String)
  Const METHOD_NAME = "GetApplicationVariable"

  Dim bTrue           As Boolean
  Dim bFalse          As Boolean
   
  If nType = 1 Then
    'Root Level, retreive parameters ...
  Else
    Call ProcessRule(nType - 1, nRuleIndex, sLogic)
    Exit Sub
  End If

    
  If nTrueRuleID = 0 And nFalseRuleID = 0 Then
    'Snipped: 1500 lines of business-specific logic
    Exit Sub
  Else
    If nTrueRuleID <> 0 Then
      bTrue = True
      Call ProcessRule(1, nTrueRuleID, sLogic)
    Else
      If nFalseRuleID <> 0 Then
        bFalse = True
        Call ProcessRule(2, nFalseRuleID, sLogic)
        Exit Sub
      End If
    End If
        
    If nFalseRuleID <> 0 Then
      bFalse = True
      Call ProcessRule(2, nFalseRuleID, sLogic)
    End If
  End If
    
  Select Case True
    Case bTrue And bFalse
      'SNIP: 100's of lines of business logic
    Case (bTrue) And (Not bFalse)
      'SNIP: 100's of lines of business logic
    Case (Not bTrue) And (bFalse)
      'SNIP: 100's of lines of business logic
    Case (Not bTrue) And (Not bFalse)
      'SNIP: 100's of lines of business logic
    Case Else
  End Select
    
  Exit Sub

ProcessRuleError:
  ...
    
End Sub

I should note that the code was not part of an underwriting analysis system or something really needing a business rules engine. And even if it did, it's possible that this was the worst possible way to do it. No less, it did provide a nice reference and "white paper" for the consulting company brought in to build the system way back when.

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