38

So I once had a job as a C# developer at a company that rewrote its legacy software in .Net after years of running VB3 code - the project had originally started in 1994 and ran on Windows 3.11.

As one of the only two guys in the team that actually knew VB I was eventually put in charge of bug for bug compatibility. Since our software did some financial estimations that were impossible to do without it (because they were not well defined), our clients didn't much care if the results were slightly wrong, as long as they were exactly compatible with the previous version - compatibility proved the results were correct.

This job mostly consisted of finding rounding errors caused by the old VB3 code, but that's not what I'm here to talk about today.

One day, after dealing with many smaller functions, I felt I was ready to finally tackle the most complicated function in our code. This was a beast of a function, called Calc, which was called from everywhere in the code, did a whole bunch of calculations, and returned a single number. It consisted of 500 or so lines of spaghetti.

This function had a very peculiar structure:

Function Calc(...)
...
If SomeVariable Then
...
If Not SomeVariable Then
...
(the most important bit of calculation happened here)
...
End If
...
End If
...
End Function

But for some reason it actually worked. For days I tried to find out what's going on, where the SomeVariable was being changed or how the nesting indentation was actually wrong and didn't match the source, but to no avail. Eventually, though, after many days, I did find the answer.

SomeVariable = 1

Somehow, the makers of VB3 though it would be a good idea for Not X to be calculated as (-1 - X). So if a variable was not a boolean (-1 for True, 0 for False), both X and Not X could be truthy, non-zero values.

And kids these days complain about JavaScript's handling of ==...

Comments
  • 3
    Clever use of code mechanics :)

    (or maybe not).
  • 2
    Nothing was clever about that code base, @Voxera. It was a prime example of programming by accident.
  • 0
    @configurator Oh I do agree =D
  • 0
    @configurator I don't get how they were both truthy? Did you mean that it evaluates 0 as false?
  • 0
    @Metalor 1 is truthy. (-1 - 1) = -2 is also truthy. Not only worked for 0 or -1.
  • 0
    @configurator oh wow... That's actually a thing??!
  • 0
    @Metalor I can't remember all the details as this was over a decade ago, but yes. If I remember correctly, casting to a boolean would coerce a correct value and work correctly, but there was a broken way to convert values to boolean 'I'm guessing through the Variant type), which didn't do the coercion it should have done and caused all this.

    For bonus points, the debugger, when faced with an invalid boolean, showed True rather than the actual value - because that was the boolean formatter it used - which means the problem wasn't actually visible.

    The sad bit is that our entire codebase accidentally relied on that behavior :/
Add Comment