Worst C# code you've ever seen in production?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

45 Replies - 31928 Views - Last Post: 18 June 2015 - 09:10 AM

#1 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Worst C# code you've ever seen in production?

Posted 03 December 2010 - 12:05 PM

Luckily I haven't had my mind scarred so bad yet, but I saw this gem in production code the other day:

if(1 == getReturnResponse())
{
}



They're what I like to call, yoda conditionals. "If blue is sky", "if broken is car". :lol: We really scolded the guy who wrote it!

Is This A Good Question/Topic? 0
  • +

Replies To: Worst C# code you've ever seen in production?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Worst C# code you've ever seen in production?

Posted 03 December 2010 - 12:09 PM

That's probably an old-time C programmer, but we employed some hard-core C# developers that insisted on that paradigm. It doesn't bother me, and I sometimes find myself slipping into that mode.

The worst code I've ever seen in production is probably my own. I've sort of had to learn C# on the fly, and I'm sure seasoned C#/OOP devs would find themselves in the throes of massive attacks of vomiting if they were to inspect my code. Thankfully I am the only C# dev here, so the vomiting is strictly self-induced :P
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5078
  • View blog
  • Posts: 13,705
  • Joined: 18-April 07

Re: Worst C# code you've ever seen in production?

Posted 03 December 2010 - 12:12 PM

That is pretty mild compared to what you could be running into.

I have run into a company running the same code for over 170+ world wide websites and everything never even involved a single user created function. Everything was long spaghetti code and where you would call a function from multiple places, the code was just repeated. My first 2 months there and I had cut the number of files from 800 to 600 just by introducing functions.

I had worked there for 5 years and still after 5 I was introducing functions where they should have been put in to begin with. ;)

This post has been edited by Martyr2: 03 December 2010 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Worst C# code you've ever seen in production?

Posted 03 December 2010 - 12:21 PM

How can you use C# and not use functions? Wow!

Another one I saw just this morning:

if(cond1)
{
    if(cond2)
    {
        if(cond3)
        {
         ....//7 layers deep.



I pity my friend/coworker because he's the one stuck maintaining this particular aspect of the codebase.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Worst C# code you've ever seen in production?

Posted 03 December 2010 - 12:35 PM

The worst C# code I've ever seen in production was easily my own. I had the same situation as Jack, but I was also learning how to program for companies instead of just as a hobby at the same time. Oh, how I'd cringe if I could see that old code.

The worst code in any language I've ever seen in production was RPG III code on an AS/400...ugh, I'm getting physically ill even remembering it.

It's bad enough that it was RPG III and not RPG/ILE (which includes the revolutionary concept of being free format, and not positional), but this was the most poorly-written, hacked-together piece of shit application you'd ever seen or will ever see.

13,000 lines of IF statements in one file. Seriously, thirteen thousand lines of logic, repeating the same conditions with only one changed, over and over again... :death:

The person who put this together had never heard of relational databases. The entire purpose of this application, database, and machine was to hold the database of record for the company. Which was a single hundred-column monstrosity of a flat file with over two million records. Columns were defined as either alpha, numeric, or alphanumeric. Except, lots of fields that should have been numeric were alphanumeric, so we'd have account numbers like 12345 and 012345, which was distinct.

The table dealt with cars (sold aftermarket appearance protection products and warranties) but there was no related table with make names. So our data entry clerks could put in whatever they wanted. We had over 30 different versions of Chevy, including misspellings. CHEV, CHEVY, CHEVROLET, CHV, CHERVOLET, etc...

Dates were garbage stings that had no validation, so it was entirely possible to input a date like 20100431 (April 31st) which would break all the other applications down the line from it that actually converted it into real dates.

It was the worst thing I was ever around. Luckily it was my job to ETL the data, and not fix the application. Not that working with that garbage table was much better, but...I could have had to work on that code.

@Sergio:

That could be valid, under certain context:

if(cond1)
{
    if(cond2)
    {
        if(cond3)
        {
           //do something
        }
        //do something
    }
    //do something
}

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Worst C# code you've ever seen in production?

Posted 03 December 2010 - 02:14 PM

Yeah, real developers are often hardest on themselves, too. I have to admit my trepidation on finishing up this set of tutorials is fear of being exposed as a C#/OOP hack :stupid:
Was This Post Helpful? 0
  • +
  • -

#7 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Worst C# code you've ever seen in production?

Posted 03 December 2010 - 02:22 PM

I found some code I wrote a long time ago, it was a bingo game. It's amazing how much I grew in since then. That code is just... :withstupid:

http://www.dreaminco...%23-bingo-time/


Hahaha, that thread put a smile on my face. :lol:
Was This Post Helpful? 0
  • +
  • -

#8 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Worst C# code you've ever seen in production?

Posted 03 December 2010 - 02:37 PM

It would be code, that I'm ashamed, I wrote. I was following a not so good book's implementation of managing game states. Here it is.

        protected override void Update(GameTime gameTime)
        {
            newState = Keyboard.GetState();
            newPadState = GamePad.GetState(PlayerIndex.One);

            if (inDialog && dialogNPC != null)
            {
                dialog.Show();
                dialogNPC.StartDialog(dialogNPC.DialogName);
                dialogNPC = null;
            }

            if (!dialog.Enabled)
            {
                inDialog = false;
                dialogNPC = null;
            }

            if (activeScreen == startScreen)
            {
                HandleStartScreenInput();
            }
            else if (activeScreen == helpScreen)
            {
                HandleHelpScreenInput();
            }
            else if (activeScreen == createPCScreen)
            {
                HandleCreatePCScreenInput();
            }
            else if (activeScreen == quitPopUpScreen)
            {
                HandleQuitPopUpScreenInput();
            }
            else if (activeScreen == genderPopUpScreen)
            {
                HandleGenderPopUpScreenInput();
            }
            else if (activeScreen == classPopUpScreen)
            {
                HandleClassPopUpScreenInput();
            }
            else if (activeScreen == difficultyPopUpScreen)
            {
                HandleDifficultyPopUpScreenInput();
            }
            else if (activeScreen == nameInputScreen)
            {
                HandleNameInputScreenInput();
            }
            else if (activeScreen == introScreen)
            {
                HandleIntroScreenInput();
            }
            else if (activeScreen == creditScreen)
            {
                HandleCreditScreenInput();
            }
            else if (activeScreen == actionScreen)
            {
                HandleActionScreeenInput();
                HandlePlayerInput(gameTime);
            }
            else if (activeScreen == viewCharacterScreen)
            {
                HandleViewCharacterScreenInput();
            }
            else if (activeScreen == quitActionScreen)
            {
                HandleQuitActionScreenInput();
            }
            else if (activeScreen == combatScreen)
            {
                HandleCombatScreenInput();
            }
            else if (activeScreen == treasureScreen)
            {
                HandleTreasureScreenInput();
            }
            base.Update(gameTime);

            oldState = newState;
            oldPadState = newPadState;
        }



Consider that I was going to be adding in many, many, many more states. *shudders* Fortunately I found a better way to handle them.
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Worst C# code you've ever seen in production?

Posted 03 December 2010 - 02:50 PM

Hey, at least you didn't have 13,000 lines of them.
Was This Post Helpful? 0
  • +
  • -

#10 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Worst C# code you've ever seen in production?

Posted 03 December 2010 - 03:58 PM

True. This is C, but something I used to see a lot it.

while (8) {
}

for (;8;) {
}


Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Worst C# code you've ever seen in production?

Posted 03 December 2010 - 04:15 PM

8? Now that's odd. How about while(69), or for(;42;), do { /* this */ } while (3.14);?
Was This Post Helpful? 0
  • +
  • -

#12 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Worst C# code you've ever seen in production?

Posted 03 December 2010 - 06:55 PM

8 was used to represent infinity, you know, infinite loop....

This post has been edited by SixOfEleven: 03 December 2010 - 06:57 PM

Was This Post Helpful? 3
  • +
  • -

#13 MentalFloss  Icon User is offline

  • .
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,500
  • Joined: 02-September 09

Re: Worst C# code you've ever seen in production?

Posted 04 December 2010 - 01:15 PM

View PostSixOfEleven, on 03 December 2010 - 05:55 PM, said:

8 was used to represent infinity, you know, infinite loop....


That is hilarious.
Was This Post Helpful? 0
  • +
  • -

#14 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Re: Worst C# code you've ever seen in production?

Posted 04 December 2010 - 07:05 PM

My Prime checking code in C#. Gah its ugly. I am going to thread it to start at certain points but even then its going to be slow.
        static void prime10000()
        {
            //takes about a minute
            int check = 0;
            List<long> primes = new List<long>();
            for (long i = 0; ; i++)
            {
                if (check == 10001)
                    break;
                if (checkifprime(i))
                {
                    check++;
                    primes.Add(i);
                }
            }
            Console.Write(primes[10000]);
            Console.ReadLine();
        }
        static bool checkifprime(long num)
        {
            for (long i = num - 1; i > 0; i--)
            {
                if (i == 1)
                    return true;
                if (num % i == 0)
                    return false;
            }
            return false;
        }

I wrote that for a problem to find the 10001st prime. Takes a minute. But it is definitely my worst good code. If you know what I mean.
Was This Post Helpful? 0
  • +
  • -

#15 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Worst C# code you've ever seen in production?

Posted 09 December 2010 - 01:22 PM

*
POPULAR

I found code like this in an application..

public static string BooleanToString(bool value)
{
    if (value == true)
    {
         return "True";
    }
    else
    {
         return "False";
    }
}


Was This Post Helpful? 6
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »