Sunday, 18 August 2013

Multiple meanings for one variable?

Multiple meanings for one variable?

I'm replacing a guy who was in his first job as a programmer. You'd think
being fresh out of school means the basics are fresh in one's mind, but
think again.
The code base is horrible, Ctrl+C / Ctrl+V is apparent in lots of areas,
about 60% of the logic is coded in forms and the rest is in a monstruous
module called helpers. It hits the database in a If condition and re-hits
the database with the exact same query inside the Else part. In general,
it's as if declaring a variable had a higher cost than hitting the
database or querying XML with an xPath.
Logic is scattered between stored procedures, table-valued functions (with
an inexistent naming convention so you don't know if you're looking for a
SP or a TVF) and inline SQL (string concatenations), and the seldom
comment will just rephrase the code it's commenting.
Complex conditional logic is written in nested If blocks with a cyclomatic
complexity that's just off the charts, in arrow-shaped procedures that can
reach 1200 lines of code.
I can deal with all of the above... to a certain degree. But recently I
came across something that just blew my mind.
The author was declaring a String variable with a meaningful name (call it
smurfName), passed it to a table-valued function to get an integer value,
and then used that integer value to determine another string value that
gets used further down the process, here's a simplified illustration:
Dim smurfName As String
smurfName = (run some inline xPath on a IXMLDOMNode to get a string value)
...
smurfName = (call a helper to run inline SQL, pass smurfName as a
parameter, returns a count)
If smurfName = 0 Then
smurfName = "abc"
ElseIf smurfName > 1 Then
smurfName = "def"
End If
smurfName = (call another SQL procedure, pass smurfName as a parameter)
And it just keeps going.
Is reusing a variable (/giving it a new meaning) a common practice or it's
just another "WTF" in that code base I'm stuck with? Is there any valid
reason for doing that? If not, then what drives a programmer to work like
this? I'm a self-taught programmer, maybe this guy knows things that I
don't (I didn't know about xPaths until I saw them in that code base), but
when I tried understanding the meaning of that variable, this picture came
to my mind:

No comments:

Post a Comment