James Ingram shares one of his most impressive experiences ...

I had some legacy code originally written by a highly paid consultant. Among his innovations were that all his "Cancel" buttons were on the left, and all his "OK" buttons on the right; the opposite of the MS standard (and every app I've ever seen). The users had gotten used to it, so we couldn't change that.

When he had to present the user with a number of actions to perform, he didn't like the idea of having a button for each option (and an MDI form with menus was something he'd only dreamt about in a nightmare). No! Far more flashy to have an option button for each choice, and an "OK" button at the bottom of the form (on the right, natch, because the Exit button is on the left). A handy two-click launch for each action!

So what did the code behind the "OK" button look like? Well, naturally, it could only be the largest nested If statement in the world:

If optCashReportDay.Value = True Then 
  DoCashReportDay 
Else 
  If optCashReportWeek.Value = True Then 
    DoCashReportWeek 
  Else 
    If optCashReportMonth.Value = True Then 
      DoCashReportMonth 
    Else 
      If optCashReportAnnual.Value = True Then 
        DoCashReportAnnual 
      Else 
        If optBondReportDay.Value = True Then 
          DoBondReportDay 
        Else 
          If optBondReportWeek.Value = True Then 
            DoBondReportWeek 
          Else 
            If optBondReportMonth.Value = True Then 
              DoBondReportMonth 
            Else 
              If optBondReportAnnual.Value = True Then 
                DoBondReportAnnual 
              Else 
                If optStockReportDay.Value = True Then 
                  DoStockReportDay
                Else 
                  If optStockReportWeek.Value = True Then 
                    DoStockReportWeek
                  Else 
                    If optStockReportMonth.Value = True Then 
                      DoStockReportMonth
                    Else 
                      If optStockReportAnnual.Value = True Then 
                        DoStockReportAnnual
                      Else 
                        If optOptionReportDay.Value = True Then 
                          DoOptionReportDay
                        Else 
                          If optOptionReportWeek.Value = True Then 
                            DoOptionReportWeek
                          Else 
                            If optOptionReportMonth.Value = True Then 
                              DoOptionReportMonth
                            Else 
                              If optOptionReportAnnual.Value = True Then 
                                DoOptionReportAnnual
                              Else 
                                If optAssetReportDay.Value = True Then 
                                  DoAssetReportDay
                                Else 
                                  If optAssetReportWeek.Value = True Then 
                                    DoAssetReportWeek
                                  Else 
                                    If optAssetReportMonth.Value = True Then 
                                      DoAssetReportMonth
                                    Else 
                                      If optAssetReportAnnual.Value = True Then 
                                        DoAssetReportAnnual
                                      Else 
                                        'And so on .... seriously
                                      End If
                                    End If
                                  End If
                                End If 
                              End If
                            End If
                          End If
                        End If 
                      End If
                    End If
                  End If
                End If 
              End If 
            End If 
          End If 
        End If 
      End If 
    End If 
  End If 
End If 

Some of his menu screens had 10 or more items, meaning that the nested IFs would actually begin off the right hand side of the code window.

That was one of his better pieces of code...

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