Archive for February, 2008

Get those Guards up

I often find code which have several nested conditionals like this:

public void FunctionWithoutGuardClauses(Person person)

{

    if (person != null)

    {

        if (person.Phone != null)

        {

            person.Phone.Call();

        }

        else

        {

            throw new ArgumentNullException();

        }

    }

    else

    {

        throw new ArgumentNullException();

    }

}knight4

According to Steve McConnell in Code Complete 2 it is just fine with regards to putting the most probable clause first. But it is also an anti-pattern in this case; arrow code. With even more nesting it gets worse, and it starts to get nightmarish to follow the possible execution paths.

Oh, and see how I blatantly disregard the Law of Demeter!

Introducing Guard Clauses

A guard clause is a conditional which constitutes our first line of defence in our methods. It can break control flow in an elegant matter, so the only thing left to follow is the valid execution path.

There is also nothing wrong with more than one return path in our methods, as long as it is as clean as it gets with guard clauses. Atwood seems to think so as well, so I’m really home safe here ;-p

So lets see how the above code can be refactored with guard clauses:

public void FunctionWithGuardClauses(Person person)

{

    if (person == null || person.Phone == null)

        throw new ArgumentNullException();

 

    person.Phone.Call();

}

So what are you waiting for, get those guards up and tear those nested nightmares apart!

No Comments

Music with capital M

Growing up with the C64 and the SID, a lot of good feelings emerge when the small tunes reach my ears. I have also fond memories of my first PC and an imported Gravis UltraSound in the early 90’s and the demoscene back then.

So when a coworker introduced me to Slay Radio, I just had to spread the love. I have previously been an avid listener to Nectarine Demoscene Radio, so now I suddenly have a luxurious problem on my hands; to which do I lend my ears?

Well, enough of my hard choices. Get ready for endless hours with nostalgic musical pearls and good feelings all around and tune in to either Slay Radio or Nectarine now!

No Comments

My take on Software 2008, the agile methods in practice seminar

I’ve just returned from an arrangement hosted by The Norwegian Computer Society, called Software 2008. I attended the “Agile methods in practice – What is it and how to do it?” full day seminar. (NB: The last two links are only available in Norwegian!).

Here’s a short review of the day.

First quarter

The first talk was given by Aslak Hellesøy. He gave a general and a bit historical view of the agile methodologies. He spoke well, and had good anecdotes in his speech. I liked in particular the little story about how cargo cults came about, and how we see them all over the place today in the software industry.

The second talk was given by Trond Pedersen and Nils Christian Haugen. They had an original take on their speech, it wasn’t even a speech; it was a role play. They guided us through some typical scenarios in a software development project and showed the agile aspects of it. I think it was a bit contrived, but a think they got across some good points.

Second quarter

The “long” sessions are now history, and we’re given a serious of Lightning Talks. The topics ranged from how “GUI prototypes are evil” to “how to delete production code”. The two talks I enjoyed the most was from Kaare Nilsen, and the one from Trond Wingård. The first because of his charismatic appearance, and the second because of its great topic on present value effects with incremental delivery in agile projects. This could be a real eye opener to just about anyone.

Third quarter

The seminar crowd paired up with each other and interviewed each other to form mind maps. The topic was how we could introduce agile methods in our own organizations.

After a slow start with some standard awkwardness we managed to get some drawings and text down on paper. It was an ok exercise, but I’m not really sure I have any real use for it.

Fourth quarter

Here most of us was introduced to a concept called “open spaces“. I have heard of it before, but have not been a participant in such an endeavour. A range of topics from the days agenda was thrown on a flip over and we sat in on which topic we would find most interesting. Well, you could either have an “engaged foot”, or an “interested foot”, or both to participate in any given topic. But if you found yourself twiddling your thumbs, you just wandered off to another space to see if you would get any “feet” there.

I sat in on a topic about what agile practices does and doesn’t work, and I think we had some interesting discussions and got to poke a bit into the material. The session was a bit on the short side, but I certainly grew fond of the concept.

Summary

All in all, it was a good day, and I got some good input which I will take with me to my own organisation.

I liked the discussions and mingling the most, and I’m definitively going to engage more in such events.

1 Comment

Remember to check out PartCover

Bil Simser just wrote about the lack of coverage tools for .NET in this article, and I agree with his points.

In our current project we’re using NCover and NCoverExplorer (older editions), which are working just fine as we’re still in the .NET 2.0 world. It seems there are some problems with the older versions (they have gone commercial on us) and the new .NET 3.5 framework.

This isn’t particularly good news, as we’re going to utilize VS 2008 and .NET 3.5 shortly. The choice is wether fleshing out 150 USD, or find an alternative. One of the comments on Bils post linked to another coverage utility; PartCover.

So, note to self: Check out PartCover when we’re going .NET 3.5.

No Comments