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

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

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

#31 Servatis  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 394
  • Joined: 30-March 09

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

Posted 27 May 2011 - 02:42 AM

if(CheckTrue() == false)
{
    return true;
}
else
{
    return false;
}



I have found several of these in the code that I have to change. I gave them the name "flipflop booleans".
One button press I found, runs through 5 of these, I think I now see the reason why this guy doesn't work here anymore. It's making me dizzy just looking at it.

This post has been edited by Servatis: 27 May 2011 - 02:42 AM

Was This Post Helpful? 0
  • +
  • -

#32 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

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

Posted 27 May 2011 - 02:46 AM

Well that's enterprisey.
Was This Post Helpful? 0
  • +
  • -

#33 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 27 May 2011 - 07:15 AM

It's amazing the length some people will go to duplicate the ! operator.
Was This Post Helpful? 0
  • +
  • -

#34 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 02 June 2011 - 09:19 AM

I was making a change to an application today. We encrypt the settings file when saving and decrypt when loading it to read. The application seems to be having a problem reading and saving, so I looked at the method for encrypting....and I saw this....

Attached Image

This is in production code that's been running for 3 years. Not "worst", but I guess my former colleagues liked that movie.
Was This Post Helpful? 0
  • +
  • -

#35 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1020
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

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

Posted 02 June 2011 - 01:57 PM

for(int i = 1; i <= 1; i++)

Found multiple times in the same file. I have no idea what they were trying to do.
Was This Post Helpful? 1
  • +
  • -

#36 DrAcid  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 31-May 11

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

Posted 02 June 2011 - 02:02 PM

View PostMomerath, on 02 June 2011 - 01:57 PM, said:

for(int i = 1; i <= 1; i++)

Found multiple times in the same file. I have no idea what they were trying to do.


Oh my God :D
Why on Earth would do they do this? :D

Laughed hard. Choked in the end...
Was This Post Helpful? 0
  • +
  • -

#37 Shane Hudson  Icon User is offline

  • D.I.C Technophile
  • member icon

Reputation: 345
  • View blog
  • Posts: 1,286
  • Joined: 06-December 09

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

Posted 02 June 2011 - 03:36 PM

View PostMomerath, on 02 June 2011 - 09:57 PM, said:

for(int i = 1; i <= 1; i++)

Found multiple times in the same file. I have no idea what they were trying to do.


I think this code causes the universe to implode on itself!

To be honest, I think I am probably lucky enough not to be able to add to this thread!
Was This Post Helpful? 0
  • +
  • -

#38 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

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

Posted 02 June 2011 - 03:46 PM

Wonder if the JIT would just optimize that out? It's effectively declaring a scope block or simulating a do { } while (false); loop.
Was This Post Helpful? 0
  • +
  • -

#39 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1020
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

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

Posted 02 June 2011 - 06:14 PM

View Posteclipsed4utoo, on 02 June 2011 - 09:19 AM, said:

I guess my former colleagues liked that movie.

Not enough to get the quote right :) And it was a book long before it was a movie (or a radio show, or a computer game, etc.)
Was This Post Helpful? 0
  • +
  • -

#40 darkrain714  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 15-January 09

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

Posted 21 June 2011 - 08:57 PM

View PostSergio Tapia, on 03 December 2010 - 12:05 PM, said:

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!


I've actually seen this suggested before somewhere. It's terrible to read, but it will drop a syntax error if you forget one of the '=' instead of making you find your unintentional assignment.
Was This Post Helpful? 0
  • +
  • -

#41 CodeGrappler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 120
  • Joined: 29-November 10

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

Posted 21 June 2011 - 09:01 PM

View Postdarkrain714, on 21 June 2011 - 08:57 PM, said:

View PostSergio Tapia, on 03 December 2010 - 12:05 PM, said:

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!


I've actually seen this suggested before somewhere. It's terrible to read, but it will drop a syntax error if you forget one of the '=' instead of making you find your unintentional assignment.


It's actually fairly common to do that in languages that let you do it. I.E C++ where if(i = 1) would evaluate to true. While if(1 = i) would throw a syntax error. A lot of people recommend it. I however recommend that you write it the "normal" way and just don't make mistakes :)
Was This Post Helpful? 0
  • +
  • -

#42 darkrain714  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 15-January 09

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

Posted 21 June 2011 - 09:06 PM

View PostCodeGrappler, on 21 June 2011 - 09:01 PM, said:

View Postdarkrain714, on 21 June 2011 - 08:57 PM, said:

../


It's actually fairly common to do that in languages that let you do it. I.E C++ where if(i = 1) would evaluate to true. While if(1 = i) would throw a syntax error. A lot of people recommend it. I however recommend that you write it the "normal" way and just don't make mistakes :)


Yeah, I've never had a taste to write it that way, was just pointing out the reasoning. As to the never making mistakes part, I'll look into that one :)
Was This Post Helpful? 0
  • +
  • -

#43 guardianlamppost  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 17-June 15

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

Posted 17 June 2015 - 07:16 AM

I've been assigned a project and could not for the life of me understand what the purpose of these two lines were.

foreach(object obj in list)
    valueInit.Value=obj;



I continued to the class for the valueInit object and found this..

public List<object> ValueList {get;set;}
public object Value { set{ ValueList.Add(value); } }



The previous developers are no longer with the company, which leaves me to maintain this.
And it does not look better in the rest of the solution.
Was This Post Helpful? 1
  • +
  • -

#44 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 17 June 2015 - 07:53 AM

Wow, abusing properties sucks.

A lot of people don't realize that properties are just methods with special syntax. But the people that do need to realize that just because you can do things that you can do from methods, doesn't mean that you should do them.

"Obvious behavior" should be a goal to strive for. Assigning to a property to add to an internal collection is terribly non-obvious.
Was This Post Helpful? 0
  • +
  • -

#45 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5885
  • View blog
  • Posts: 20,092
  • Joined: 05-May 12

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

Posted 18 June 2015 - 08:27 AM

Unfortunately, it takes a few rounds of being a victim of "cute" or "overly smart" code, that we as developers learn not to inflict the same pain on others and start applying the principle of "least surprise".

I admit to writing highly optimized "smart" code early in my software development career. At that time, I didn't think much of it because I assumed that people would invest the same amount of time I did in doing the initial analysis and design. As I started inheriting ownership of larger and larger code bases, then I realized that no sane person could do that and still have enough time to keep the big picture in their head when diving into a new code base.
Was This Post Helpful? 1
  • +
  • -

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