Archive for September, 2005

PDC05: Expression Quartz

Thursday, September 15th, 2005

I’m really looking forward to using Quartz, one of the three Expression graphic design tools announced yesterday at the PDC. Quartz is the XHTML/CSS designer, and even the fact that Microsoft would release such a standards-based tool would have been unthinkable not long ago. I think at this stage though (with schema-checking in VS 2005 and the cross-browser Atlas project), it’s fair to say Microsoft is finally embracing web standards and cross-browser compatibility. Quartz provides a completely CSS and semantic markup based designer and uses external CSS stylesheets which are automatically updated when you change the properties of an item.

Most (all?) standards-based web developers are currently forced to use a text editor and constant alt-tabbing to a separate web browser to create their sites, and even though it’s possible to be quite productive this way, it can be pretty painful, and slow. With only a quick demo at the keynote it’s difficult to say how well this will turn out, but for more details check out the Quartz features page.

PDC05: Integrated query and C# 3.0

Thursday, September 15th, 2005

The big news for C# developers at PDC has definitely been Language Integrated Query (the LINQ project) and the new features in C# 3.0, and the largest room in the convention centre was packed, with an overflow session scheduled for tomorrow for those that didn’t make it in time.

There’s far too much to describe in a blog post, but the headline features of version 3.0 include lambda expressions, extension methods (a little like mixins in Ruby), anonymous types, and local type inference. A lot of these are designed to support the new LINQ features, which make it possible to write query-like expressions in C# 3.0 or VB9 code. Queries can be run against anything that implements IEnumerable, which means you can query arrays with where clauses, group by, and order the results however you want.

The really great thing is that all of this is strongly-typed – Anders and his team have managed to take some of the most compelling features of dynamic scripting languages like Ruby and Python and deliver them in C# along with compile-time type checking and IntelliSense.

The syntax takes some getting used to:

Customer[] customers = GetCustomers();
var results =
   from c in customers
   where c.City == “London”
   select new { FullName = c.FirstName + “ “ + c.LastName, c.CompanyName }

This little snippet shows a number of things: first there’s the the var keyword, which is not a variant, but rather tells the compiler to infer the type of the results local variable. Writing “var i = 3” for example, is exactly the same as “int i = 3”, and this shorthand becomes useful later in the statement.

Most of the rest of the statement is really syntactic sugar for a number of regular method calls. In fact the statement could be written in C# 2.0 as something like:

private class CustSummary { ... }
IEnumerable results = customers
   .Where(delegate(Customer c) { return c.City == “London” })
   .Select(delegate(Customer c) {
      return new CustSummary(c.FirstName + “ “ + c.LastName, c.CompanyName)
});

If you haven’t tried out generics and anonymous delegates in C# 2.0, the code above is going to be just as impenetrable as the 3.0 version, and as Anders pointed out, all of the features planned for 3.0 make heavy use of the innovations in the 2.0 release of the CLR. In fact, C# 3.0 does not require a new version of the CLR – everything works on VS 2005 today once you install the LINQ Customer Technical Preview.

There are a couple of things about this however that are difficult to express in version 2.0. First of all, where did the CustSummary class come from? In fact, CustSummary is an anonymous type, created just to support the statement. It’s a real type, defined in your assembly, and you will have full IntelliSense support for its members, you just can’t refer to it by name since it is generated by the compiler (so it’ll actually be called type0001 or something). This is why we used the var keyword to define the results local – this way the compiler can define results correctly for us, since it knows the name of the anonymous type.

The other strange thing about the code is the Where and Select methods. Where did they come from? We seem to be calling them on the Customer array, but they aren’t part of the Array class. In fact these are extension methods, which have been added to everything in the current scope that implements IEnumerable, though they aren’t part of the IEnumerable interface. You get to use the extension methods simply by adding a using reference to System.Query, which includes a class that defines the extra methods.

Extension methods are a little scary since they allow you to add methods to any predefined class. By using a special syntax you can define these as static methods in a separate class, but make them available on the class they extend. Want to add a ToTitleCase() method to all strings? No problem. Add ToBase64() to all byte arrays? Easy. Anders demonstrated adding a ToXml method to IEnumerable, which created an XML stream for all arrays, lists and anything else that implements that interface. Powerful stuff, though as he made clear, the potential for abuse is frightening :)

If you want to find out more about these features, the best place to start is the LINQ technical preview, available on the MSDN site. All the source code for the extension methods is included as well, so you can dive straight in and start playing.

PDC05: Double-check locking fixed in 2.0

Thursday, September 15th, 2005

After a nice introductory session by Jan Gray, Joe Duffy delivered a much more in depth talk on multithreading issues in .NET. Joe is a PM on the CLR team with responsibility for the System.Threading namespace, so if anyone knows the details on this, he does. He made one interesting point about the double-check locking pattern, which kind of did and kind of didn’t work in 1.1. Double-check locking is an attempt to avoid the overhead of creating a lock by checking a value before entering the lock. The value is then retested inside the lock.

if (instance == null)
{
   lock (creationLock)
   {
      if (instance == null)
         instance  = new Singleton();
   }
}
return instance;

The idea is that, once the instane is created, you never need to use the lock. In theory this is a pretty clever trick, but there’s a problem: modern processors can reorder instructions for performance reasons, which means that some of the assumptions made by the double-check pattern will not always be true. In the singleton example, if there are instructions to execute inside the constructor, instance may be set to a non-null value before the constructor completes, and therefore before the lock is exited. The result of this is that an invalid reference to instance is returned to the caller. For more details on this, read this great post by Chris Brumme.

Processors make various optimizations like these, but for the most part programmers are shielded from this complexity by a memory model defined by the language or runtime which states which optimizations can occur, regardless of what the processor wants to do.

The 1.1 version of the Common Language Infrastructure spec had a relatively weak memory model which did not prevent this specific optimization from taking place, so the double-check pattern was not guaranteed to work. In reality however, the x86 processors (the only ones supported by the offical .NET 1.1 release) don’t reorder instructions in this way, so the double-check pattern did in fact work on that platform.

The good news is that the new 2.0 version of the CLI spec includes a stricter memory model which clears up all this confusion and ensures the double-check pattern will work.

PDC05: Vista and Office 12

Wednesday, September 14th, 2005

Phew! The first day at the PDC was really busy, starting with a long (very long) double-keynote from Bill Gates and Jim Allchin. I guess it’s what you expect with these events, but the speakers themselves weren’t particularly engaging, but the demos really made up for it.

First up was a combined Vista and Office 12 demo – Microsoft are really keen to promote these together, hoping corporate customers will look at buying both together as part of a long overdue upgrade cycle, and they are the focus of a lot of the sessions here. Visual Studio 2005 in comparison is assumed to be the platform of choice, even though it hasn’t even been released yet.

The Vista demo was impressive, with lots of nice eye candy and a few changes since Beta 1 was released. Beta 2 is a few months off it seems, but the sidebar is back, RSS is everywhere, search is everywhere and IE has of course finally embraced tabs. There’s actually a really nice feature in IE7 that shows the scaled contents of all open tabs in a window, making it easy to navigate between them or close those you no longer need. If you have the habit as I do of steadily building up a collection of tabs over a long browsing session, this is going to be really useful.

Of course a lot of these features aren’t new, at least when you consider non-Microsoft software. Search everywhere looks exactly like Apple’s spotlight technology, as does a new app switching feature (alt-tab now shows live previews of all open windows, a lot like Exposé). Where Microsoft excels though, is improving work others have started. The film producer Julia Philips had a saying, “If you can’t be best, be first. If you can’t be first, be best,” which seems pretty appropriate.

Having heard some positive things about Office 12 on various blogs before the conference, I wasn’t sure what to expect. Office has had more features than sense for the past 8 years, and Office 97 was the last upgrade to really mean anything. The new version (due next summer I think) has loads of new features of course, but much more important is the new UI, which tries to make all that power more accessible.

Seven or eight tabs now appear where the old toolbars were, and each tab has a number of groups of three or four buttons. It’s a little hard to describe, but it really looks a lot like the old interface, except the grouping of related items really makes a big difference. By reducing the number of items the user has to choose between, it becomes easier to conceptualize and navigate around a complex interface. The hierarchy allows you to ignore anything you’re not interested in and go directly to what you need. This is a pretty common UI approach – my running copy of iTunes for example, has fourteen buttons on the main window, but you wouldn’t realise it at first. There’s one section to move between tracks, another to search, another to manage playlists, and so on. It seems a small change, but it makes a big difference in managing the complexity of features that modern software offers. Office 12 also uses larger icons for key buttons which makes them easier to distinguish and provides mini “landmarks” to use when scanning a toolbar with a number of button groups.

Overall, there’s a real feeling of the quality of these products, even though they haven’t left (or for Office even reached) an initial beta. The first public release of Office will be in a few months time, and I’ll definitely be getting a copy and having a closer look.