Rant of the day: IDisposable

My colleagues are probably used to the fact that I rant about code quality frequently. I take code quality very seriously. Not because I’m especially expert in it, but because features of basic code quality make it easier for other people to read and maintain the code.

Today’s irritation comes from some code (replicated in a number of classes I might add) that implements IDisposable. It is a fine interface and by implementing it you are telling the rest of the world that you have some stuff that can’t just be left to the garbage collector to clean up. These are things like file streams, database connections, etc. Any type of scarce resource that you want to hand back as soon as you are finished with it rather than leave it up to the garbage collector.

However, I came across this “gem” in some code today where the class, basically a utility class, contained no fields (so it wasn’t holding on to anything at all, let alone anything that might be a scarce resource). Yet, for some reason it implemented IDisposable. What was it going to dispose? What could it dispose?

The answer was in the code:

public void Dispose()
{
    // Nothing to dispose of.
}

Quite!

Developer Day Scotland 2009

Developer Day Scotland - 2nd May 2009There is less than 2 weeks until Developer Day Scotland 2009. There are still a few places left so if you haven’t got one, now is your chance. If you’re still not sure then read on.

Developer Developer Developer events are founded on a number of principles which I think makes it unique and well worth a single day of your time once a year.

DDD events are free. It doesn’t cost you anything to attend. Think of any large conference that you might like to go along to. They costs hundreds of pounds, if not more. Sure, we might not have all the bells and whistles of a big fancy conference, but at the end of the day what do you want out of an event like that? Do you want the glitz and schmaltz or do you want to learn something new?

Speakers from the community. We invite people that have something to say, a story to tell, or an experience to learn from. This year we had 84 session submissions from all sorts of folks and on all sorts of subjects. The majority of these people are real day-to-day software developers or DBAs that actually work for a living.

DDD events are democratic. All the sessions are voted in by the community, so we only put on what people have asked to see. This year the 84 sessions were whittled down to 20. We have 5 database sessions all lined up so if you are a DBA or database developer you won’t miss anything. We also have a route through the agenda for Web Developers. We also have sessions on languages (dynamic and functional languages as well as what’s coming soon in C#), processes and tools (such as scrum, virtualisation, TDD and refactoring) and architecture & patterns (such as MVC, MVVM and AOP)

No Marketing BS. The core philosophy for the sessions are that they contain useful information. Stuff that you can either take back and start using the next day or (if not yet available) to start planning how to move to that technology.

To paraphrase the Lincoln’s Gettysburg address, Developer Day Scotland is of the developer community, by the developer community and for the developer community.

Developer Day Scotland

Upcoming events

There are a lot of upcoming events that you may be interested in. I’ve been putting posts on the Scottish Developers site as I get told about them and there have been a fair few this week. So, here’s a quick round up in chronological order:

 

Developer Day Scotland

MSDN Library now has a low bandwidth version

This is fantastic news. MSDN has always been slow and now Microsoft have produced a low bandwidth version designed for travelling developers on slower 3G and HSDPA connections. However, it will probably be my default view from now on. It is much cleaner and easier to read and, naturally, it is much faster to load which is what I want when I need to look something up. I don’t want to lose the train of my thought while waiting for MSDN to load up.

If you are as excited about this as I am then there is more information here: Launching low bandwidth (loband) Beta for long haul! or you can just jump directly to the low bandwidth version of MSDN.

NOTE: In the top right corner is a link to “Persist low bandwidth view”. If you click that any existing searches will be displayed in the low bandwidth version.

Developer Day Scotland

Microsoft MVP Award

Colin Angus Mackay MVP Last week, Microsoft honoured me once again by bestowing on me the MVP (Most Valuable Professional) award again. This is an annual award that Microsoft give to “exceptional technical community leaders from around the world.”

From the MVP website:

Microsoft MVPs are a highly select group of experts that represents the technical community’s best and brightest, and they share a deep commitment to community and a willingness to help others. MVPs are objective technology experts who are eager to share their knowledge. They have no obligation to Microsoft and freely share their expert opinions and experience, earning users’ respect and trust.

I’m naturally very pleased to be an MVP and to have my skills and experience recognised in this way.

The StackOverflowException

Take a look at the following code:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            RecurseForever();
        }
        catch (StackOverflowException)
        {
            Console.WriteLine("Caught Stack Overflow Exception");
        }
        catch (Exception)
        {
            Console.WriteLine("Caught general Exception");
        }

        Console.ReadLine();
    }

    static void RecurseForever()
    {
        RecurseForever();
    }
}

What do you think the output of the program will be?

If you had asked me a few days ago I’d have said the output would be “Caught Stack Overflow Exception”, however that isn’t the case. If you run the code in the debugger this is what you actually get:

StackOverflowException

The exception simply isn’t caught.

If the application isn’t being debugged it will simply end at this point. It goes directly to jail. It does not pass GO. It does not collect £200.

ConsoleApplication2 has stopped working

Tip of the Day #9 (The Project Location Is Not Trusted)

This tip is to get a tool called ZoneStripper by James Kovaks to stop the annoying “project location not trusted” dialog box, below, appearing when you open downloaded solutions in Visual Studio.

If you download zipped source code from the web, unzip it and then open the solution in Visual Studio 2008 (and apparently VS 2003 and VS 2005 as well) you may get a dialog that says “The project location is not trusted” … “Running the application may result in security exceptions when it attempts to perform actions which require full trust.” A bit like this:

The project location is not trusted

What happens is that when you download something from the internet Windows (from Windows XP SP2 onwards) will add an alternate stream to the file called zone.identifier. If the file is a zip it will then add that alternate stream to each of the files as it unzips.

You can view this stream by typing something like the following at a command prompt:

notepad DevWeek2009_PreCon.zip:zone.identifier

You can then read the contents of the alternate stream:

What the ZoneStripper program does is delete the zone.identifier alternate stream from the file so that zone aware applications (and the OS) treat the file normally.

Note: If you unzip the files to a FAT based file system then you won’t have a zone.identifier in the first place as the FAT file system does not support alternate file streams.

I guess I've never needed to do this before…

I guess I’ve never created a struct in a while (at least in Visual Studio 2008 using C# 3.0) because I’ve just discovered that the Automatic Properties don’t work in structs.

I’ve just created this struct:

public struct CapacityUnit
{
    public string Name { get; private set; }
    public long Multiplier { get; private set; }

    public CapacityUnit(string name, long multiplier)
    {
        Name = name;
        Multiplier = multiplier;
    }
}

Which at first glance looks okay except that I get a compiler error in the constructor on Name. The reason for this is that structs need to have the fields initialised before the this object can be used. Name uses this implicitly as in this.Name. So, it would seem that there is no way, at least as far as I can see, to initialise these properties when using Automatic Properties as I would need to use an implicit this.

Update

If you follow my blog on a regular basis you’ll have noticed the entries fall off over the last couple of months. That’s because I’m currently heavily involved in organising Developer Day Scotland on the 2nd May. (By the way, delegate registration is now open. As of writing we are about two thirds full so there are still a few places left)

All this organising is taking most of my time at the moment but I promise that there will be more technical posts coming soon. Hopefully in the areas of ASP.NET MVC, C# 4.0 and enterprise web applications.

Scottish Developers March Newsletter is out. It details all the upcoming events that we have in the works.

Quote of the day

“Just changing your variable name rarely affects the outcome of your code. Just as naming your dog one name vs another. The dog is still the same dog.” [^]