Eran Kampf
Eran Kampf
1 min read

What's wrong with this code? #1

I decided to start a new column gathering all sorts of “what’s wrong with this code?” snippets. Why?

  1. Its fun.
  2. Its good for interview questions.
  3. It starts discussions. More interesting than just talking (or writing) to myself.

So, here’s what I plan:

  1. I post a code snippet (most probably C# but I’m not setting constraints on myself)
  2. You comment on what’s wrong with the code on the snippet
  3. I post my answer, interesting comments, etc. (you can comment on my answer too! 😉)

I know there are many “What’s wrong with this code?” out there already. I’ll try not to recycle (too much ;)) When looking at these snippets note the following “Bad Code” classifications: Confusing code, Overly complex code, Performance Issues, Buggy. So, to start with an easy one (I think), check out the following Singleton implementation:

public sealed class Singleton
{
    private Singleton() { }

    private static Singleton _insatnce;
    private static object _syncRoot = new Object();

    public static Singleton Instance
    {
        get
        {
            lock (_syncRoot)
            {
                if (Singleton._insatnce == null)
                {
                    Singleton._insatnce = new Singleton();
                }
                return Singleton._insatnce;
            }
        }
    }
}

Update July 16th, 2007: I’ve posted the answer and a summary on the comments at the following post.