CodeSOD: The Duff Code
As we frequently note, a staggering number of real-world software products start their lives as Access databases running from a shared folder somewhere. There are professional developers who end up maintaining these monstrosities.
Gregory has had the misfortune of being one of those developers. A client has a terribly performing Access database, and it happens to be the driver of their business: it generates insurance quotes for an insurance company.
Let's take a look at some of the code.
'A Pause functionPublic Function HoldIt(Longish As Integer) Dim Startof Dim temp temp = 0 Startof = Second(Now) Do temp = Second(Now) - Startof Loop Until temp > LongishEnd Function
Hey, I think I found the performance problem. The only good thing I can say about this busy loop is that they actually check the system time, and didn't just throw a pile of iterations at it and hope it was good enough.
Then again, why do they want a pause function anyway? I'm not sure I want to know.
Public Sub MegaQuit() Dim FredBlogs Dim intx As Integer Dim intCount As Integer intCount = Forms.Count - 1 For intx = intCount To 0 Step -1 If Forms(intx).Name <> "HiddenStarter" Then DoCmd.Close acForm, Forms(intx).Name End If Next If pboolCloseAccess <> True Then FredBlogs = MsgBox("Application will close. Continue?", vbOKCancel, "EXIT") If FredBlogs = vbCancel Then DoCmd.OpenForm "Start_up" Else pboolCloseAccess = True DoCmd.Quit acQuitSaveAll End If End IfEnd Sub
This method closes all the open windows, asks a confirmation, and then either returns to the startup screen or quits. It's honestly nothing spectacular, aside from the mega-name of the function, and the use of FredBlogs. TIL that "Fred Bloggs" is the UK equivalent of "John Q. Public" in the US- a placeholder name for the average person on the street.
No, that doesn't help me understand why that's the name of this variable, but at least I learned something.
But let's close out with a function that outputs some error messages. I expect to see t-shirts based off these error messages on Shirts that Go Hard before the end of the week.
Public Static Sub FrErr(NameOfApp) Dim Count Count = Count + 1 If Count < 5 Then On Error GoTo FrErrErr MsgBox "I'm broken. I Don't know what happened (I wasn't running at the time)," & vbCrLf & _ "but I called: " & _ NameOfApp & " and bang! The duff code came back with " & vbCrLf & _ Err.Number & ":" & Err.Description & ". Sorry." Else MsgBox "I'm broken. I Don't know what happened (this isn't the first time)," & vbCrLf & _ "but I called: " & _ NameOfApp & " and bang! The duff code came back with " & vbCrLf & _ Err.Number & ":" & Err.Description & ". I'm very sorry. Have you considered restarting the PC?" End If Exit SubFrErrErr: MsgBox "I'm Broken. I Don't know what happened and when I tried to find out I got an error. Sorry.", , "Sorry"End Sub
Duff code is not to be confused with Duff's Device
I'm broken. I don't know what happened (this isn't the first time).
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!