Today is Halloween, a day filled with chills, horrors, and Jamie Lee Curtis. An interesting aspect of horror movies is how often the roots of the horror lurk in the past. Michael Meyers had been in an asylum for decades before his infamous Halloween rampage. Midsommar represents a centuries old tradition. Barbarian is rooted in sins committed a generation prior. Freddy Krueger was the manifestation of the sins of our protagonists' parents. Hell, even Dracula is a menace that had been lurking for centuries before our story begins.
In honor of that, we're going to look at some code from Davide. Like so much classic horror, the seeds of this sin were planted many, many years before Davide arrived.
In 1991, Microsoft released their first version of Visual Basic. The language evolved until 1998, with the release of VB6. Mainstream support ended in 2005, extended support ended in 2008, but like true horror, VB6 has not truly died. The development tools continue to run on all 32-bit versions of Windows.
Back in those olden days, Davide's predecessors decided to implement an Enterprise Resource Planning system in VB6. It grew, and evolved, and became something that couldn't be controlled anymore- it was 2.5M lines of code. It entangled itself into the company, taking over every core business function, and rapidly becoming indispensible.
We can compare it to so many monsters of horror- the shapeshifting Thing, gradually replacing parts of the company with itself. The fungus from The Last of Us, taking over the brain of the company. We can compare it to The Blob, which may also be the most accurate description of the coding practices used in building it.
Here's some code that evaluates a formula entered by the users:
Option Explicit
Dim AA$(26)
Dim VV#(26) 'valori delle variabili
Dim B$(26)
Dim OP$(26)
Dim RANGO(26) As Integer
Dim P$(27)
Dim OPN#(2), COPN$(26)
Dim CV%
Public scrCtl As MSScriptControl.ScriptControl
Dim bCreated As Boolean ' gi� creato lo script control
Dim strUltCodart As String ' ultimo cod. articolo (per seganlazione su che articolo c'� una formula sbagliata)
Dim strUltFormula As String
Dim nEsito As Integer
Public Function FormulaControlla(strFormula As String) As Boolean
17000 Rem --- Input = FORMULA$, Output = AA$(__),CV%,OK%=1 se FORMULA$ non buona
Dim Formula$, f$, CP%, CN%, FLAGP%, c$, OK%, LX%, s%, A$, B$, AB%, BB%, BRANGO%, CRANGO$
FormulaControlla = True
'--- con l'introd. di msscript non controlla pi� niente ....
Exit Function
'-------------------------
Formula$ = strFormula
17010 f$ = Formula$ + Chr$(0): CP% = 0: CN% = 0: CV% = 0: FLAGP% = 0: c$ = "": OK% = 0
17020 LX% = Len(Formula$)
17030 For s% = 1 To LX%
17040 A$ = Mid$(f$, s%, 1): B$ = Mid$(f$, s% + 1, 1)
17050 AB% = Asc(A$): BB% = Asc(B$)
17060 If A$ >= "0" And A$ <= "9" Then If (B$ >= "a" And B$ <= "z") Or B$ = "(" Then OK% = 1: GoTo 17230 Else CN% = CN% + 1: GoTo 17230
17070 If A$ = "*" Or A$ = "/" Or A$ = "+" Or A$ = "-" Then If B$ = "*" Or B$ = "/" Or B$ = "+" Or B$ = "-" Or B$ = Chr$(0) Or B$ = ")" Then OK% = 1: GoTo 17230 Else GoTo 17130
17080 If A$ = "(" Then If B$ = ")" Or B$ = "*" Or B$ = "/" Or B$ = "+" Or B$ = Chr$(0) Then OK% = 1: GoTo 17230 Else CP% = CP% + 1: GoTo 17130
17090 If A$ = ")" Then If B$ = "(" Or (B$ >= "0" And B$ <= "9") Or (B$ >= "a" And B$ <= "z") Or B$ = "." Then OK% = 1: GoTo 17230 Else CP% = CP% - 1: GoTo 17130
17100 If A$ >= "a" And A$ <= "z" Then If (B$ >= "0" And B$ <= "9") Or (B$ >= "a" And B$ <= "z") Or B$ = "(" Or B$ = "." Then OK% = 1: GoTo 17230 Else GoTo 17130
17110 If A$ = "." Then If Not (B$ >= "0" And B$ <= "9") Then OK% = 1: GoTo 17230 Else CN% = CN% + 1: FLAGP% = FLAGP% + 1: GoTo 17230
17120 OK% = 1: GoTo 17230
17130 '---- crea AA$ (__)
17140 If CN% = 0 Then GoTo 17180
17150 If FLAGP% > 1 Then OK% = 1: GoTo 17230
17160 CV% = CV% + 1: AA$(CV%) = Mid$(f$, s% - CN%, CN%)
17170 CN% = 0: FLAGP% = 0
17180 CV% = CV% + 1: AA$(CV%) = A$
17190 If A$ = "*" Or A$ = "/" Or A$ = "+" Or A$ = "-" Then GoTo 17200 Else GoTo 17230
17200 If A$ = "-" And c$ = "(" Or A$ = "-" And s% = 1 Then BRANGO% = 3 + 3 * CP%: CRANGO$ = Right$(" " + str$(BRANGO%), 2): AA$(CV%) = ">" + CRANGO$: GoTo 17230
17210 If A$ = "-" Or A$ = "+" Then BRANGO% = 1 + 3 * CP%: CRANGO$ = Right$(" " + str$(BRANGO%), 2): AA$(CV%) = AA$(CV%) + CRANGO$
17220 If A$ = "*" Or A$ = "/" Then BRANGO% = 2 + 3 * CP%: CRANGO$ = Right$(" " + str$(BRANGO%), 2): AA$(CV%) = AA$(CV%) + CRANGO$
17230 '---- ultima fase
17240 If OK% = 1 Then s% = LX%
17250 c$ = A$
17260 Next s%
17270 If CN% = 0 Then GoTo 17310
17280 If FLAGP% > 1 Then OK% = 1: GoTo 17310
17290 CV% = CV% + 1: AA$(CV%) = Mid$(f$, s% - CN%, CN%)
17300 CN% = 0: FLAGP% = 0
17310 If CN% > 0 Or FLAGP% <> 0 Or CP% <> 0 Then OK% = 1
17320 If OK% = 1 Then FormulaControlla = False Else FormulaControlla = True
End Function
Public Sub FormulaGenPolacca()
18800 Rem --- ROUTINE che, data la NOTAZIONE INTERA
18810 Rem --- GENERA LA NOTAZIONE POLACCA
18820 Rem --- Input = AA$(__), CV%, Output = P$(__)
Dim i As Integer, j As Integer, K As Integer, CONTA As Integer, MAXI%, l As Integer
Dim NUOVA%, KK%, M As Integer
18830 i = 0: j = 1: l = 0: K = 1: CONTA = 0: MAXI% = CV%
18835 If NUOVA% = 1 Then NUOVA% = 0: For KK% = 1 To 26: VV#(KK%) = 0: Next KK%
18840 For KK% = 1 To 26: B$(KK%) = "": OP$(KK%) = "": RANGO(KK%) = 0: P$(KK%) = "": Next KK%: P$(26) = ""
18850 Rem
18860 If CONTA > 0 Then CONTA = CONTA + 1
18870 i = i + 1
18880 If AA$(i) = "(" Or AA$(i) = ")" Then If i = MAXI% Then l = l + 1: OP$(l) = "T": RANGO(l) = 0: GoTo 18910 Else GoTo 18870
18890 If AA$(i) >= "a" And AA$(i) <= "z" Or Asc(AA$(i)) = 46 Or Asc(AA$(i)) > 47 And Asc(AA$(i)) < 58 Then If i = MAXI% Then B$(j) = AA$(i): l = l + 1: OP$(l) = "T": RANGO(l) = 0: GoTo 18910 Else B$(j) = AA$(i): GoTo 18870
18900 l = l + 1: OP$(l) = Left$(AA$(i), 1): RANGO(l) = Val(Right$(AA$(i), 2))
18910 If B$(j) <> "" Then P$(K) = B$(j): K = K + 1: j = j + 1
18920 If l = 1 And i = MAXI% Then GoTo 19020
18930 If l = 1 Then GoTo 18850
18940 If RANGO(l) > RANGO(l - 1) Then CONTA = CONTA + 1: GoTo 18850
18950 If CONTA = 0 Then P$(K) = OP$(l - 1): K = K + 1: RANGO(l - 1) = 0: GoTo 19020
18960 For M = 1 To CONTA + 1
18970 If M = l Then M = CONTA + 1: GoTo 19010
18980 If RANGO(l - M) = 0 Then GoTo 19010
18990 If RANGO(l) > RANGO(l - M) Then GoTo 19010
19000 P$(K) = OP$(l - M): K = K + 1: RANGO(l - M) = 0
19010 Next M
19020 If OP$(l) = "T" Then P$(K) = OP$(l) Else GoTo 18850
19030 '
End Sub
Public Function FormulaValutaPolacca() As Double
19500 Rem --- ROUTINE che, data la NOTAZIONE POLACCA
19510 Rem --- CALCOLA il VALORE DELLA FORMULA
19515 Rem --- Input = P$(__), Output = RISULTATO#,ERRORE%=1 se Division by zero
Dim K As Integer, ERRORE%, OPR$, Q%, NO%, R As Integer, ABC$, RIS#, Risultato#, X$
19520 FormulaValutaPolacca = 0: K = 0: ERRORE% = 0
19530 K = K + 1
19540 If P$(K) >= "a" And P$(K) <= "z" Or Asc(P$(K)) = 46 Or Asc(P$(K)) > 47 And Asc(P$(K)) < 58 Then GoTo 19530
19550 OPR$ = P$(K): P$(K) = "": Q% = 1: While P$(K - Q%) = "": Q% = Q% + 1: Wend: COPN$(2) = P$(K - Q%): P$(K - Q%) = ""
19560 If OPR$ <> ">" And OPR$ <> "T" Then NO% = 1: Q% = 2: While P$(K - Q%) = "": Q% = Q% + 1: Wend: COPN$(1) = P$(K - Q%): P$(K - Q%) = "" Else NO% = 2
19570 Rem-----
19580 For R = 2 To NO% Step -1
19590 If Not (COPN$(R) >= "a" And COPN$(R) <= "z") Then OPN#(R) = Val(COPN$(R)): GoTo 19610
19600 ABC$ = COPN$(R): OPN#(R) = FormulaAssegna(ABC$)
19610 Next R
19620 Rem-----
19630 Rem-----
19640 If OPR$ = "T" Then RIS# = OPN#(2): GoTo 19710
19650 If OPR$ = ">" Then RIS# = -OPN#(2): GoTo 19700
19660 If OPR$ = "*" Or OPR$ = "x" Then RIS# = OPN#(1) * OPN#(2): GoTo 19700
19670 If OPR$ = ":" Or OPR$ = "/" Then If OPN#(2) <> 0 Then RIS# = OPN#(1) / OPN#(2): GoTo 19700 Else Risultato# = 0: ERRORE% = 1: GoTo 19730
19680 If OPR$ = "+" Then RIS# = OPN#(1) + OPN#(2): GoTo 19700
19690 If OPR$ = "-" Then RIS# = OPN#(1) - OPN#(2)
19700 Rem-------
19710 If OPR$ = "T" Then GoTo 19720 Else X$ = str$(RIS#): P$(K) = Right$(X$, Len(X$) - 1): GoTo 19530
19720 Risultato# = RIS#
19730 FormulaValutaPolacca = Risultato#:
19740 If ERRORE = 1 Then MsgBox "Attenzione: Divisione per 0 nella valutazione della formula.", vbCritical, bsTtlErr
End Function
Public Function FormulaAssegna(ABC$) As Double
19800 Rem --- assegna i valori alle variabili operatori -------
19801 Rem --- input ABC$ , globale vv#() output VV# -------
Dim NN%, BVV#
19810 NN% = Asc(ABC$) - 96
If NN% = 1 Or NN% = 2 Or NN% = 3 Or NN% = 11 Or NN% = 14 Then
BVV# = VV#(NN%): FormulaAssegna = BVV#: GoTo Fine
End If
19830 If VV#(NN%) <> 0 Then
BVV# = VV#(NN%): FormulaAssegna = BVV#: GoTo Fine
End If
19860 BVV# = CDbl(InputBox("Inserire il valore della variabile <" + ABC$ + "> =", "Richiesta valore parametro", "0"))
19880 VV#(NN%) = BVV#
19900 FormulaAssegna = BVV#
Fine:
End Function
Public Sub FormulaChiama(strFormula As String)
Dim bOk As Boolean
Dim Risultato#
30200 Rem --- CALCOLO QUANTITA' (da FORMULA$)
30205 '
30210 bOk = FormulaControlla(strFormula): If bOk = False Then Risultato# = 0 Else FormulaGenPolacca: Risultato# = FormulaValutaPolacca
End Sub
The only glimmer of hope is the first line- at least they used Option Explicit
, which makes variable declarations required (instead of just creating a variable anytime you use one). Everything else is horror. VB6 did not require line numbers, which implies that either the developer responsible cut their teeth in older versions of BASIC, or worse: this code started its life in an older version of basic. Or, possibly even worse: the developer couldn't imagine flow control without GoTo
, because there are a lot of them.
While this code was clearly originally written in Italian, the fact that it's in another (natural) language isn't what makes the code impossible for me to understand. The code itself is cryptic and complicated enough that I don't want to decipher it, for fear that solving this puzzle invites Cenobites to tear me apart.
Davide adds: "The woeful part of the story is that it actually works most of the time."
True horrors lurk in this world, nightmares that we can't hope to escape, legacy code that needs to be maintained, and lies in wait for vulnerable developers. Once ensnared in the grasp of this monster, can any developer truly hope to escape?
And finally, for a bonus story of technological horrors, I told a creepy campfire tale about Thomas Midgley, Jr. on stage this weekend. Pardon the cellphone video. The Man Who Sold the World.