Whoa! Here’s some bad code

Programming

This post has no basis in advice or analysis, I just was documenting an existing SharePoint site I sadly must maintain at work. If you've read my posts in the past, you'll remember that I am not a big SharePoint fan. Well, this isn't really SharePoint related (I just wanted to kick an app while it was down with that last comment) aside from a demonstration of how things can go horribly awry when one tries to mix development methodologies for SharePoint…

The application in question is a corporate use Room Reservation system. Oddly, the company uses Lotus Notes which has an ugly (though quite functional) room reservation system built right in. In my opinion, this never should have been created. The company is trying to get away from Notes but until they migrate the 1500 – 2000 custom lotus notes database applications to SharePoint, Notes is not going anywhere. It should have been one of the last items migrated and it should have just been migrated to Microsoft Exchange as it also has a good meeting scheduler.

At any rate, the reason for the post. Check out this code snippet from a C# 2.0 Web Service which runs under SharePoint. To be honest, until I saw this, I didn't even know C# supported "goto" *slaps head*

And sadly that's one of the more clear-cut code chunks up in there. This my friends is why we have Coding Best Practices and Code Reviews… DON'T LET THIS HAPPEN TO YOU (and you better pray I don't ever catch you writing something like this, you will be flogged!)

            double num = -41.0;
            double num2 = 39.0;
            string str22 = str10;
            if (str22 != null)
            {
                if (!(str22 == "week"))
                {
                    if (str22 == "day")
                    {
                        str12 = "<Where><DateRangesOverlap><FieldRef Name="EventDate" /><FieldRef Name="EndDate" /><FieldRef Name="RecurrenceID" /><Value Type="DateTime"><Today /></Value></DateRangesOverlap></Where>";
                        goto Label_01A5;
                    }
                }
                else
                {
                    str12 = "<Where><DateRangesOverlap><FieldRef Name="EventDate" /><FieldRef Name="EndDate" /><FieldRef Name="RecurrenceID" /><Value Type="DateTime"><Week /></Value></DateRangesOverlap></Where>";
                    num = -8.0;
                    num2 = 15.0;
                }
            }
            goto Label_01BB;
        Label_01A5:
            num = -1.0;
            num2 = 1.0;
        Label_01BB:
            time2 = time.AddDays(num);
            DateTime time3 = time.AddDays(num2);
            string roomid = "0";