Monday, September 24, 2007

The joys of System.Diagnostics.StackFrame

I see code like this an awful lot where a logging function or whatever is passed a string literal of the current method name:
public void TheOldMethodUnderTest()

private void Log(string methodName)
    Console.WriteLine("The name of the method was: {0}", methodName);
I've also seen a lot of code like this where the actual method name and the string literal have become out of sync, either because of refactoring, or more commonly, the cut-and-paste method of code reuse. Fortunately there's a really easy way of getting the current stack trace from the System.Diagnostics.StackFrame class. The code below does exactly the same thing as the code above, but the Log() function works out for itself what its caller's name is:
public void TheMethodUnderTest()

private void Log()
    System.Diagnostics.StackFrame stackFrame = new System.Diagnostics.StackFrame(1, false);
    System.Reflection.MethodBase method = stackFrame.GetMethod();
    Console.WriteLine("The name of the method was: {0}", method.Name);
Doing this kind of thing is a legitimate requirment in many applications, but if you find yourself having to put a lot of boiler-plate code in every method because of cross cutting concerns such as logging, it's worth looking at some kind of AOP framework that provides dynamic proxies that you can intercept.

Thursday, September 13, 2007



I had a great time in London over the last couple of days at MIX 07 UK. Most of the excitement was about Silverlight, but there was also a lot of coverage of VS2008. I've been reading a lot about Silverlight recently, but this was the first time I'd seen a good demonstration of it in use. It's obviously Microsoft's Flash killer and it fills a big gap in RIA application development where you'd currently have to use Flash. For .net developers like me it opens up some great opportunities, so it can only be good. I guess its success will primarily hinge on how well Microsoft drives adoption. Scott Guthrie explained that they only have to persuade about 15 of the top sites to use it and it will become ubiquitous. One thing that really struck me, that I hadn't properly grepped before was how core XAML is to the current MS development stack. I really liked the way you could use the Expression * tools to move XAML projects between designers and developers. Those tools (especially Blend) are yet another thing I need to play with.

I went to the second of the VS2008 sessions, but it was pretty much covering stuff I already knew, although it was nice to see all the LINQ to SQL stuff presented. One thing that was new was the Rails style scaffolding stuff. I don't remember what it was called, but MS have blatantly got their eyes on the Ruby camp. Only for the best I think.

I was also really looking forward to the 'Why IronRuby', and 'IronPython et al' sessions. Dynamic languages are generating some real excitement these days and was very keen to get some insight into how Microsoft sees them playing with .net. 'Why IronRuby' by Dave Verwer was a bit of a disappointment, not because Dave didn't do an excellent job introducing Ruby, but I was rather hoping for a more philosophical discussion about dynamic languages in the .net world and how well they play with the existing MS technology stack. I had to leave before the questions, so maybe there was some interesting discussions? I hoped to grab Dave for a chat later on, but I couldn't track him down. Michael Foord's (aka Fuzzyman) IronPython session was much more what I wanted. Michael spent a lot of time digging into the intricacies of building dynamic languages on top of the CLR, the DLR and some of the cool stuff you could do with IronPython and Silverlight. I'm kinda seeing what the Python and Ruby people are saying about the benefits of dynamism, but I'm still not quite ready to really embrace it yet. The opportunity will  come when the IronRuby implementation has been fully developed in a year or so, but I must stay, the DSL story from the Ruby camp is very compelling.

My favorite session was the 'A nice cup of tea and a sit down', a panel discussion with Scott Guthrie. As you probably know, Scott is the general manager of the .net development group and his blog is always my first stop to keep up with what's happening at MS. It's not everyday that you get to fire some questions at one of the core .net guys, so I really enjoyed myself.

I asked Scott about TDD at Microsoft and how they saw TDD fitting in with their products. I was especially keen to hear what he thought about the problems with mocking the BCL.

He told us about a new MVC framework that's in the pipeline for ASP.NET that sounded very intriguing and how they were consciously trying to keep it interoperable with open source tools. I was especially gratified to hear him mention Windsor and RhinoMocks.

It's a shame there wasn't more time, I really wanted to ask him about functional programming and LINQ and how explicitly MS are going to encourage more declarative programming styles in the BCL and documentation. Right now the message seems to be that LINQ is query and the functional declarative stuff is only mentioned if you're prepared to dig deep.

Saturday, September 01, 2007

Microsoft and Innovation

I love reading Scott Hanselman's blog. I missed a great post back in May, 'Is Microsoft losing the Alpha Geeks?' In it he talks about his recent visit to RailsConf where he was very impressed by the energy of the Ruby-on-Rails (RoR) community. Unless you've been avoiding the blogosphere for the last couple of years, you'll know all about the incredible buzz around RoR. It's certainly taking a lot of the most respected people in the development community along with it, not least of all Martin Fowler and his company Thoughtworks. So Scott asks if Microsoft has lost it's way; is it 'losing the Alpha Geeks?' So what's an Alpha Geek? Let's assume for the sake of argument we're talking about the guys who actually move the industry forward, not just the people who really have a deep understanding of the technology, but the people who invent it. I don't think Microsoft are loosing them, because I don't think they ever had them in the first place. Let's step back and think about innovation for a while. How does technology, or even in the broader sense, culture, change and advance? What kind of conditions promote innovation and which stifle it? The great thinker in this field was the economist Joseph Schumpeter, he coined the phrase 'creative destruction', to describe the driving force of innovation in a capitalist economy. Essentially the rate of innovation is proportional to the number of people able to innovate; the number of minds allowed to tackle a particular question and the ability of individuals or groups to attempt to profit from their idea without a severe cost of failure. Capitalism thrives on huge numbers of small companies and entrepreneurs constantly trying and failing. A Darwinian survival of the fittest where the vast majority do not succeed. Indeed it goes beyond capitalism, Jarred Diamond's seminal work 'Guns, Germs and Steel', brilliantly shows how human development progressed in direct proportion to the size of human communities and the speed at which technological developments could be propagated. When you get bored of 'learn WPF in 21 days' it's must read! The software industry is a pure manifestation of Schumpeterian economics. The barriers to entry are almost non-existant; anyone with an idea can have a go. It doesn't really make a lot of difference if you're a highly paid Microsoft employee or a Gujarati teenager with a computer, the quality of your ideas is the only thing that matters. In fact there are two good reasons why the Gujarati teenager has an advantage: Firstly he doesn't have to persuade his line manager that his idea is a good one before he's allowed to explore it; he can have a go at any hair brained wackiness he likes. His idea doesn't have to play nicely with any particular corporate strategy or favorite technology. Secondly, and conversely, he has to persuade the whole world that his idea is a good one. The Microsoft guy, once he's persuaded the company, will have the whole weight of Microsoft's marketing behind him and millions of Microsoft shops around the world will use his technology even if it's sub-optimal. But because the Gujarati teenager has none of this support his idea will only thrive if it's a really really good one. So Microsoft's 1000's of highly paid geeks verses who knows how many millions of the rest of the world's programmers? It's no competition, the cutting edge will always be outside of Microsoft or any other corporation for that matter. I think Microsoft collectively knows this and the company has become a lot more responsive to good ideas coming out of the world wide software community. The new openness of public betas and shared source initiatives are evidence of this. Microsoft has also become much better at picking the best talents from the community, just take the example of John Lam. So what does this mean for the average Microsoft Mort like you and me? Well, if you're interested in the state of the art and you want to see what the software world might look like in the next five or ten years, then you've really got to raise your eyes from the MSDN front page. You'll almost never see anything truly innovative there. And even when you do see something new there, there's no guarantee at all that it's a good idea. It hasn't been filtered by the community yet. When Microsoft has adopted something, it's not the cutting edge any more it's the mainstream. Take an interest in what's happening in the rest of the software world and occasionally try out something that's nothing to do with Redmond. Ruby on Rails obviously has something good going for it or there wouldn't be a whole lot of very clever people banging on about it all the time. Now this doesn't mean that you or I should suddenly abandon Visual Studio and everything we know for a Linux box, Emacs and a command line, but it does mean that it's worth keeping an eye on similar things that might be evolving in the .net ecosystem, like Subsonic or Monorail. And don't assume that ASP.NET is the only and best web development framework. I hope this doesn't sound like I'm Microsoft bashing, indeed my whole career (and it's been a relatively prosperous and happy one so far) is built around Microsoft. I'm very happy with the Borg's new found openness and these days there's such a flourishing open source community around the Microsoft tools, especially .net, that you don't have to wait for corporate adoption before being able to integrate the best modern development techniques into your work.