Thursday, 27 November 2008

Conquering VB envy with C#4.0

Whilst your average C# programmer probably considers himself superior to those poor people who habitually use VB.Net, he is at the same time silently envious of one or two (but certainly no more than that!) features of the deluded ones' language. Concise, hassle-free, and above all, Type.Missingless COM Interop for one. And deep XML integration for two. But with C#4.0, and Anders' masterly introduction of objects that can be statically typed as dynamic, that will change. Gone will be all envy; in its place, pure pity. ;-) Here's one example for why.

I finally found time (we've just finished our iteration), and an excuse (I've got to give a briefing on PDC), to delve into my smart black USB hard disk ("the goods") that I brought back from the PDC, and to fire up the Virtual Hard Drive where lives Visual Studio 2010 and C# 4.0. As an aside, I'm running it under VirtualBox, rather than Virtual PC, without any difficulties at all, and with very good performance.

In Ander's presentation about the Future of C# he showed a simple example of how the dynamic features of C# 4.0 could be used to implement a property bag, where you could write (and read) arbitrary properties on the bag and it would store whatever you assigned to it. The punch line was that he implemented it in a couple of lines of code.

That gave me the idea that it shouldn't be too difficult to simulate something like VB.Net XML integration, where you can take an XML object, and access its attributes and elements as if they were properties of the object. And it wasn't. Now don't get too excited: I spent about fifteen minutes on this, but the outcome should be enough to whet your appetite.

First, the end-result:

static void Main(string[] args)
{
    var xml = "<Pet Type='Cat' Name='Leo'><Owner Name='Sam' Age='27'/></Pet>";
    dynamic dynamicXml = new DynamicXElement(xml);

    Console.WriteLine("Name={0}, Type={1}", dynamicXml.Name, dynamicXml.Type);
    Console.WriteLine("Owner={0}, Age={1}", dynamicXml.Owner.Name, dynamicXml.Owner.Age);
    Console.ReadLine();
}

In line 4 I'm creating one of these new-fangled dynamic object thingies, declaring it with the magical dynamic keyword. The dynamic keyword tells the compiler to do all member resolution on the object at run time, rather than compile time. And how does it do the member resolution? That's where the DynamicXElement comes in. The DynamicXElement is equipped with the right knobs and levers so that it can participate in dynamic resolution. It must be tremendously complicated then? You'll see in a minute.

Lines 6 and 7 show off the amazing capabilities of this DynamicXElement. Having supplied it with some XML when we initialised it, we can now access values of attributes just as if they were properties of the element. Likewise with child elements and their properties. Isn't that exciting? Doesn't it deserve some applause (as the more desperate of the PDC presenters might say!)?

So how does dynamic binding work? I won't pretend to you that I understand it fully at the moment. What I do know is that if you want to have a say in how member resolution works, your objects have to implement IDynamicObject. I also know that in .Net 4.0 there will be a base implementation of that interface called DynamicObject (follow the link to get the code for it); if you inherit from that it takes care of the minor complications for you, leaving your code to be as simple as:

class DynamicXElement : System.Dynamic.DynamicObject
{
    XElement _xml;

    public DynamicXElement(string xml)
    {
        _xml = XElement.Parse(xml);
    }

    public DynamicXElement(XElement element)
    {
        _xml = element;
    }

    public override object GetMember(System.Scripting.Actions.GetMemberAction info)
    {
        var attribute = _xml.Attribute(info.Name);

        if (attribute != null)
        {
            return attribute.Value;
        }

        return new DynamicXElement(_xml.Element(info.Name));
    }
}

As you can see, I only need to override one method, GetMember, to give me the control I need of dynamic behaviour (there are other overrides available if you want to handle calls to property setters or methods). This method is called whenever the runtime wants to resolve a call on a property getter. The GetMemberAction object that is passed to the method contains a Name property giving, unsurprisingly, the name of the property that's being asked for. In my ultra-simple implementation, I'm using that to first check for the existence of an attribute with that name; or, failing that, assuming that it must be the name of a child element. I'm returning the child element in a DynamicXElement wrapper so its attributes and children can be accessed in the same dynamic way.

Simple wasn't it?

But please look gently at this code. I know that it will fall to pieces in anything approaching a real-world situation (like when an element has an attribute and a child element with the same name). If you want to see some more substantial examples, look at Nikhil Kothari's blog. Here's his dynamic JSON wrapper, for instance.

Friday, 21 November 2008

Null Reference checking, functional style

I love Extension methods. I only wish I could pull one out of my computer, so that I could give it a great big hug. Anyway ... That little outburst was prompted by the way that extension methods helped me out with an elegant solution when I needed to do a null reference check on a variable.

We've all written code like

variable = SomeMethodThatMightReturnNull();

if (variable == null) 
{
	throw new ExceptionIndicatingTheNullness();
}

which is alright, but don't you think there are too many curly braces there? You don't? Well, I suppose there are only two, but wouldn't it be more elegant if there were none?

I've written before about a nice feature of extension methods: because they are effectively static methods you can call one on a null reference, and it doesn't blow up. Instead it just passes the null value through into the method.

So we can create this:

public static class FluentMethodExtensions
{
    public static T EnsureNotNull<T>(this T instance, Func<Exception> exceptionBuilder) where T : class
    {
        if (instance == null)
        {
            throw exceptionBuilder();
        }
        return instance;
    }
}

(Note the where constraint on the method (line 3): we need that so that we can do the null comparison in line 5.)

With that in place we can zap those curly braces:

variable = SomeMethodThatMightReturnNull().EnsureNotNull(() => new ExceptionIndicatingTheNullness());

How about that then?

Of course, the usual reason for throwing your own custom exception is so that you can include helpful details about the problem. In that case you might want to create some overloads to EnsureNotNull. How about this one, intended to allow you to pass through parameters so that you can build them into a message using string.Format:

public static T EnsureNotNull<T>(this T instance, Func<object[], Exception> exceptionBuilder, params object[] exceptionMessageParameters) where T : class
{
    if (instance == null)
    {
        throw exceptionBuilder(exceptionMessageParameters);
    }

    return instance;
}

You use it like so:

// ...
Range range = GetRange(workbook, rangeName)
                .EnsureNotNull(BuildInvalidRangeNameException, rangeName);
// ...

private ExcelException BuildInvalidRangeNameException(object[] exceptionParameters)
{
    return new ExcelException(string.Format("Named range {0} does not exist.", exceptionParameters));
}

You can create your own. Go on. Try it. It's fun.

Thursday, 20 November 2008

Scary MSBuild log

This is not what you want to see in your MSBuild log:

UndesirableMsBuildLog

This unwelcome tale appeared in my log because I had this Target in the project file:

<Target Name="DeleteOldResultFiles">
 <ItemGroup>
         <LogFile Include="$(ReportsDirectory)\*.*"/>
   </ItemGroup>
   <Delete Files="@(LogFile)" TreatErrorsAsWarnings="true"/>
</Target>

Unfortunately, I had miss-typed the $(ReportsDirectory) property as $(ReportDirectory), and said miss-typed property did not exist, so MSBuild was defaulting to the empty string. Thus the LogFile Item was being populated with a list of all the files in my C:\ drive, and the Delete task was obediently purging them.

The moral of this story: don't run MSBuild as Admin - or if you're as foolish as I was, make sure you've at least got a cloned Virtual machine (as I had) from which to copy any files which might get trashed.

Wednesday, 19 November 2008

New syndrome identified: Pre Posting Tension

A little while back, I wrote that a beloved member of our household is suffering from a rare and incurable condition: Sudden Onset Digital Amnesia. Now I have diagnosed that I myself am afflicted with a syndrome previously unknown to Medical Science: Pre Posting Tension. As my contribution to the greater good of mankind, I will, in this post, catalogue the symptoms of this condition. If you recognise yourself as a fellow sufferer, please get in touch: we may be able to form a support blog.

A cycle of symptoms

First, realise that the symptoms come in cycles. The trigger seems to be the discovery that somebody, anybody, has linked to an article on my blog. This produces a feeling of euphoria. If the link is from a high page-rank site like DZone (thanks mswatcher!) the euphoria is elevated to near ecstasy, increasing with every up-vote received.

The excitement is short-lived, however. Soon after closing the Feedburner Site Stats window bearing the good tidings of incoming links, a great wave of worry and doubt sweeps over me. Will I be able to find material for a follow-up post? Can I again craft new and interesting phrases to describe my future subject matter? Will the visitors return? How many of them will subscribe, and how soon will they unsubscribe if the quality of posts diminishes?

Then the nervousness begins, increasing with every hour that passes unblogged. Glimpsing Windows Live Writer in my Start Menu, or the Blogger icon in my Favourites list causes me to tremble with anxiety. Every line of code written is scrutinised for post potential, every fleeting thought examined for article-worthiness.  Then a plateau is reached when inspiration dawns: my mental state stabilises as words and phrases begin to congregate together in my mind.

Once fingers begin tapping keyboard, tension eases somewhat. But woe-betide anybody who interrupts, because this is when irritability sets in; concentration is total and all else is forgotten as words bed themselves into the page. If body is dragged away from the keyboard, mind remains at work - resulting in responses even shorter than the usual, manly, grunts when questioned. Internal pressure again builds up until the words get a chance to escape onto the page. Then, disaster. My train of thought comes up against a red light: the flow of words dries up. Writer's block has set in. Panic takes hold: visitor numbers will surely be dropping off by now. Only fresh content can restore them, and fresh content is held up in the sidings of my mind.

I fumble for words, and gradually the stream of thoughts begins again. The post rumbles on to completion. I check it over and over again, trying to winkle out the obvious errors that I no are lurking their[1]. Then fresh doubt springs up. What if readers don't like it? What if I've written something senseless? Maybe it doesn't hang together. More often than not, I answer my self-doubt with Pilatean response: "What I have written, I have written", and hit "Publish", before I beat myself up any further.

Relief comes flooding over me, as the post flashes up on my blog. But what's this on my Browser Toolbar? A shortcut to Google Analytics? I wonder whether anybody's read that article yet...

Afterword

My wife proof-read this, and commented that it would be amusing if it wasn't true. My protest that it was all exaggerated for effect met only with a dismissive "Puh"!

Footnotes

  1.  Sic - in case it wasn't obvious from the context!

Friday, 14 November 2008

Passing around references to events (almost)

Have you ever wished that you could pass a reference to an event on an object in C#? Perhaps you wanted to tell another object to monitor an event on something that you own. How do you say that in C#? If you've used C# 3 for any length of time you'll know that, through a magic combination of lambda expressions and Expression trees, it is possible to pass around references to properties and methods in a nice, type-safe, refactoring-proof way. But up till now I've not read of anyway to do the same for events1.

I've been doing some thinking about this recently, and I think I've invented a nice solution that will work in many cases. One case where I needed it was when creating a class to help me unit test events. I wanted an easy way to monitor a class, prod it and poke it in various ways, and then check that it raised the appropriate events. I came up with the EventAsserter. Hopefully I'll be able to share this with you in its full glory before long, but for now, I'll show a prototype as an example of using my event-passing technique.

Getting Hooked

The assumption is that you are calling a method, and want to be able to tell that method to hook up to an event of your choosing on an object that you specify. The basic idea is for you to pass into the method a delegate that allows it to call you back with its own event handler for you to hook up appropriately.

It looks like this:

using System;
using System.Diagnostics;

namespace EventMonitor
{
    class Program
    {
        static void Main(string[] args)
        {
            var eventRaiser = new EventRaiser();
            var eventAsserter = new EventAsserter<EventArgs>(handler => eventRaiser.TestEvent += handler);

            eventRaiser.RaiseEvent();

            eventAsserter.AssertEventOccurred();
        }

        class EventRaiser 
        {
            public event EventHandler<EventArgs> TestEvent;

            public void RaiseEvent()
            {
                TestEvent(this, EventArgs.Empty);
            }
        }

        class EventAsserter<TEventArgs> where TEventArgs : EventArgs
        {
            bool _eventOccurred;

            public EventAsserter(Action<EventHandler<TEventArgs>> attacher)
            {
                attacher((object sender, TEventArgs e) => _eventOccurred = true); ;
            }

            public void AssertEventOccurred()
            {
                Trace.Assert(_eventOccurred, "Event did not occur");
            }
        }
    }
}

It's a very simple example. I've got a dummy class, EventRaiser, that exists only to supply and raise an event. Then there's my prototype EventAsserter. This needs to be a generic type (taking the Type of the EventArgs from the event that you're wanting to handle) so that it knows what kind of event handler to supply: that's what it does in its constructor. It receives a delegate, and it calls this delegate back, passing through an event handler, and it expects that the delegate will hook the handler up to the correct event. Finally, the Main method ties everything together. As it constructs a new instance of EventAsserter  (in line 11) it hooks it up to the Test event of EventRaiser. Then it fires the event, and checks that it was indeed raised.

A Fallback solution for awkward cases

The sharp-eyed amongst you will have immediately spotted the flaw in my cunning plan: this will only work if the events that you are interested in have been declared using EventHandler<T>. This ought to be the case in a lot of code written since .Net 2.0, when the generic EventHandler was introduced. But what if you have events using custom delegates - PropertyChangedEventHandler for example, like the modified version of EventRaiser below?

You could special-case the technique for each type of event you want to handle, creating an overload for each type of EventHandler. Perhaps a nicer solution is to use the adapter pattern:

// ...
var eventAsserter = new EventAsserter<PropertyChangedEventArgs>(
                handler => new PropertyChangedEventAdapter(eventRaiser).PropertyChanged += handler);
// ...

public class PropertyChangedEventAdapter
{
        public event EventHandler<PropertyChangedEventArgs> PropertyChanged;

        public PropertyChangedEventAdapter(INotifyPropertyChanged source)
        {
            source.PropertyChanged += HandleEvent;
        }

        private void HandleEvent(object sender, PropertyChangedEventArgs e)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(sender, e);
            }
        }
}

Or, you could consider this solution below: it's less elegant, and encapsulation takes a bit of a beating, but workable as far as I can see.

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace EventMonitor2
{
    class Program
    {
        static void Main(string[] args)
        {
            var eventRaiser = new EventRaiser();
            var eventAsserter = new EventAsserter<PropertyChangedEventArgs>(
                asserter => eventRaiser.PropertyChanged += asserter.HandleEvent);

            eventRaiser.RaiseEvent();

            eventAsserter.AssertEventOccurred();
        }

        class EventRaiser : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;

            public void RaiseEvent()
            {
                PropertyChanged(this, new PropertyChangedEventArgs(""));
            }
        }

        class EventAsserter<TEventArgs> where TEventArgs : EventArgs
        {
            bool _eventOccurred;

            public EventAsserter(Action<EventAsserter<TEventArgs>> attacher)
            {
                attacher(this);
            }

            public void HandleEvent(object sender, TEventArgs e)
            {
                _eventOccurred = true;
            }

            public void AssertEventOccurred()
            {
                Trace.Assert(_eventOccurred, "Event did not occurr");
            }
        }
    }
}

In this case the EventAsserter has to make its handler public. Then in its constructor it passes itself to the attacher delegate. The calling code then needs to know that it has to hook the HandleEvent method to the event that it is interested in. Not pretty, I know; but pragmatic at least. It just goes to show the benefit of using EventHandler<T> over defining your own custom delegates.

What do you think? Can you see yourself making use of this? Any refinements that you can suggest?

Footnotes

1. If you try assigning a delegate to an event within a lambda expression that is converted to an Expression tree you get the error "An expression tree may not contain an assignment operator".

Thursday, 6 November 2008

Careers Presentation - Mathematicians should consider Software Development

Yesterday I was invited back to Birmingham University, from whence I graduated five years ago, to give a careers presentation. I obviously haven't moved around regularly enough to get off the mailing lists! They wanted me to talk to the students about how my Maths degree is helping my career in software development.

Here's an only slightly edited version of what I had to say:

"I was in Los Angeles at a conference last week. You might have heard about this conference in the news: It was the one where Microsoft announced the next versions of  Windows. I wish I had time to tell you about the cool stuff I saw: Windows 7, Windows Azure, Oslo, Dublin, and a whole bunch of other code-names; but I haven't.  I mention it in only in passing because that was how I came to use Skype for the first time ever, talking to my wife and daughter back here in England. I might be a geek,  but I am a bit slow when it comes to getting the latest gadgets.

Skype is amazing: it's easy to setup, no need to remember a phone number – I just had to enter my wife's name to connect with her,  sound quality was crystal clear. And even when you call from the States, it's free - so I bet you students have all used it? Have you ever wondered how they did that? How they make Skype work? Or maybe it is that you've played a game on the Wii, or Playstation 3, and wished that you could create something like that.

If you become a Software Developer you might just get the chance to do just that: to work on the next Facebook, the next Ebay, the next Skype. Or maybe you won't work on anything so glamorous, but for your customers, whoever they are, your work might be just as significant - even more important - in getting their work done. That is part of the privilege and responsibility of being a developer: you can make the difference between a good day and a bad day for whoever is using your software.

My Background

I started working at Paragon five years ago - one week after my honeymoon, and two weeks after my final fourth year presentation. I started as a Junior Developer, responsible for implementing software that somebody else had designed, with a lead developer taking charge of all the project management. Over time I was given more responsibilities: like talking to clients to find out what they wanted their software to do, and leading other people in designing it. In the last few years I was doing more and more of the project management, and less and less writing computer code - I was getting withdrawal symptoms, and had to start doing projects in the evenings to stay sane! So I asked to be moved back in the other direction: now I am responsible for designing the new product that we're developing, writing code and leading the development team (it’s a huge team -  of two people - including me!). Someone else has taken charge of keeping the project on track.

I'll be honest, and say that my job isn't really very glamorous - I don't jet off to conferences every week. A lot of the time I sit at my desk: much of that time I'm typing away at the keyboard - sometimes I just stare into space - thinking and daydreaming - but all related to the project of course. Depending on the projects I'm working on, I occasionally have to meet up with clients and give presentations about our work, or lead discussions with them about what they want doing. One of the most exciting parts is at the start of a project, when all we have is a blank whiteboard, and we brainstorm together [sorry, that's not politically correct: I should say, we have an idea shower together - hmm, sounds worse!] to decide how we are going to solve the problems that we face. It's can be daunting, but also very satisfying once we've cracked it.

The Joy of Software Development

So what do I like about being a Software Developer:

  • There are always new things to learn. The number of new technologies is increasing at an exponential rate – at least, that's how it feels.  Companies like Microsoft and Google are bringing out new Software, and new Software Development Kits all the time. I do a lot of work with the Microsoft .Net Framework, and when it first came out, I thought that within a few years I could become an expert in using it. Now I reckon that it isn't possible for somebody to become expert in more than a 1/10th. You'll never run out of new things to explore.
  • There are always new problems to solve. Computers are getting more and more powerful, yet easier to program. They are being used to tackle an ever wider variety of challenges. At the conference last week, the head of Microsoft Research showed us just a few examples of things they've been working on. Software like the Worldwide Telescope, that anybody can download: it stitches together detailed pictures from observatories all over the world so that anybody can explore the universe from their armchairs. Even amateur astronomers have been using it to make new discoveries. Other new areas include Computational Biology - decoding the human genome, investigating cures for HIV, all with software; Robotics, Social software - applications like Facebook that scale up to connect millions of people; right down to creating computer games that teach kids how to program.
  • There are opportunities do so something significant, to make a direct impact on peoples lives. Paragon is only a small company, yet I've been able to work on a couple of pieces of software that are used to manage the clean-up operation at a major nuclear site.
  • Software development lets you turn dreams into reality fast. An Architect can imagine a building - but then he has to draw up his plans, and get approval for them; and he can’t complete the project without builders, who'll probably make a mess of his fine design, and could take years to complete the project. With software you can imagine something, and code it up straightway and get immediate feedback - never more so that than with tools we have today, which do the hard parts bits like managing computer memory or putting fancy graphics on screen, leaving you to do the really interesting parts.
How Maths Helps

As you can tell, I think that being a Software Developer makes for a great career. But how did my Maths Degree help prepare me for it?

  • Most importantly, it taught me how to think. Learning mathematics trained me to think precise, logical, step -by-step thoughts: the only kind that a computer understands
  • During my time at Birmingham, I learnt how to solve problems. I remember Problem Workshops - Friday Afternoons, up in the Maths Library. There I learnt how to strip the fluff away from a problem, to distil it down to its essence. I grew adept at looking at a problem from different angles - if we squint at it, does it look like a different problem, one that we do know how to solve?
  • And, of course, there are areas of mathematics that I learnt on specific courses that have proved to be very useful. Computers do a lot of Boolean algebra: And, Or, Not. Knowing your truth tables will stand you in good stead. Graph Theory is another thing that comes up a lot. Perhaps you've already noticed, but Facebook is one big Graph: the people are the nodes, and every time you send a friend request you are inviting that person to create an edge between you. In my job, I've found the stuff I learned about Operations Research to be really helpful. In fact, one of my first jobs was to implement software that used meta-heuristics like Simulated Annealing, Tabu search and Genetic algorithms.
  • Lastly, you'll be glad to know that the projects that they make you write, and the presentations that you have to do all help. It's very strange, but before clients will give you money to do some work, they insist that you explain to them what you are going to do, and why it will be good value for money. And when you've built the software for them, for some reason they want to know how it works and how they should use it. And you never know: in five years time, you might be invited back to give a careers presentation!
What to do next

So if I’ve convinced you that Software Development is something you’d like to look into for a career, what should you do? The main thing is: try it out, get some experience, build up a portfolio of work that you can show to a potential employer. There aren’t many people who would volunteer for you to try out your brain surgery skills on them; and you can’t practice rocket science in your back garden. But anybody with a computer can get experience in being a Software Developer. There’s lots of free software out there to help you. In particular, I would recommend looking at projecteuler.net, a site where there are over two hundred mathematical problems which mostly can only be solved by writing a computer program: it’s an excellent way of building up your skills in the two areas. Then have a look at my blog: blog.functionalfun.net, where I’ve written up some of my solutions – and talk about software development in general.

The other thing I’d recommend is to find yourself a job working as a software developer during your summer holidays. I know that a lot of companies are interested in finding new talent, and offering vacation jobs is one of the best ways to do it; they often have little projects that they want doing, but don’t have time to do the work themselves. Find companies that are doing things you’re interested in, writing them a nice covering email along with your CV explaining all the hobby projects you’ve worked on, the experience you’ve built up, and I’m sure somebody will snap you up. Keep pestering them until they do - you've nothing to loose! It sure beats stacking shelves or working in a call centre.

I did this, and got jobs for all three of my summer vacations, and I got job offers from both the companies that I worked at; Paragon was one of those companies. They even sponsored me during my fourth year, which was very handy.

So, to finish: now is a very exciting time to become a Software Developer. It’s a very fulfilling and worthwhile career, with lots of scope for creativity. Your mathematical training will equip you well, and give you a big head-start. And you don’t need to take my word for it - download the software, and try it out for yourselves."

Resources

As well handing out company pens, and squeezy, stress-relieving light bulbs, I handed out a sheet listing a few useful websites:

Mathematics and software

http://projecteuler.net: Mathematical challenges to solve by writing software
http://blog.functionalfun.net: Samuel Jack’s blog about software development, and solving Project Euler

Software Development

http://www.eclipse.org/: Free Integrated Development Environment for writing Java software
http://www.microsoft.com/express/: Free Integrated Development Environments for creating software for the Microsoft .Net platform.
http://msdn.microsoft.com/en-us/xna/default.aspx: Free platform for creating games for Windows and the Xbox 360

http://stackoverflow.com/: Great site for getting answers to programming questions on all platforms
http://sourceforge.net/: Repository of Open Source Software, many projects welcoming new contributors
http://codeplex.com/: Open Source software for the Windows platform.

http://blogs.msdn.com/coding4fun/: Lots of interesting software projects to try at home
http://www.codinghorror.com/blog/: Great blog about software development

Tuesday, 4 November 2008

Passwords for the Virtual PC images on the PDC 2008 Hard disk

Yesterday I asked Google if it knew the passwords for the Virtual PC images that are on the Hard disk that they gave out at Microsoft PDC 2008. It didn't, so, forthwith, I shall teach it.

Visual Studio 2010 CTP

User Account Password
TFSSETUP 1Setuptfs
Administrator P2ssw0rd
TFSREPORTS 1Reports
TFSSERVICE 1Service

OSLO/Dublin/WF/WCF

Username: Administrator
Password: pass@word1

Update: if you are typing the password on a UK keyboard, you might find that the @ sign needs to be swapped for a " sign.