Archive for November, 2009

Singleton Considerations

November 22, 2009 8 comments

I started out writing this post as a series of comments on StackOverflow but I decided there just wasn’t enough room to discuss this.

The original question is titled “What is so bad about Singletons?” and can be found at StackOverflow here. The answer receiving the most upvotes was:

Paraphrased from Brian Button:

  1. They are generally used as a global instance, why is that so bad? Because you hide the dependencies of your application in your code, instead of exposing them through the interfaces. Making something global to avoid passing it around is a code smell.
  2. They violate the Single Responsibility Principle: by virtue of the fact that they control their own creation and life-cycle.
  3. They inherently cause code to be tightly coupled. This makes faking them out under test rather difficult in many cases.
  4. They carry state around for the lifetime of the app. Another hit to testing since you can end up with a situation where tests need to be ordered which is a big no no for unit tests. Why? Because each unit test should be independent from the other.


I agree with him on that. In cases, where such a dependency is not obvious by the classes name, the dependency should be injected. Injecting Singleton Instances into classes proves a wrong usage of the pattern.


All Objects need to control their life-cycle in a way. All objects need to have a constructor/destructor. This is even more true in non-managed languages such as C++/Objective-C where you are responsible of releasing your collaborators (Java and .NET programmers tend to forget that).  A Singleton’s life-cycle is assumed to be the same as the application’s, so I don’t see the need to “control” that here. Most Singleton implementations are using a lazy-load mechanism to instantiate themselves. This is trivial and their life-cycle is unlikely to change, or else you shouldn’t use Singleton.


The GoF Singleton Pattern description includes the following: (p.128, Consequences 3.)

Permits refinement of operations and representation. The Singleton class may be subclassed, and it’s easy to configure an application with an instance of this extended class. You can configure the application with an instance of the class you need at run-time“.

Contrary to static classes, Singleton Instances are normal classes and can therefore inherit from base classes and/or implement interfaces. In order to make a Singleton Instance swappable, you can either choose to inject (“initialize”) a “to-be-Singleton Instance” into the Singleton or lazy-load it based on some configuration info. Throwing exceptions on requests made before the Singleton Instance was initialized (and optionally, when an attempt is made to reinitialize it) ensures the same semantic behaviour as that of a simple Singleton Class. Eventually, this is no different from how an IoC Container would deal with Objects whose lifetime shall be that of a Singleton.


If your Singleton carries a significant global state, don’t use Singleton. This includes persistent storage such as Databases, Files etc. Note that log files are an exception to that. In most cases their state isn’t significant for the behaviour of the Application. Good examples for the usage of Singletons are PrintSpoolers (fire and forget).

Categories: Design

.NET Information Day Darmstadt

November 6, 2009 Leave a comment

The .NET User Group Frankfurt and Microsoft Student Partners invited to the .Net Information Day conference at TU Darmstadt University on 4th November 2009.

It was the first .NET conference I attended and also my first encounter with the .NET User Group Frankfurt. Since I had to stay at school till 5pm and arrived at Darmstadt 6.30pm I missed out on the Clean-Code-Developer Initiative opening session and would have nearly missed the complete session about “MultiTouch with .NET 4.0”.

The first complete session I attended was “Distributed Applications with .NET 3.5 & Co.” by Christian Weyer and Dominick Baier from thinktecture. Their “YouTube” demo application was really awesome and Christian was a really great speaker and fun to listen, Dominick played the “Code Monkey” part. Although they just covered WCF basics that I was already familiar with, their talk was really interesting. They showed a fully working sample application featuring a simple HTTP service on the server side.  On the client side they did not just have an ASP.NET application but also an Apple Push Notification iPhone client. All live and all working, most impressive the iPhone client live over internet.

Closing session was  Effective, loosely coupled C# with Daniel Fisher and without Michael Willers, who was ill unfortunately. Besides covering the well known SOLID principles and having his short take on Joel Spolky’s Duct Tape Programmsers blog post. Very interesting. What followed was the best explanation of why one should prefer composition over inheritance I have ever heard of. Daniel claimed that in the contrived “Person->Employee” scenario composition should be used because it resembles reality more closely than inheritance. Of course an Employee is a Person. But what makes a Person an Employee? An employment Contract. What happens if a Person is employed at multiple companies? Right, the contract model provides advantages. This made me realize that there is indeed very litte necessity in multiple inheritance (that’s why it’s left out in C# I guess). Cases where it might prove useful are often better implemented using composition.

I really enjoyed attending the conference, it was excellently organized with interesting sessions and someone also sponsored soft-drinks. I also won a Microsoft T-Shirt, probably for being the youngest attendee :-). The german .NET community is really cool with a rich diversity  and very competent speakers. I’ll try to attend one of the next User Group meetings and get to know them better.

Categories: Events

Frankfurt Marathon 2009

November 5, 2009 2 comments

0n 25th of October 2009 I have finished my first Marathon (42,195 Km).

After retiring from rowing after 2009 Season in July, I decided to try Triathlon for the next year. I knew I’d be going crazy without sports, you just can’t stop it after 5 years of daily training. For Triathlon I bought a nice De Rosa racing bike, so biking and strength training was what I did since  I took off 4 weeks to decompress.

I didn’t intend to run the Frankfurt Marathon this year, but rather run my first Marathon in early 2010 and do the “half-Ironman” at Wiesbaden in summer 2010 when I will be just out of school. It turns out my uncle had registered for Frankfurt Marathon 2009 but couldn’t start for health reasons, so he asked me if I wanted to take over his registration. That was on the weekend before. I wasn’t sure if I’d be able to do it, my last running exercises where almost a year ago and I hadn’t done much endurance training the last few weeks. So exactly 7 days before start, I tested the half-marathon and was able to pass under 2 hours easily. That gave me confidence to agree on starting at Frankfurt Marathon 2009.

FM2009_StartI was confident enough to target a time sub 4 hours. I passed km 21 exactly at 01:59:00, meeting my fan club.FM2009_KM21FM2009_Fans

I was perfectly on schedule and feeling well considering the circumstances. But after crossing the Main down at Frankfurt Höchst (800m long bridge, 15m absolute difference in height) I felt a sudden pain in the back, breaking my rhythm of breathing and making the next 17 km an absolute pain. I realized I needed to shift my goal from a sub-4-hours time to “just finish” even though I didn’t feel physically exhausted.

Km 35-40 was worst, every breathtaking hurt in my chest, forcing me to walk for 10 minutes till the situation improved.

At km 40 I felt a lot better but the last 2 km seemed to take forever. Here I am taking on the last 200m before Finnish inside the Festhalle.

FM2009_KM42I ended up with a total net time of 04:43:45, you can check my results here. On the bottom is also a link that will take you to where you can check out short videos taken at certain milestones based on the time I passed there and also see a pace chart.

Even though it was a lot harder than I thought, it was an invaluable experience. Organization was great, the audience amazing. With better preparation (or better: preparation at all) I will take on sub 4-hours next year.

Categories: Sports
%d bloggers like this: