re: What's the Worst Code You've Ever Seen or Written? VIEW POST

FULL DISCUSSION
 

The absolute worst thing I’ve ever seen was some shit like this:

public void IDontRememberTheName(int index)
{
    string i = index.ToString();

    switch(i)
    {
        case “1”:
        // ...
        break;
        case “2”:
        // ...
        break;
        // etc.
    }
}

There was absolutely no fucking reason whatsoever to convert it into a string, and IMO string switch statements shouldn’t be a thing anyway. I wanted to smack whoever wrote that shit.

 

I teach a Java class, and students often gravitate toward strings because of their functionality. Why try to get the third digit from a number using math (i.e. (5092 / 10) % 10) when you can convert it to a string and call the index method on it (i.e. Integer.toString(5092).charAt(3))?

Not making an excuse for this code! It's bad, but I can see why people do it. haha In this case, it really makes no sense.

 

Yeah, if they were actually doing something with the string, I wouldn’t have a problem. But converting it to use in a switch statement is absolutely horrible.

code of conduct - report abuse