What’s wrong with this code? #1
.NET, Development, What's wrong with this code? July 9th, 2007
I decided to start a new column gathering all sorts of “what’s wrong with this code?” snippets. Why?
- Its fun.
- Its good for interview questions.
- It starts discussions. More interesting than just talking (or writing) to myself.
So, here’s what I plan:
- I post a code snippet (most probably C# but I’m not setting constraints on myself)
- You comment on what’s wrong with the code on the snippet
- I post my answer, interesting comments, etc. (you can comment on my answer too
)
* Note that these posts are syndicated on blogs.microsoft.co.il (and maybe some other places) so please leave your comments at the main blog - www.ekampf.com/blog/ ** 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.
