Archive for October, 2008

ReSharper messes up your Visual Studio shortcuts

I must start stating I’m a huge fan of JetBrains’ ReSharper for Visual Studio, it certainly makes me more productive in my day-to-day coding tasks.

None the less, today I was just a little bit frustrated when upgrading from 4.0 to 4.1. I have a couple of custom shortcuts which I don’t mind retaining. But ReSharper happily overwrites it all with default settings, which was quite annoying. The least I’d expect would be a message telling me it would (or didn’t I read the setup correctly?) do it, and I’d happily save my settings and restore them after the upgrade.

I see that others have complained as well, and you will find JetBrains official statement here. MS & JetBrains should really figure this out.

So if you’re upgrading ReSharper these days, please back up your VS-settings first!

No Comments

Beware of the IEnumerables

Some time ago I wrote about the difference between List and Collection, and how you should use them. Usually you want to keep the details away from the users of your class, and an even tighter way to encapsulate a list of something is to expose IEnumerable<T>. Note that both IList<T> and ICollection<T> inherits this interface, so IEnumerable<T> exposes even less details to the user. Another benefit is now you can use yield behind the scenes to create your “list” on the fly.

But this also its pitfall. What do you think will be the output of this code?

   10   static void Main(string[] args)

   11   {

   12       var ola = new Pupil(){Name="Ola", IsMate = false};

   13       var trond = new Pupil() {Name = "Trond", IsMate = true};

   14       var pupils = new List<Pupil>(){ola,trond};

   15 

   16       IEnumerable<string> names = ClassMateNames(pupils);

   17 

   18       foreach (string name in names)

   19       {

   20           Console.WriteLine(name);

   21       }

   22   }

   23 

   24   private static IEnumerable<string> ClassMateNames(IEnumerable<Pupil> pupils)

   25   {

   26       foreach (Pupil pupil in pupils)

   27       {

   28           if (pupil.IsMate)

   29               yield return pupil.Name;

   30       }

   31   }

 

Yes, you’re right, the output is my only friend “Trond”:

image

But when are we really finding out who my friends are? The most natural thing is that I find them when I call ClassMateNames, as it is called first. That’s what I’d think anyway. But what if got in a fight with “Trond” between when I sort out my friends and list them to the console?

   35   private static void FightWith(Pupil pupil)

   36   {

   37       pupil.IsMate = false;

   38   }

   16   IEnumerable<string> names = ClassMateNames(pupils);

   17 

   18   FightWith(trond);

   19 

   20   foreach (string name in names)

   21   {

   22       Console.WriteLine(name);

   23   }

This gives me these friends:

image

How about that! The information that I no longer is friends with “Trond” has changed the list I thought I retrieved with the call to ClassMateNames. What is happening is called deferred execution, which delays the actual execution of the internals in the ClassMateNames to when I iterate it. This is exactly how LINQ to [insert TLA here] operate as well, so nothing is actually fetched until you need it.

In my case this was an advantage, because at the time I was listing my friends, I didn’t actually have any!

And the problem is?

The problem starts when you before and/or after the call to ClassMateNames set up and tear down a session, connection, or anything that the inside of the iterator rely on. Any fetching of IEnumerables inside using-blocks should also be considered smells in this regard. If you should clear the list of pupils right after you have sent it to ClassMateNames (you don’t need it anymore right?), you have introduced a very subtle bug as the list is empty at execution.

The advice are thus: IEnumerables and yield is definitively great tools, but be aware of the inherent difficulties and quirks of deferred execution and use them with caution both as producer and consumer!

No Comments

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