Archive for category oo principles

MSDN Live with Steve Ballmer keynote

ballmerMSDN Live has toured Norway this fall, with a handful of presentations for .NET developers. I attended the last one in Oslo on Tuesday, where Steve Ballmer made the keynote!

Most developers have seen the developers, developers, developers talk, and we had high expectations for this keynote. He was quite enthusiastic in Oslo as well, telling us about things he thinks will influence our industry in the foreseeable future. This search at Youtube seems to find the whole show.

The rest of the day I attended the main track, starting out with a Silverlight 2 presentation from Gøran Hansen. While Silverlight 1 was mostly about animations and media, I didn’t give much attention. But the second version looks promising, with the CLR and a subset of the .NET libraries present. What I miss though, is maybe some background about the fact that running .NET WinForms applications in IE in object-tags has been possible a long time (without the automatic deployment of the framework). Props however to MS for finally supporting more than Windows & IE only! Gøran did a great job presenting, I really enjoyed his presence on stage.

Next up was another presentation from Gøran, which gave the audience an introduction to ASP.NET Dynamic Data. For those of us who attended NDC2008 and Scott Hanselmans keynote there, it was plain repetition. The only difference might be that this was showing the final bits released with SP1 of the framework, and Scott showing us some earlier version. It seems like a great technology to get you up and running very quick, but I wonder how many pages you end up with in the CustomPage directory after a while. Even though the topic didn’t tickle my fancy, Gøran did a great job on this one as well.

Then it was time for lunch, rather late according to my stomach. But it tasted good, and there seemed to be enough to fill up all us starving geeks. As always during the breaks, old colleagues and friends sneak up on you and says hello. Which I really do appreciate!

After the break, Anders Norås gave a talk about SOA. More specifically about how we should start thinking along the lines of enterprise service buses and leave our old crusty webservices in the dust. I really do concur with a lot his ideas, and I’m always happy to see code with binsor-love. I also liked his onion-architecture, which resembles my standard layered architecture (no, not the "standard standard"). Hmmm, looks like another blog post (again).

"Debugging your debugging habits" was up next with Rune Zakariassen from Microsoft. He showed us some tips & tricks for your debugging needs in Visual Studio, and presented a step-by-step recipe on how you best can find bugs in your code. I thought it was a nice presentation, even though my only real takeaway was the tracepoint functionality in VS. I’ve been a sucker for Debug.Writeline when I don’t want to impact the run of things stepping through, but this gives me an even less intrusive way keeping track of how the program executes.

Finally, Gøran entered the stage for the last time this day. He showed us how WPF databinding can be used with a variant of Model-View-Presenter called Presentation Model. The emphasis was really on this pattern, and how it helps attain SoC. He also mentioned DIP with the short version "Program to an interface, not an implementation". A good thing can’t be said enough so here is the original:

High level modules should not depend upon low level modules. Both should depend upon abstractions.

Abstractions should not depend upon details. Details should depend upon abstractions.

All things considered, yet another great talk from Gøran, keep up the good work and preach on about those design patterns and OO principles!

Yet again, great work by Rune @ Microsoft arranging this event. Until next time, I’m on twitter at http://twitter.com/haugern.

1 Comment

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