<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jalf.dk</title>
	<atom:link href="http://jalf.dk/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://jalf.dk/blog</link>
	<description>Musings and thoughts on programming and other geeky stuff</description>
	<lastBuildDate>Mon, 12 Jul 2010 15:21:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Psychic Octopuses</title>
		<link>http://jalf.dk/blog/2010/07/psychic-octopuses/</link>
		<comments>http://jalf.dk/blog/2010/07/psychic-octopuses/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 15:21:00 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Meanwhile]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[supernatural]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=589</guid>
		<description><![CDATA[So the big news these days is obviously Paul the Psychic Octopus. Definitely interesting. It was able to maintain a 100% success rate in this year’s World Cup (and a much higher success rate than it had in 2008 where it mispredicted a whopping two matches). So what’s going on here? Freak coincidence? Supernatural powers? [...]]]></description>
			<content:encoded><![CDATA[<p>So the big news these days is obviously <a href="http://en.wikipedia.org/wiki/Paul_%28octopus%29">Paul the Psychic Octopus</a>.
Definitely interesting. It was able to maintain a 100% success rate in this year’s World Cup (and a much higher success rate than it had in 2008 where it mispredicted a whopping two matches).</p>

<p>So what’s going on here? Freak coincidence? Supernatural powers?
At a first glance, both sound ridiculous to me.</p>

<p>But I want to think about the more “interesting” explanation a bit: perhaps Paul really <em>is</em> psychic. Perhaps Paul <em>can</em> tell the future.
<span id="more-589"></span></p>

<p>And then what? What does that really tell us? Does it answer our problem?
Say we have a psychic octopus. It could show us <em>anything</em>. It could’ve predicted the financial crisis, it could have predicted the death of Michael Jackson, it could have given us next week’s lottery numbers. And yet this psychic octopus chose to show us the World Cup results of all things.</p>

<p>Why? Was there some significance behind this? Is God telling us that the World Cup is <em>important</em>? That we should forget about oil leaks and bank bailouts and climate change, and instead focus on who won the World Cup? That seems unlikely. Why would God, or anyone else, <em>care</em> about that? Or are we back where we started, that it’s just a freak coincidence that they chose to predict the World Cup, rather than the hundreds of other sporting events?</p>

<p>Does it mean that our lives are predetermined, that Spain <em>had</em> to win, that we have no free will?
Or does it mean that Paul was able to <em>control</em> events? Perhaps Spain could have lost, but Paul exerted his mighty Psychic will and gave them the victory. (Is that cheating? Does it count as doping perhaps?)</p>

<p><em>How</em> did Paul know the results? Reading people’s thoughts wouldn’t help, unless someone else also knew the results. Perhaps he can simply see into the future. But is he intelligent? Is he able to <em>choose</em> what to report back to us? Could he have chosen to instead arrange stones at the bottom of his aquarium into the shape of the next week’s lottery numbers? How did he know what it was we were <em>asking</em> him to predict? All Paul saw from us were two boxes with food in them, and a different flag on the top of each. How did Paul figure out “they must be asking about who will win the World Cup”? For that matter, how did he determine that we would interpret it as “the box I open indicates the winning team”? He could just as well have meant it was the team that was going to lose (he’s taking their food, after all)</p>

<p>The problem is that in order to explain anything, it is not enough to say that Paul has psychic abilities. We have to assume that Paul is an intelligent psychic octopus, that we live our lives along predetermined paths (unless Paul is able to control us) <em>and</em> that Paul has an interest in soccer specifically, and figures that of all the things he could reveal to us, the results of each World Cup match is what matters. And we’re still not able to say anything about <em>how</em> he manages to make these predictions.</p>

<p>Taken together, those assumptions makes the “supernatural” explanation sound at least as unlikely as calling it a pure statistical coincidence.</p>

<p>I’m generally a pretty skeptical person. I don’t believe in Gods, magic, ghosts, spirits or anything supernatural. Not because I’m sure none of it exists, but because it seems so absurdly unlikely for those <em>specific</em> beliefs to be true. Let’s say there really is something to the belief in ghosts. How do we know what it is? When you hear weird noises in an old house, how do you know it is the ghosts of dead people specifically? Perhaps instead the house itself is alive. Perhaps it’s the people who live in it in some alternate dimension? Perhaps it’s the Martians remote-controlling little dust clouds to mess with us. Believing in the supernatural isn’t an easy way to dodge the questions we can’t answer. Instead it just makes the problems <em>bigger</em>. Instead of explaining “Last night, I heard someone breathing even though I was home alone”, we now have to explain why <em>somehow</em> dead people are able to walk around here with us, and they’re able to make noises and for some reason they can think of nothing better to do than breathing heavily in my house. We have to explain <em>how</em> dead people come to be here (and that means we have to explain what happens to us when we die), and we have to explain how they can manipulate the world of the living. Somehow, they’re simultaneously insubstantial and invisible, and at the same time, able to make noises, or flick light switches or throw small objects around?</p>

<p>I think it was a lot simpler to explain back when it was just “I heard a weird breathing noise and I have no clue what it was”</p>

<p>Just like I’d rather have to come up with a plausible explanation for an octopus <em>through random chance</em> managing to predict World Cup matches, than having to do the same for the idea that the octopus can tell the future, is intelligent, cares about soccer, and cares about letting us <em>know</em> the result of soccer matches. Oh, and that the future is fixed and we can do nothing to change it.</p>

<p>Correctly guessing the outcome of 8 matches is pretty unlikely. Assuming it has a 50/50 percent chance of correctly guessing the outcome of each match (which sounds likely, given that octopuses probably don’t know much about soccer), the odds of this are 0.39%.</p>

<p>That’s low, very low, but not impossible. Statistically, one out of 256 octopuses should manage such a 8/8 success rate. Pretty lucky then that it happened to be Paul who got it right.</p>

<p>But that’s not quite right. Statistics don’t work like that. Paul made a number of predictions before he got famous, which were what brought him to our attention in the first place. The miracle here isn’t that he predicted 8 matches. Paul only really became famous when he predicted that Germany would beat England. If he can <em>keep</em> guessing correctly, we might start wondering if there’s something going on. But of course it’s going to look miraculous if we include past results. If you roll a die long enough, you’re going to get, say, four 6’es in a row. It’s bound to happen sooner or later. And once that happens, it’s hardly a miracle if you roll another 6. There’s a 16.6% chance that it’ll happen. We can’t include the first 4 rolls and say “I rolled five 6’es in a row! The odds of this happening are 0.013%,! It’s a miracle! This die is magical!”, because you cheated: you waited <em>until</em> you’d got the first four rolls right. The real coincidence is just that the final die roll came up a 6 as well.</p>

<p>So we only really started wondering about Paul’s predictive abilities after England’s defeat. Since then he’s made four predictions: the odds of that are much better. Even by a random coin toss, you’d have a 12.5% chance to get 4 matches right.</p>

<p>So now it’s no longer a supernatural phenomenon, but “just” a curious coincidence.
But there’s another interesting thing to note: take a look at the <a href="http://en.wikipedia.org/wiki/Paul_%28octopus%29#Results_involving_Germany">results</a> listed on Paul’s Wikipedia page. In particular, take a quick look at the flags.</p>

<p>Notice anything?</p>

<p>They don’t look very random. If you don’t look carefully, you won’t even notice that the flag in the “prediction” column varied in a few matches. They’re generally dominated by yellow and red. Nearly all the losers had flags with a lot of blue in them. Perhaps Paul just likes red and yellow better than blue. Serbia is really the only oddity then.</p>

<p>The trend is even more pronounced if we look at the results from 2008: <em>Paul guessed that Germany would win every single match</em>, regardless of the outcome of the actual match.</p>

<p>So perhaps Paul just likes the colors. Or perhaps he’s getting used to fact that whenever people show him a German flag, there’s food underneath it.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2010/07/psychic-octopuses/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>TeXOverflow.com? Sounds good</title>
		<link>http://jalf.dk/blog/2010/06/texoverflow-com-sounds-good/</link>
		<comments>http://jalf.dk/blog/2010/06/texoverflow-com-sounds-good/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 12:18:42 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=579</guid>
		<description><![CDATA[A StackOverflow sibling site dedicated to LaTeX (or TeX in general) questions has been proposed. However, the site won’t be launched unless enough prospective users indicate that they’re willing to use it. As a LaTeX user, I’d love to see this take off. Imagine that, not having to scour Google and random newsgroup archives and [...]]]></description>
			<content:encoded><![CDATA[<p>A StackOverflow sibling site dedicated to LaTeX (or TeX in general) questions has been proposed. However, the site won’t be launched unless enough prospective users indicate that they’re willing to use it.</p>

<p>As a LaTeX user, I’d love to see this take off.<span id="more-579"></span> Imagine that, not having to scour Google and random newsgroup archives and forum posts just to figure out why [X] isn’t working in your LaTeX document, or how to do [Y].</p>

<p>Imagine having a site based on the StackOverflow software, where knowledgeable users are actually going to <em>see</em> your question, and answer it, and even rate the answers so you know which ones are of the highest quality.</p>

<p>I wish we had a site like that when I was a student.</p>

<p>So go and <a href="http://area51.stackexchange.com/proposals/2148/tex-latex-and-friends?referrer=2FnOmt5lNeUN5QfMOt5KEg2">commit to using the site:</a>. The site won’t be launched until it has gathered a critical mass of supporters.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2010/06/texoverflow-com-sounds-good/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Well, that’s that. What should happen now?</title>
		<link>http://jalf.dk/blog/2010/04/well-thats-that-what-should-happen-now/</link>
		<comments>http://jalf.dk/blog/2010/04/well-thats-that-what-should-happen-now/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 21:15:17 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[thesis]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/2010/04/well-thats-that-what-should-happen-now/</guid>
		<description><![CDATA[Dear world. I graduated. My thesis defense went well and I’m no longer a student. Just thought I’d let you know So what happens now? No clue, but I suppose it involves finding a job.]]></description>
			<content:encoded><![CDATA[<p>Dear world.</p>

<p>I graduated. My thesis defense went well and I’m no longer a student. Just thought I’d let you know</p>

<p>So what happens now? No clue, but I suppose it involves finding a job.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2010/04/well-thats-that-what-should-happen-now/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Dogfooding redux</title>
		<link>http://jalf.dk/blog/2010/04/dogfooding-redux-2/</link>
		<comments>http://jalf.dk/blog/2010/04/dogfooding-redux-2/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 16:36:08 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=560</guid>
		<description><![CDATA[A while ago I wrote a bit about Microsoft’s practice of “dogfooding” their software. That sparked a fair amount of discussion on Reddit. Of course, a few people assumed I was talking in absolutes, that because the practice of dogfooding is not perfect, it must be evil. That’s a bit of an exaggeration. I never [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I wrote a bit about Microsoft’s practice of <a href="http://jalf.dk/blog/2010/01/the-downside-to-dogfooding/">“dogfooding”</a> their software. That sparked a fair amount of discussion on Reddit.</p>

<p>Of course, a few people assumed I was talking in absolutes, that because the practice of dogfooding is not <em>perfect</em>, it must be <em>evil</em>. That’s a bit of an exaggeration.<span id="more-560"></span></p>

<p>I never said that “dogfooding is bad”. But dogfooding generally means using your product <em>on a daily basis</em>. Not just poking around with it, or checking it out, but actually using it as your primary tool for… whatever the product is supposed to do for you.</p>

<p>This implies that you can’t really use a lot of <em>other</em> products as intensively while you’re dogfooding your own. A developer on the Visual Studio team can’t <em>both</em> use VS10 for all development, <em>and at the same time</em> use Eclipse, Vim and Emacs on a daily basis. If he were to use all  those, there would no longer be enough time left to use VS10, and so he wouldn’t be dogfooding it.</p>

<p>And that is the problem. Dogfooding is a valuable way to incrementally improve and polish your product. But it also takes up time that could have been spent using <em>someone else’s</em> product. And when you use a product in your daily life, you become blind to a number of its weaknesses.</p>

<p>One of the Reddit commenters mentioned a lovely example: Java developers, people who write Java and nothing else, don’t really see the need for closures. They just don’t really consider it a valuable feature. But pretty much everyone else does. Does this mean that in Java, alone of all languages, closures are not relevant? C# programmers got closures and are ecstatic about them. C++ programmers are about to get closures, and everyone’s just itching for it to happen. Functional languages always had closures, and programmers in those languages just can’t live without them. Are we to believe that specifically in Java, closures just <em>do not matter</em>?</p>

<p>Or does it just mean that the Java developer <em>doesn’t yet realize how much easier closures could make his job</em>? He doesn’t realize this because he’s never had the chance to use them. He’s stuck in the world of Java as it looks today, and if you were to ask him what he’d like changed about the language, he’s not going to say “closures” or “higher order functions”, or even “templates” or “type inference”. He’s going to come up with some small incremental improvement. Wouldn’t it be nice if class X was added to the class library? Wouldn’t it be nice if Y was named differently? Wouldn’t some shorthand syntax for things you can do already be nice?</p>

<p>And the same is true for dogfooding. Visual Studio is slow. Many common operations cause multiple seconds of wait time. Adding an empty file to a project in VS2008 is painful sometimes. But oddly enough, I only really notice it when I’ve gotten used to Vim or Emacs or some other alternative IDE or code editor, which isn’t so slow. Its project structure is fundamentally broken, but I only really notice this when I’ve just spent a few weeks playing around with makefiles<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>. And the same goes for the Windows Mobile phone I had a few years ago. While it worked, I tolerated its quirks. Apart from a few instances when it went <em>really</em> haywire, it wasn’t so bad. Sure, it was sluggish and needed to be rebooted regularly, and sure, the interface wasn’t exactly pretty or convenient to use. But it worked, most of the time, and I got used to it. I didn’t love it, and I didn’t think it was a particularly good phone, or platform or OS, but it was usable. Then the phone broke, and it took me a few weeks with my replacement phone to realize just how much better <em>everything else</em> was. My Windows Mobile phone sucked. I just didn’t realize it until I got to use a different one on a daily basis.</p>

<p>But let me reiterate, dogfooding is a good thing. There’s no doubt of that. Your product can only get better by having your developers actually use it in the same ways that end users are expected to. But if dogfooding is <em>all</em> you do, then the world will pass you by.</p>

<p>So why did I dig this old post up again?</p>

<p>Because Visual Studio 2010 is about to be released, and as we may have expected, this means Microsoft’s developers have to beat their drum a bit about how awesome it is <em>because it’s been dogfooded</em>.</p>

<p><a href="http://blogs.msdn.com/somasegar/archive/2010/04/08/dogfooding-vs-2010-and-net-4.aspx">Soma</a> just seems to forget that the glaring performance issues in both beta 1 and 2 came as a huge surprise to Microsoft <em>until the betas had been released in the wild</em>. As much as they dogfooded it, it didn’t give them the information they needed: <em>that VS10 is painfully slow compared to everything else, including VS9</em>.</p>

<p>Both betas were released pretty much with the message “Don’t worry. It might use a lot of managed code and use a completely new WPF-based editor. But it runs really well and is just as fast as previous versions of Visual Studio”.</p>

<p>There were two reasons for this. One seems to be (according to another Microsoft developer’s blog post which I can’t seem to find at the moment, unfortunately) that they simply didn’t collect the right metrics from all their dogfooders. A lot of their developers <em>did</em> think it was painfully slow, but were never asked how they felt about the current performance level.</p>

<p>And the other is obviously that all their dogfooders <em>were using VS10 in their day-to-day work</em>. They had nothing else to compare it with. They weren’t using Eclipse or Vim or Emacs or even VS9 or VS6 in their daily work. So they became used to the downright painful performance.</p>

<p>But once they released the beta into the wild, it was obvious to <em>everyone else</em>, all those people who had <em>not</em> been using VS10 on a daily basis for months, that it was a huge step backwards.</p>

<p>So yes Microsoft, you’ve heavily dogfooded VS10. No doubt about that. But let’s not pretend that it solved all your problems, or that it gave you the best possible product. In reality, it led to you scrambling the last 6 months to bring the performance back up to where it should have been all along, and where you’d probably have kept it if you’d used <em>other</em> IDE’s occasionally so that you’d had a basis for comparing performance.</p>

<p>It’s not that dogfooding is <em>bad</em>. It’s just not enough. And it is not, in itself, a selling point or a proof of quality.</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>No, I’m not saying makefiles are “better” or even “good”. They have plenty of problems on their own. But they do allow a remarkable degree of flexibility over VS solutions, as <a href="http://gamesfromwithin.com/from-full-to-lite-in-under-an-hour">Noel Llopis realized here</a>. The point is not that VS should switch to plain old makefiles, but just that perhaps the VS project system could be improved to incorporate some of the strengths of these. <a href="#fnref:1" rev="footnote">↩</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2010/04/dogfooding-redux-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thesis defense!</title>
		<link>http://jalf.dk/blog/2010/04/thesis-defense/</link>
		<comments>http://jalf.dk/blog/2010/04/thesis-defense/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 15:56:46 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Meanwhile]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[thesis]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=556</guid>
		<description><![CDATA[The end is nigh. On monday the 12th of April, I’m going to defend my master’s thesis. If you’re in the area, and are geeky enough to find it interesting, feel free to drop by. The precise place and time is: 15:00, April 12, 2010 Room S125 / 3–1-25 DIKU (Datalogisk Institut) Universitetsparken 1 København [...]]]></description>
			<content:encoded><![CDATA[<p>The end is nigh.</p>

<p>On monday the 12th of April, I’m going to defend my <a href="http://jalf.dk/thesis/">master’s thesis</a>. If you’re in the area, and are geeky enough to find it interesting, feel free to drop by.
<span id="more-556"></span></p>

<p>The precise place and time is:
15:00, April 12, 2010</p>

<p>Room S125 / 3–1-25
DIKU (Datalogisk Institut)
Universitetsparken 1
København Ø</p>

<p>Looks like I’m going to be busy the next couple of days preparing my presentation.</p>

<p>That is all.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2010/04/thesis-defense/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Post-thesis, post-aprils-fools update</title>
		<link>http://jalf.dk/blog/2010/04/post-thesis-post-aprils-fools-update/</link>
		<comments>http://jalf.dk/blog/2010/04/post-thesis-post-aprils-fools-update/#comments</comments>
		<pubDate>Sat, 03 Apr 2010 14:03:54 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Games]]></category>
		<category><![CDATA[Meanwhile]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[thesis]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=551</guid>
		<description><![CDATA[Just over a month ago, I handed in my Masters Thesis. All that’s left now is an oral defense of it one of the next weeks. So what happens then? I suppose I should find a job. A few people have asked if I am going to do a PhD, but I don’t think so. [...]]]></description>
			<content:encoded><![CDATA[<p>Just over a month ago, I handed in my Masters Thesis. All that’s left now is an oral defense of it one of the next weeks. So what happens then? I suppose I should find a job. A few people have asked if I am going to do a PhD, but I don’t think so. I think I’ve had enough of academia for now. It was fun while it lasted, but I think it’s time to try something different.
<span id="more-551"></span>
 But beyond that, I don’t really know what I’m going to do. For now, I’ve just enjoyed my free time, catching up on all the things I haven’t really had time for while writing the thesis (such as playing Mass Effect 2, which I heartily recommend, and yes, some coding on various hobby projects).</p>

<p><a href="http://jalf.dk/blog/tag/thesis/">Here</a> is what I’ve previously written about my thesis on the blog, <a href="http://en.wikipedia.org/wiki/Software_transactional_memory">here</a> is what Wikipedia has to say on the subject, and <a href="http://jalf.dk/thesis">here</a> is the thesis itself, including source code.</p>

<p>I’ve been meaning to write this post pretty much for the last month. The reason I’m finally doing it is that I also wanted to drop a quick line on a cute aprils fool joke that should be of interest to a lot of gamers:</p>

<p><a href="http://www.rockpapershotgun.com/">Rock, Paper, Shotgun</a> dedicated the entire day to perfectly ordinary PC game reporting/blogging <a href="http://www.rockpapershotgun.com/2010/04/02/back-to-the-pre-working-for-future-1993"><em>as if it’d been April 1st, 1993</em></a>. Cute and intelligent, and served as a fun trip down memory lane. Nice idea, and a nice change from the usual fare of everyone trying to pull off outrageous or absurd stories ad nauseam. Especially as there seemed to be practically no worthwhile pranks to be found anywhere this year (even Google had some pretty tame ones), your up to the minute coverage of PC gaming news as of 17 years ago really made the day.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2010/04/post-thesis-post-aprils-fools-update/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Singletons: Solving problems you didn’t know you never had since 1995</title>
		<link>http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/</link>
		<comments>http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/#comments</comments>
		<pubDate>Fri, 12 Mar 2010 04:40:59 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[singleton]]></category>
		<category><![CDATA[stackoverflow]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=532</guid>
		<description><![CDATA[Funny how it goes. Some subjects are just flat out impossible to write catchy titles for. Others seem to attract them like flies. A lot of very clever people have written volumes about “The Simpleton Pattern”, and “Singletonitis”. Many people are in love with the Singleton pattern. Others — a small minority, I suspect — [...]]]></description>
			<content:encoded><![CDATA[<p>Funny how it goes. <a href="http://jalf.dk/blog/2010/01/the-meaning-of-raii-or-why-you-never-need-to-worry-about-resource-management-again/">Some subjects</a> are just flat out impossible to write catchy titles for. Others seem to attract them like flies. A lot of very clever people have written volumes about <a href="http://steve.yegge.googlepages.com/singleton-considered-stupid">“The Simpleton Pattern”</a>, and <a href="http://www.gamedev.net/community/forums/mod/journal/journal.asp?jn=259115">“Singletonitis”</a>.</p>

<p>Many people are in love with the <a href="http://en.wikipedia.org/wiki/Singleton_pattern">Singleton pattern</a>. Others — a small minority, I suspect — consider it a mistake, an anti-pattern, or something that was only ever included in <em>the</em> Design Patterns book as a lifeline to procedural programmers who couldn’t really figure out this OOP thing.
<span id="more-532"></span></p>

<p>I won’t pretend to be half as clever as all the people who have already written about the problems with singletons years ago, and I don’t think I have anything <em>new</em> to bring to the table. But it is a pattern I learned to loathe very soon after I first saw it in use. (Singletons do sound attractive when you first hear of them. But they pale a bit when you end up having to tear up and rewrite half your code just because all your singleton classes start revealing their shortcomings) And for a long time now, I’ve tried to convince other programmers that Singletons have some serious problems. Recently, it seems like I’ve even gotten noticed for it on StackOverflow.</p>

<p>First, <a href="http://stackoverflow.com/users/87234/gman">GMan</a> posts an answer to <a href="http://stackoverflow.com/questions/2080233/is-it-good-programming-to-have-lots-of-singleton-classes-in-project/2080242#2080242">one question</a>, and I comment with a mild disagreement, and the discussion goes on for a few more comments. As singleton rants go, this one is pretty mild, and I don’t really think about it any further. Then, a few weeks later, I discover his blog and <a href="http://blackninjagames.com/?p=24">this post</a>. Wow! A convert. A person I know to be extremely bright and a knowledgeable programmer has changed his mind in response to something <em>I</em> said… I’m flattered.</p>

<p>And today, I noticed another question being posted, which had both Boost and Singletons in the title — how could I resist? Two subjects I enjoy talking about, even if the things I say about them are very different. Surprisingly, the comments there already mentioned me, and some of my earlier answers regarding singletons. Should I be flattered that people have started bringing my name up when discussing Singletons?</p>

<p>Anyway, one of the comments also suggested I write a blog post describing my argument in detail. So I will.</p>

<h1>Two wrongs don’t make a right</h1>

<p>There are a lot of problems with singletons. In fact, it’s surprising that so many people still consider the pattern useful, when it is afflicted with so many weaknesses and flaws. However, for now I will single out the two that I feel are the most fundamental: not just problems with how a singleton works, but with what they’re trying to achieve:</p>

<p>A singleton, as defined by the Gang of Four, combines two properties:</p>

<ul>
<li>it guarantees that exactly one instance of an object exists. While that one instance is typically created lazily, so it doesn’t technically exist throughout the entire application’s lifetime, it always seems to the programmer as if precisely one instance exists, and</li>
<li>it guarantees global access to this one instance.</li>
</ul>

<p>Let’s pick those apart a bit. The last one is easiest: it is, by now, fairly common knowledge that <em>global state is bad</em>. We don’t like global variables, we don’t like static class members, we don’t like anything that makes it harder to isolate bits of our code. Dependence on global state causes a lot of problems: it hurts parallelism, as access to global mutable state generally has to be serialized through the use of locks. It makes dependencies harder to detect and control (any function might silently decide to access our singleton. The function signature says nothing about this, so we have to read the source code of the function to determine if this is the case. And because it is so convenient to always just add a reference to a singleton, we tend to do it a lot. When you have a singleton, you quickly end up in a situation where three out of four classes depend on it. How did that happen? Why, logically speaking, do so many classes need direct access to the database? Or the renderer? Is that good design? Not only is this messy, it’s also painfully hard to fix after the fact. Once we have these dependencies on global objects everywhere, that’s a lot of code we need to change to eliminate the global. Almost every class will be impacted by the change, and a huge number of functions have to have their signatures modified to take that extra parameter replacing the global. Or even worse, the function has to be completely rewritten to eliminate the need for whatever service the singleton provided. The more globals you have in your project, the more your dependency graph starts resembling spaghetti. And the harder it gets to clean it up.</p>

<p>It hurts reusability, as code taken from one project and inserted into another may break because it depended on globals not present in the new project. It hurts testability partly for the same reason, a unit test testing a class must suddenly provide a number of globals as well just for the code under test to compile, but also because global state makes tests less deterministic. One test might change the state of this global, affecting the outcome of the next test to run.</p>

<p>Globals are bad for a lot of reasons. They have their uses, no doubt about that, but we should be suspicious whenever the solution to a problem involves global data. It might be the best solution, but often, it is more trouble than it’s worth.</p>

<p>The other point is more subtle. Why do I object to a class enforcing that “only one instance may exist”? It’s really just common sense. As the Agile movement tells us, we don’t really know what our code is going to look like tomorrow. Over the course of development, we <em>have</em> to adapt to changes, modify our code, revise decisions already made. Why put roadblocks in front of us? Why make it harder to adapt to unforeseen changes or requirements?</p>

<p>Today, I might think that I need only one logger instance. But what if I realize tomorrow that I need two? That’s not so far fetched. We may have one log we write ad-hoc messages intended for debugging purposes, solely to be read by developers, and another formalized log, where structured messages are written when predetermined events occur, so that the application can be monitored in production. Sure, we <em>could</em> define the two as completely separate classes, and then we’d only need one instance of each (but then we’d start duplicating code). Or we could use the same log instance to write to both logs (but then the logging code would become more complex, having to interleave two separate and non-overlapping logs.</p>

<p>Once we’ve accepted that an application may need more than one logger, shouldn’t we do ourselves the favor of ensuring that our loggers <em>can</em> be instantiated more than once, just in case it turns out to be the right thing to do? We’re not even adding any complexity, there’s no cost associated with this. On the contrary, we’re <em>removing</em> significant complexity. Thread-safe singletons are surprisingly hard to get right. Dependencies between singletons are tricky and circular ones can cause them to blow up in all sorts of fun ways. And let’s not even get into how to handle anything our singletons might do while the application is shutting down. What if the database singleton tries to write a simple “goodbye” log message to the log singleton? What if the log singleton got destroyed before the database one? Ouch.</p>

<p>Singletons are hard to write and hard to use. Removing them only simplifies our code, so if it also enables us to better adapt to unforeseen requirements, why <em>shouldn’t</em> we remove them?</p>

<p>Not convinced? Let’s think of some other examples then:</p>

<ul>
<li><em>the application configuration should be a singleton, right? We <strong>obviously</strong> can’t have more than one of those!</em> Wrong. We can. We often do. Think about what happens when the user opens the “Options” screen and modifies the settings. During that time, two sets of settings exist: the “applied” settings that are currently in effect, and the “speculative” ones, currently being picked out by the user. Once he clicks OK, the speculative changes should be applied, replacing the ones that were previously in effect. But until then, we have two sets of settings to maintain.</li>
<li><em>a database connection pool then! If we have more than one pool of connections, we can’t efficiently share them!</em> Correct, but perhaps we don’t <em>want</em> to share them. Perhaps I want to ensure that library A has one pool of 10 connections available to it, component B has a smaller pool of 3 connections, an components C, D and E use the global pool with however many connections it supplies. That would ensure that no matter the number of threads running in component B, it’ll never starve out other components trying to access the database. It can never hold more than three connections, leaving room for other components. Of course, in the common case, we do want all connections to be shared in one single pool. But perhaps not <em>always</em>. So yes, there should probably be a globally accessible default pool available. But why shouldn’t it also be possible to define new <em>local</em> pools if the user deems it necessary? Why limit ourselves to one instance?</li>
</ul>

<p>And even if you do come up with some case where we absolutely <em>must</em> never have more than one instance, where it would make the sky come crashing down on us, consider testing. Consider that each of your unit tests should set up the environment it needs, and run within that environment, in isolation from other tests. That means that every test should create its own logger instance, or database pool instance, or whatever else our singletons are doing, just so it can avoid being polluted by stateful changes made by earlier tests. Each unit test for the Direct3D renderer <em>should</em> set up its own renderer object. Each physics simulation test <em>should</em> initialize the physics engine first, and shut it down again after use. Singletons don’t easily allow that. Sure, we can extend them with explicit <code>Create()</code> and <code>Destroy()</code> methods, but then our abstraction is starting to get leaky. We can no longer assume that precisely one instance exists, because we might have just destroyed the one that existed.</p>

<p>The “exactly one instance” guarantee removes flexibility from our code that we may need, in order to enforce a constraint that we <em>definitely</em> don’t need. Where’s the harm in allowing the user to create more than one instance <em>if he decides to?</em></p>

<p>C++ programmers are familiar with <code>std::cout</code>, the standard output stream. Funny thing about this, it is a simple global object. We can <em>obviously</em> never have more than one standard output stream. But we <em>can</em> have more than one stream. The standard library just initializes one of them to point to the standard output, and saves it as a global variable. We don’t need it to be a singleton, we don’t even need it to be a static class specially defined for the purpose. We just need a stream, defined somewhere where it’s globally accessible.</p>

<p>True, a sufficiently stupid programmer <em>could</em> create a new stream when he intended to write to <code>std::cout</code>, and true, a singleton implementation would have prevented that. But is it worth it? When was the last time you saw someone <em>accidentally</em> invoke <code>std::ostream() &lt;&lt; "Hello world";</code>, when they intended to write <code>std::cout &lt;&lt; "Hello world";</code>? It’s not the most common typo I’ve seen.</p>

<p>We don’t <em>need</em> to prevent multiple instantiations. If we want only one instance, we just instantiate the class once, and refer to that instance whenever we need it, end of story. We don’t need the compiler to slap us over the wrists if we do create multiple instances, because we never do so by mistake. If we do it, it’s because we have a reason. It’s because our initial assumption that only one instance was needed, turned out to be wrong!</p>

<p>So there you have it. A singleton combines two <em>negative</em> qualities. It takes the “you can never create a second instance of this class” constraint, which hardly ever makes sense, and even when it does, does not typically need to be enforced by the compiler, <em>and combines it with a global object</em>, giving us all the downsides of both!</p>

<p>Two wrongs don’t make a right. Not even if they were described as a good idea by some guys 15 years ago. They’re still no greater than the sum of their parts: two wrongs. One bad thing combined with another bad thing, creating a <em>very</em> bad thing.</p>

<p>Too many programmers rely heavily on singletons to solve a problem they never had. They never <em>needed</em> a compile-time guarantee that multiple instances of a class can never be created. They just needed one instance to be created.</p>

<p>Sometimes, we do need globals, yes. In those cases, make old-fashioned globals. Use static class members, or if the language allows it, global (non-member) objects. Or use the Monostate pattern, or whatever you feel is the cleanest solution. But remember that the problem you’re trying to solve is “enabling global access to this data”. No more, no less. You do <em>not</em> want a solution which sneaks completely unrelated constraints and limitations in through the back door.</p>

<p>And while I can’t personally think of many cases where this is true, you <em>might</em> also run into situations where it is truly <em>necessary</em> to prevent more than one instance of a class from ever existing. Again, I can’t think of what situation this might be, but I won’t rule out that it can occur. If it does, then enforce <em>that</em> constraint alone. But don’t go around providing <em>global access</em> to the object as well. Whatever specialized purpose your “one instance only” class serves, it’s highly unlikely that <em>everyone</em> should be allowed access to it. So don’t make it a global.</p>

<p>Most of the time, your classes should have neither of these attributes. Sometimes, rarely, they may need <em>one</em> of them. But the singleton pattern imbues the class with <em>both</em> properties, and <em>that</em> is just a plain bad idea.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Privacy: Or why I don’t trust Google with my personal information</title>
		<link>http://jalf.dk/blog/2010/02/privacy-or-why-i-dont-trust-google-with-my-personal-information/</link>
		<comments>http://jalf.dk/blog/2010/02/privacy-or-why-i-dont-trust-google-with-my-personal-information/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 01:50:14 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Meanwhile]]></category>
		<category><![CDATA[buzz]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[privacy]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=506</guid>
		<description><![CDATA[So Google launched their Twitter/MySpace/Facebook killer, Buzz, and apparently subscribed every GMail user to it without asking anyone for permission. The result is that a lot of people now have sensitive personal information floating around in public. An example of this (found via ArsTechnica) is this woman, who starts her post like this: I use [...]]]></description>
			<content:encoded><![CDATA[<p>So Google launched their Twitter/MySpace/Facebook killer, Buzz, and apparently subscribed every GMail user to it without asking anyone for permission.</p>

<p>The result is that a lot of people now have sensitive personal information floating around in public.<span id="more-506"></span> An example of this (found via <a href="http://arstechnica.com/tech-policy/news/2010/02/a-frustrated-user-lashes-out.ars">ArsTechnica</a>) is <a href="http://fugitivus.wordpress.com/2010/02/11/fuck-you-google/">this woman</a>, who starts her post like this:</p>

<blockquote>
  <p>I use my private Gmail account to email my boyfriend and my mother.</p>
  
  <p>There’s a BIG drop-off between them and my other “most frequent” contacts.</p>
  
  <p>You know who my third most frequent contact is?</p>
  
  <p>My abusive ex-husband.</p>
  
  <p>Which is why it’s SO EXCITING, Google, that you AUTOMATICALLY allowed all my most frequent contacts access to my Reader, including all the comments I’ve made on Reader items, usually shared with my boyfriend, who I had NO REASON to hide my current location or workplace from, and never did.</p>
</blockquote>

<p>Ouch.</p>

<p>Others, with less at stake personally, are <a href="http://news.cnet.com/8301-31322_3-10451428-256.html">also pissed</a>:</p>

<blockquote>
  <p>See, I love the idea of neat new tech innovations that lead to streamlined communication, real-time updating, in-line video and photo posting, and supersimple friend and contact integration. I do not, however, like a product that bursts through my door like a tornado and opts me in to wanton in-box clutter and spam (or, more precisely, bacn) publicly reveals my personal contact list without asking me, threatens to broadcast my e-mail address anytime someone wants to @ me in a Buzz, and even appears to grab photos off my Android phone that I’ve never uploaded.</p>
</blockquote>

<p>or <a href="http://ventspace.wordpress.com/2010/02/10/i-have-google-buzz-now-apparently/">this one</a></p>

<blockquote>
  <p>So…yeah, I guess I’m on Google Buzz. It’s linked to my Picasa and WordPress accounts, so you can follow everything I do. Cause that’s not creepy or anything. The best part is that the defaults for everything are public, and you end up broadcasting to a bunch of random people unless you sit down and sort through. I’m expecting this to backfire for a bunch of people, and not just eventually but almost immediately. It might not be a bad idea to start a betting pool on when the first child porn charges are filed as some highschool student accidentally sends herself to the entire school.</p>
</blockquote>

<p>I could go on, but I really don’t want this to turn into some kind of link farm.</p>

<p>I’m not personally affected by this. I do have a GMail account, and yes, they opted me in to Buzz, but the account contains no personal information whatsoever, and no personal emails. I use it exclusively as a dumping ground for spam,  and form mails I don’t want cluttering up my <em>real</em> email inbox. I’ve never even sent an email from the account.</p>

<p>I use the Google search engine, but I am not signed in to it, and have never created a profile or a customized homepage on it. I’m sure they could still identify me just by examining cookies or my IP address, but at least they’d have to work for it. And it’s not like my Google searches are state secrets anyway. As long as people are not able to search for my name and bring up a list of everything I’ve searched for, I’m satisfied.</p>

<p>I also use Google Analytics for this blog. I feel OK about that because this blog is already my public face on the internet. Google already knows a lot about it simply by indexing it for their search engine. I have no problem with them generating statistics on where my visitors come from, as long as they make the information available to me too. The only sensitive information associated with this blog is my login password, and I’m pretty sure Google doesn’t have that. And they’re not getting it, even if they launched a GPassword service tomorrow.</p>

<p>I use the WordPress software, but not hosted on WordPress.com. I don’t use Picasa or Google Reader. I don’t use Google Documents.</p>

<p>So all in all, yes, Google certainly knows a lot of fragments of information about me. Google searches can turn up quite a bit, they can collect a few more bits and pieces through cookies when I use their search engine, and they have a lot of statistics on who reads my blog. But they can’t read my emails. They don’t have any really sensitive information about me. Nothing related to my work, personal life or studies is tied to Google.</p>

<p>And this brings us to the point of this post:</p>

<h1>Don’t blame Buzz, blame GMail</h1>

<p>A lot of people are furious at Google for the mixture of incompetence and indifference towards users’ privacy with which Buzz was launched, and while that might be justified, it is missing a fundamental point.</p>

<p>Buzz is just doing what Google does best, what they’ve always done, and what they <em>should</em> be doing. Here’s what Google’s own <a href="http://www.google.com/corporate/">website</a> has to say on the company’s mission:</p>

<blockquote>
  <p>Google’s mission: to organize the world’s information and make it universally accessible and useful</p>
</blockquote>

<p>Google is dedicated to making information <em>universally accessible</em>. For a lot of information, that’s a good thing. Their search engine turned the internet upside down — for the first time ever, users were able to actually <em>find</em> the information they needed. Google is good at this, and we’ve benefited hugely from it.</p>

<p>And social networking is right up Google’s alley as well: Social networking is all about making information about you and me accessible to the world in an organized manner. A lot of Facebook’s popularity relies on their ability to analyze our existing relations, friendships and networks, and use this to suggest new friends. My Twitter would be useless if I couldn’t follow the people I wanted to keep up with, and if others couldn’t find my tweets through searches. Buzz is simply more of the same, and there is nothing wrong with that. It’s another social networking service, and Google is <em>exactly</em> the right company to do something like this. No one is better at organizing information and telling us exactly what we want to know.</p>

<p>The problem is that another of their services is not so well suited for the company. Email is something almost everyone considers personal and private. Even the US government, in its desperate war on people who wear turbans, speak funny and pray to Allah, has only given itself permission to sniff the subject lines of people’s mails sent over GMail. This is considered the equivalent of reading the envelope, without opening it and looking at the letter inside. Because that letter is personal. And so are the bodies of our emails.</p>

<p>But if we consider our emails to be sensitive personal information, then <em>why do so many people entrust them to a company whose stated mission is “to make the world’s information universally accessible”</em>?</p>

<p>A company like that should <em>never</em> be entrusted with our sensitive information.</p>

<p>Facebook has made some major blunders regarding privacy, but their mission seems to be something like “can’t we just all get along”. In Facebook’s perfect world, everyone are friends with everyone else. This doesn’t excuse their privacy issues, but at least it tells us that they’re not directly opposed to the idea of privacy. They’re just clumsy and don’t think things through.</p>

<p>Google, however, is different. In the perfect Google world, <strong>privacy does not exist</strong>. In Google’s dream world, I could go take a look at Bill Gates’ emails or Steve Jobs’ search history. or Bono’s shopping list. It is information. It should be made available to the world.</p>

<p>So no, there’s nothing wrong with Google Buzz. It should absolutely broadcast everything Google knows about us to the world. The problem is that Google has been given sensitive information <em>in the first place</em>. Google shouldn’t know anything about us that can’t safely be published through Buzz. If GMail had never existed, Google would not know that the woman in the first example has received emails from her abusive ex-husband, and so they couldn’t have caused her any problems. The only things Buzz would have known about us would be what we told it.</p>

<p>Imagine if Twitter or Facebook had been built by Google, based on their search engine and their ability to categorize and organize information. That is what Buzz could potentially become, and that’d be nothing short of amazing. At least as long as we all take care to keep our emails and other sensitive information <em>far</em> away from Google.</p>

<p>Don’t opt out of Buzz because of privacy concerns. Opt out of GMail instead. Expect every new service Google launches to do as Buzz. Their mission is to make all information available to the world, and they’re going to keep trying. You’re fighting a losing battle. You can keep opting out of their services till the cows come home. It’s always a temporary solution at best. Instead, fix the root issue: Make sure Google is not given any sensitive information about you in the first place.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2010/02/privacy-or-why-i-dont-trust-google-with-my-personal-information/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The downside to “dogfooding”</title>
		<link>http://jalf.dk/blog/2010/01/the-downside-to-dogfooding/</link>
		<comments>http://jalf.dk/blog/2010/01/the-downside-to-dogfooding/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 17:00:05 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[dogfooding]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[visual-studio]]></category>
		<category><![CDATA[Windows Mobile]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=433</guid>
		<description><![CDATA[A term that’s become very popular, and which especially Microsoft’s developers seem to champion, is “dogfooding”. The idea that as a developer, you should use your own products on a daily basis, even during development. This exposes you to all the weaknesses and flaws of the product, and makes you much better equipped to deliver [...]]]></description>
			<content:encoded><![CDATA[<p>A term that’s become very popular, and which especially Microsoft’s developers seem to champion, is “dogfooding”. The idea that as a developer, you should use your own products on a daily basis, even during development. This exposes you to all the weaknesses and flaws of the product, and makes you much better equipped to deliver a product that’s actually <em>worth using</em>.
<span id="more-433"></span></p>

<p>But perhaps there’s a counter-argument that people seem to miss. If you use a lousy piece of software on a daily basis, <em>you get used to it</em>. You stop thinking about how it <em>should</em> be, and only consider <em>how it is</em>.</p>

<p>I think the first place I heard of the term “dogfooding” was on the <a href="https://blogs.msdn.com/windowsmobile/archive/2007/05/04/dogfood-doesn-t-always-taste-good.aspx">Windows Mobile team blog</a>. And let’s be honest, is Windows Mobile really a competitive product? Is it worth using? Perhaps in a vacuum. If all you know is Windows Mobile, then, well, it’s not <em>too</em> bad. But there’s an obvious reason why the product is struggling in the marketplace. Compared to <em>everything else</em>, it feels horrible to use.</p>

<p>Perhaps the recipe for fixing Windows Mobile would be <em>less</em> dogfooding. Windows Mobile developers shouldn’t be forced to use their own buggy, slow, in-development OS all the time on their phones. Perhaps they should be given iPhones and Blackberries. Perhaps some of them should even be given simple old-school non-smartphones. The ones that didn’t need to be rebooted, and didn’t “feature” load times for opening your contacts list, or to write a new SMS (text message). Perhaps they need to be shaken up a bit, and see what <em>else</em> a phone can feel like when you use it. Windows Mobile 6.5 might be better than WM6.0. But that’s not the competition they need to beat. They need to beat the iPhone, they need to beat Android, Blackberry and Symbian. So those are the products they should use at least as much as they use Windows Mobile.</p>

<p>The same may be true for Visual Studio. It’s great that the team <a href="https://blogs.msdn.com/ricom/archive/2009/10/19/my-history-of-visual-studio-part-10-final.aspx">uses Visual Studio 2010 internally</a> as much as possible during development. But that also means that they get used to its performance issues. And it means they get used to the assumption that “this is what an IDE is like”.</p>

<p>Perhaps Visual Studio would be a better product if the team was forced to use Emacs, Vim and Eclipse. Or perhaps even Notepad and makefiles.</p>

<p>And how much better would TFS be, if the developers had used Git or Bazaar instead of <a href="http://blogs.msdn.com/somasegar/archive/2007/06/18/so-what-does-microsoft-use-for-software-development.aspx">dogfooding</a> <a href="http://blogs.msdn.com/granth/archive/2009/08/27/vsts-pioneer-tfs2010-dogfood-server.aspx">TFS</a> during development?</p>

<p>Dogfooding has its advantages, certainly, but I don’t think it <em>alone</em> is a recipe for a good, competitive product. It leads to an incremental improvement over the previous version of your product, but it doesn’t take into account what <em>else</em> is happening in the world. It doesn’t give you the opportunity to question your basic assumptions<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>. Sometimes, incremental improvement is not what your product <em>needs</em>.</p>

<p>Just a thought.</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>Of course I’m not claiming that Microsoft’s developers <em>never</em> use or examine competing products. And likewise, there are obvious benefits to dogfooding, and I’m certainly not claiming that the practice should be eliminated. But I think it is telling that their blog posts frequently mention how heavily they dogfood their products. But they never mention “for this release of Visual Studio, we actually went back and looked at why many people still prefer Vim.”, or “In developing Windows Mobile 7, the entire team was issued phones running various other OS’es, and this taught us what we need to do to finally ship an OS that will take over the world”. <a href="#fnref:1" rev="footnote">↩</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2010/01/the-downside-to-dogfooding/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The meaning of RAII — or why you never need to worry about resource management again</title>
		<link>http://jalf.dk/blog/2010/01/the-meaning-of-raii-or-why-you-never-need-to-worry-about-resource-management-again/</link>
		<comments>http://jalf.dk/blog/2010/01/the-meaning-of-raii-or-why-you-never-need-to-worry-about-resource-management-again/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 05:00:52 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[raii]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=340</guid>
		<description><![CDATA[I tried really hard to come up with some witty title or pun to weave into the title of this post. I couldn’t. RAII is just a terrible name, and it isn’t really clever or funny. Unfortunately, it is also the single most important key to C++. It is not just an idiom but a [...]]]></description>
			<content:encoded><![CDATA[<p>I tried <em>really</em> hard to come up with some witty title or pun to weave into the title of this post. I couldn’t. RAII is just a terrible name, and it isn’t really clever or funny. Unfortunately, it is also <em>the</em> single most important key to C++. It is not just an idiom but a fundamental philosophy used to solve almost any problem in the language. So we can’t really avoid it.</p>

<p>If I had to pinpoint one thing that marked the difference between a skilled and an unskilled C++ programmer, it would be “do they understand RAII”. Many people don’t, hence this post.<span id="more-340"></span></p>

<p>RAII is, apart from being badly named, one of those deceptively simple concepts that you <em>think</em> you understand when you first hear of it, think “well duh, that’s obvious”, and then proceed to write code as usual, because you just don’t see how widely applicable it is.</p>

<p>But let’s get the name out of the way first. <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization">RAII</a> stands for “Resource Acquisition Is Initialization”. And if you’re not already familiar with the idiom, then this has told you <em>nothing at all</em>. If you did know about RAII in advance, then you can, when you stop and think about it, kind of see how the name relates to it… vaguely… sort of.</p>

<p>What it actually <em>means</em> is simple: Resources should be managed by classes. When the class is initialized, the resource is acquired (hence the name). When the class is destroyed, the resource is released. And the lifetime of the object should exactly match the desired lifetime of the resource. That sounds obvious, and many programmers will (assuming they’re working in a language that <em>has</em> classes), say that this is what they always do.</p>

<p>Often, C++ developers think this just means “smart pointers. Wrap your memory allocation in a <code>boost::shared_ptr</code> and you’re done”. I see that as one not-very-often used border case though, rather than a typical example of RAII. So let’s take a step back instead.</p>

<p>The key idea isthat any kind of resource, not just memory, but file handles, sockets, database connections, or even more abstract resources like loggers or profiling timers or textures, really <em>any</em> concept or process which has a lifetime, should be mapped to an object.</p>

<p>Unlike the typical object-oriented line of thought which goes that “everything must be an object, because then.… well, everything will be an object, and your code will be better”, here we actually have a concrete <em>reason</em>: We want to use the object to manage the lifetime of the resource.</p>

<p>When I allocate memory with <code>new</code>, I have to deallocate it again sooner or later, with <code>delete</code>. (Or in C, with <code>malloc()</code> and <code>free()</code> respectively). And I have to make sure that this is done. And I have to make sure that it is not done twice. And that the object is not accessed after this is done. There are a lot of constraints we have to obey, all related to the lifetime of the resource. And this is why unmanaged programs have a reputation of leaking memory left and right. If we allocate memory, and it is to be used by a dynamic number of objects or functions all referencing the same allocations, which of the users is responsible for deleting it? And how do we know when it is safe to delete, when no users remain?</p>

<p>Ironically, most managed languages have <em>not</em> solved the problem. They have added a garbage collector (which yes, is very useful for a wide number of reasons), but that only solves one specific instance of the problem. It takes care of avoiding memory leaks, but it doesn’t avoid resource leaks <em>in general</em>.</p>

<p>The garbage collector ensures that this code won’t leak memory:</p>

<pre><code>void foo() {
  SomeObject* obj = new SomeObject();
  bar(obj);
}
</code></pre>

<p>where without a garbage collector, we’d (at least without RAII) have to write code such as</p>

<pre><code>void foo() {
  SomeObject* obj = new SomeObject();
  try {
    bar(obj);
    delete obj;
  }
  catch(...){ delete obj; }
}
</code></pre>

<p>In the garbage collected case, we don’t know what <code>bar</code> does, and we don’t <em>need</em> to know. It doesn’t have to delete the object. And neither does the <code>foo</code> function. So we have successfully dodged the problem of managing the lifetime of memory allocations. We haven’t really <em>solved</em> the problem though. We still don’t have any good tools to <em>manage</em> the lifetime. We’re just guaranteed by the system that it’ll last <em>long enough</em>.</p>

<p>In C++, this effect can be approximated using some kind of smart pointer<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>.</p>

<p>Smart pointers allow us to write code like this:</p>

<pre><code>void foo() {
  boost::shared_ptr&lt;SomeObject&gt; ptr = new SomeObject();
  bar(ptr);
}
</code></pre>

<p>and be sure we won’t leak memory. Of course, this solution isn’t perfect — reference counting is much more expensive than a good garbage collector, and if we create cyclic references, the objects will never be deleted, as the reference counts never reach zero. It is a decent approximation, but nowhere near as good and reliable as the garbage collector in managed languages.</p>

<p>But the problem shows up again if we use another type of resource. What if we’d opened a database connection instead?
We’d have to write code such as this:
(The following Java-like pseudocode is copied almost verbatim from <a href="http://stackoverflow.com/questions/161177/does-c-support-finally-blocks-and-whats-this-raii-i-keep-hearing-about/161247#161247">this StackOverflow.com answer</a>, courtesy of <a href="http://stackoverflow.com/users/14065/martin-york">Martin York</a>.)</p>

<pre><code>void writeToDb()
{
  Db db = new Db("DBDesciptionString");
  try
  {
    // Use the db object.
  }
  finally
  {
    db.close();
  }
}
</code></pre>

<p>(And of course it gets even worse if <code>db.close()</code> can throw exceptions. Then we have to catch <em>that</em> exception, just to avoid it propagating out from the <code>finally</code> clause if we reached <code>finally</code> because of an exception being thrown in the <code>try</code> clause.)</p>

<p>The resource management problem still exists. We still have to wrap the code in exception handling just to make sure that the connection is closed as soon as we’re done with it. And we have to do this at <em>every</em> use. And it gets complicated fast.</p>

<p>Of course, .NET makes this a bit simpler:</p>

<pre><code>using (Db db = new Db("DbDescriptionString"))
{
  // use the database object.
}
</code></pre>

<p>But the onus is still on the user of the class to ensure it is closed correctly. There is no obvious way to encode into the <code>Db</code> class that “once we’re done with an object of this type, the connection must be closed immediately”.</p>

<p>And in C++, smart pointers are no longer suitable solutions, since the resource to be managed is no longer a pointer allocated with <code>new</code>.</p>

<p>Instead, a more basic flavor of RAII comes to the fore:</p>

<pre><code>void someFunc()
{
    Db db("DBDesciptionString");
    // Use the db object.
} 
</code></pre>

<p>Yes, that’s all. When the <code>db</code> object goes out of scope, at the end of the function, its destructor is called. The destructor internally calls <code>this-&gt;Close()</code> for us, so we don’t need to do it! We just have to trust the scoping rules of C++, which guarantee that destructors are called on local variables when they go out of scope, and on class members when the class is destroyed.</p>

<p>So in a sense, the key idea in RAII is simply that “resources should behave sensibly”. They should get copied safely if an assignment is made (or otherwise, assignments should be prevented), they should be available if their owning object is successfully created (if it can’t create the resource, it should throw an exception, aborting the creation of the object), and when they are no longer used, they should be cleaned up.</p>

<p>The C++ standard library class template <code>std::vector</code> is a wonderful example of RAII in action. The resources being managed by a <code>vector</code> are memory (the array allocated internally to hold the objects being contained in the vector, as well as the objects themselves. When the <code>vector</code> is destroyed, every object it holds must be destroyed too, and the array in which they were placed must be deallocated.</p>

<p>In the following examples, assume that a function <code>foo</code> is passed a vector of <code>MyClass</code> objects by value. We don’t know how many, if any, objects are stored in it, but since we are passed a copy of the original <code>vector</code>, we take ownership of it. It exists only in the function <code>foo</code>, and must be destroyed afterwards.</p>

<pre><code>void foo(std::vector&lt;MyClass&gt; vec) {
  ...
 //  when we get to the end of the function, all local variables, including vec, 
 // are automatically destroyed by having their destructors invoked.
 // So no matter how many MyClass objects were stored in the vector, it ensures that they too have their destructors called.
 // And the vector also deallocates its internal array, leaving neither of its resources alive at the end of the function
}

void foo(std::vector&lt;MyClass&gt; vec) {
  throw std::exception("Oops");
  // as above, vec is automatically destroyed when we leave the function,
  // regardless of *how* we leave it. Even if we leave it because an exception was thrown and not caught.
} 

void foo(std::vector&lt;MyClass&gt; vec) {
  // other is constructed as a copy of vec. std::vector ensures that both of vecs resources are copied as well
  std::vector&lt;MyClass&gt; other = vec;
  // we now have two vectors, each owning a dynamically allocated array and a number of MyClass objects
  // and again, at the end of the function, both are deallocated cleanly
} 

void foo(std::vector&lt;MyClass&gt; vec) {
  std::vector&lt;MyClass&gt; other; // a second, empty, vector

  // perform an assignment, setting vec to be an empty vector
  // std::vector makes sure that if you do this, the resources previously held by vec are cleanly released
  // before copies are made of the resources held by other
  vec = other;

  // and so when the function ends, the MyClass objects originally held by vec
  // have already been destroyed, so their destructors are *not* invoked now
} 
</code></pre>

<p>As the above shows, <code>vec</code> owns its resources, and manages them tightly. Whenever a change happens to <code>vec</code>, it reflects this by updating its owned resources. If it is destroyed, it destroys its owned resources. If it is copied, it copies the resources it owns. If it is assigned to hold something else, it first destroys its existing resources. And so on. Nothing you do can bring it “out of balance”. It just works. <em>That</em> is RAII. Smart pointers are just convenient adapters turning raw pointers into RAII objects. But RAII is much more than smart pointers.</p>

<p>It is the broad and general idea that <em>resources should be mapped to objects</em>, so that the object can not be created unless it succeeded in acquiring its resource, and it can not be destroyed without also releasing its resource. This effectively saves C++ programmers from having to worry about resource management.</p>

<p>Take an example that’s guaranteed to cause pain without the use of RAII: Handling exceptions being through halfway through constructors. Say you have a class with multiple members which are initialized in its constructor. After the first member has been initialized, but before all of them have been initialized, an exception is thrown. Let’s use the following contrived example:</p>

<pre><code>class Foobar {
  Foo f;
  Bar b;
  MyClass c;

public:
  Foobar() : f(42), b("hello world), c('a') {}
};
</code></pre>

<p>unfortunately, <code>b</code>’s constructor throws an exception. How to handle this? We know that in C++, partially constructed objects do not automatically have their destructors called. when the construction is aborted.</p>

<p>And since we want to avoid any resource leaks, we require that the following must happen:
– <code>a</code> must have its destructor called (because <code>a</code> was successfully initialized before the error occurrd)
– <code>b</code> must release any resources it acquired in its constructor before it threw the exception
– <code>c</code> must do nothing. Its construction was not yet begun when the error ocurred, so it would be an error to attempt any kind of cleanup of <code>c</code>.
– The <code>Foobar</code> object (the object pointed to by the <code>this</code> pointer) must ensure that the above, and nothing else, happens, and it must do so without relying on its own destructor (which won’t be called, as construction did not successfully complete).</p>

<p>And of course, pretending that only <code>b</code> can throw an exception may be a simplification over the real world. Perhaps every member could throw one from its constructor. Care to write a <code>Foobar</code> constructor which takes all this into account, providing enough <code>try</code>/<code>catch</code> blocks to correctly catch every exception that might be thrown, and release exactly the resources that have been allocated until then, and <em>nothing</em> else? A tall order, and an open invitation for bugs. And of course, it’d lead to a huge, bloated and error-prone constructor. It’d also prevent us from using the <em>initializer list</em>. We’d have to perform some kind of “safe” non-throwing default construction of both <code>a</code>, <code>b</code> and <code>c</code> before entering the constructor body, where exception handling is possible, and from there, attempt to perform assignments to bring the three members into the desired state.</p>

<p>In pseudocode, the constructor might look something like this:</p>

<pre><code>Foobar() {
  a = new Foo(42);
  try {
    b = new Bar("hello world");
  }
  catch {
    destroy a;
    throw;
  }
 try {
    c = new MyClass();
  }
  catch {
    destroy b;
    destroy a;
    throw;
  }
}
</code></pre>

<p>Note that all this complexity is only necessary because we want to handle several different resources. <code>a</code>, <code>b</code> and <code>c</code> all contain resources that must be attempted acquired, and properly released if this fails. If there’d been only one resource, the job would have been much simpler. There wouldn’t be any point at which <em>some</em> resources have been acquired, and others have not. If we succeeded in acquiring that one resource, there’d be no risk of errors occurring afterwards, so we wouldn’t need complex conditional cleanup code. And if we failed to acquire the one resource, there’d be nothing to clean up — after all, the resource was never acquired!</p>

<p>So to keep down the complexity, the only safe way to define a class is to make it own <em>at most one</em> resource. And this one-to-one mapping of resources to classes is exactly what RAII is all about. If <code>a</code>, <code>b</code> and <code>c</code> had all been RAII objects, then the above code <em>would work</em>. Regardless of which members could or couldn’t throw exceptions. According to the rules of C++, we know that in the above case,</p>

<ul>
<li>the <code>Foobar</code> destructor (<code>this-&gt;Foobar::~Foobar()</code> will not be called, as <code>*this</code> was not successfully constructed.</li>
<li>the <code>a</code> destructor will be called, as this member was fully constructed at the time of the error.</li>
<li>the <code>b</code> and <code>c</code> destructors will not be called, as these members were not fully constructed at the time of the error.</li>
</ul>

<p>So assuming that <code>b</code>’s constructor takes care of releasing any resources successfully allocated when the error occurred (the number of which, as pointed out above, should ideally be zero), we’re actually home free! What happens is exactly what we listed earlier as our goal. <code>a</code> has its destructor called, <code>c</code>’s constructor was never run in the first place, so it doesn’t have to do anything, and <code>*this</code> doesn’t have to do <em>anything</em> special in its constructor. All of its members take care of their own resources, so the number of resources managed by <code>*this</code> is zero!</p>

<p>We don’t even need to write a destructor for <code>Foobar</code> now, if all its members are RAII objects. Whether the <code>Foobar</code> object is partially or fully constructed, its members take care of themselves. That is the power of RAII. Once a resource has been mapped to a class, we can use it as much as we like, and even in very complex situations, and never have to worry about the resource being leaked. It is managed by its wrapping RAII object, and the C++ lifetime and scope rules ensure that this wrapper object gets destroyed when it goes out of scope</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>A smart pointer is an object which behaves as a pointer (meaning that it overloads the <code>*</code> and <code>-&gt;</code> operators, so it can be dereferenced to yield the pointed-to value), but also enforces some kind of ownership semantics on the value. A plain pointer does nothing when it goes out of scope. If it pointed to some dynamically allocated memory, nothing happens to that memory. And if no one else have a pointer to it, then that memory is lost, and can not be reclaimed.
A smart pointer does <em>something</em> when it is destroyed. Some variants simply free the memory they point to (<code>boost::scoped_ptr</code>, <code>std::auto_ptr</code> or <code>std::unique_ptr</code> all fall into this category, although with some important differences), while others implement reference counting, so that the memory is only destroyed when <em>all</em> smart pointers pointing to it have been destroyed. <code>boost::shared_ptr</code> is by far the best known implementation of this concept. <a href="#fnref:1" rev="footnote">↩</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2010/01/the-meaning-of-raii-or-why-you-never-need-to-worry-about-resource-management-again/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
