IanG on Tap

Ian Griffiths in Weblog Form (RSS 2.0)

Blog Navigation

April (2018)

(1 item)

August (2014)

(1 item)

July (2014)

(5 items)

April (2014)

(1 item)

March (2014)

(1 item)

January (2014)

(2 items)

November (2013)

(2 items)

July (2013)

(4 items)

April (2013)

(1 item)

February (2013)

(6 items)

September (2011)

(2 items)

November (2010)

(4 items)

September (2010)

(1 item)

August (2010)

(4 items)

July (2010)

(2 items)

September (2009)

(1 item)

June (2009)

(1 item)

April (2009)

(1 item)

November (2008)

(1 item)

October (2008)

(1 item)

September (2008)

(1 item)

July (2008)

(1 item)

June (2008)

(1 item)

May (2008)

(2 items)

April (2008)

(2 items)

March (2008)

(5 items)

January (2008)

(3 items)

December (2007)

(1 item)

November (2007)

(1 item)

October (2007)

(1 item)

September (2007)

(3 items)

August (2007)

(1 item)

July (2007)

(1 item)

June (2007)

(2 items)

May (2007)

(8 items)

April (2007)

(2 items)

March (2007)

(7 items)

February (2007)

(2 items)

January (2007)

(2 items)

November (2006)

(1 item)

October (2006)

(2 items)

September (2006)

(1 item)

June (2006)

(2 items)

May (2006)

(4 items)

April (2006)

(1 item)

March (2006)

(5 items)

January (2006)

(1 item)

December (2005)

(3 items)

November (2005)

(2 items)

October (2005)

(2 items)

September (2005)

(8 items)

August (2005)

(7 items)

June (2005)

(3 items)

May (2005)

(7 items)

April (2005)

(6 items)

March (2005)

(1 item)

February (2005)

(2 items)

January (2005)

(5 items)

December (2004)

(5 items)

November (2004)

(7 items)

October (2004)

(3 items)

September (2004)

(7 items)

August (2004)

(16 items)

July (2004)

(10 items)

June (2004)

(27 items)

May (2004)

(15 items)

April (2004)

(15 items)

March (2004)

(13 items)

February (2004)

(16 items)

January (2004)

(15 items)

Blog Home

RSS 2.0

Writing

Programming C# 5.0

Programming WPF

Other Sites

Interact Software

Source Code, Context, and Visual Cues

Friday 6 February, 2004, 03:29 PM

I've recently contributed to a couple of discussions on some Microsoft blogs. The first was on Brad Abrams' blog, and was about the use of the 'I' prefix for interface names. The second was on Rob Relyea's blog, on the topic of what might be the best syntax for XAML's compound properties.

At first glance, these might seem like two unconnected ideas. One is about guidelines for naming of types in .NET class libraries. The other is about the syntax for a markup language. However, I realised that there is a common theme linking them. Actually, that's not quite right - it's more self-centred than that. I realised that there's a common theme linking the way that I happen to look at these two things. :-)

I like the convention of starting interface names with 'I'. I also like the syntax that the current (PDC) build of Longhorn uses for XAML's compound properties. (And not, as I previously erroneously called them, complex properties.) And I like them for the same reason. They both enable me to know what I'm looking at with minimal effort.

For example, suppose you had the following code snippet:

public void Frob (IFoo f)
{
  // What do we know about f right now?
}

To me, that I on IFoo yells "Polymorphism!" at me. I instantly think to myself that f could refer to pretty much any type of object, and in all likelihood a type I'm not even aware of. Indeed, someone could come along and implement IFoo after I wrote that Frob method. The effect would not be the same if the type were named Foo. My default assumption would be that Foo is probably a class, and that all Foos will behave more or less the same.

I find this only marginally useful when writing code, because the chances are I'll have to go and find out what Foo is in order to do anything. However, I find it enormously useful when reading someone else's code, either in code review, or when I have to work on code I didn't originally write. (And sometimes it's equally useful even if I did write the code, because I might not have looked at it in months...)

It's the improved readability that nails it for me. I like this naming convention because it means that an important fact about the code leaps out at me. There are other examples of this in the naming guidelines. For example, most experienced C# developers will assume that this:

foo.Bar()

is calling an instance method while this:

Foo.Bar();

is calling a static method. And all without requiring any context. Ah, the power of naming conventions! We get a similar effect with the compound property syntax in XAML - right now, when you see this:

  <Button>
    <Button.Background>
      ...
    </Button.Background>
  </Button>

you know that the <Button.Background> is definitely a compound property rather than being a child of the <Button>. So it's the same kind of idea - I am able to know what I'm looking at without first having to decode the context.

These cues are important because they minimise the amount of work you have to do to understand what you are looking at. Without these cues, you would need to have internalized a lot more about the context in which these items appear before you would know that they were compound properties (or interface types, or whatever). This is useful for exactly the same way that syntax colouring is useful - it is just easier to read syntax-coloured source code because the computer does a bunch of parsing for you and presents the results in easily recognisable but unobtrusive ways.

In short, the big win is that you don't have to devote as much brain power to working out what you're looking at, which leaves more spare for reasoning about what you're looking at. This improves your chances of making the code work, or of noticing problems in existing code.

So does this mean I want to see these kinds of cues for everything? No. That way lies madness. If you come up with some kind of adornment to make every conceivable kind of thing distinct regardless of context, you start to reduce the readability. You'll end up with what happens if you take Hungarian Notation to extremes. (Simonyi does specifically suggest not using qualifiers when they're not needed, but I've seen some projects where this notation was used well beyond its usefulness.) There are various problems with having too many distinctive cues:

  1. You'll run out of cues fairly quickly. For example, you can only go so far with syntax colouring before the average person will have a hard time telling colours apart. Creative solutions to this problem tend to exacerbate the next problem.
  2. Lots of different cues can make code look ugly.
  3. The more cues you have, the longer it takes new developers to learn them.

This is why I don't really agree with the popular argument that this is the IDE's job rather than the job of lexical cues. If you want the cues to be visible, then it's true that the IDE can add extra cues you wouldn't get in plain text. Syntax colouring is the obvious example. But there's not much you can do beyond colouring without running into the problems listed above. So if you're going to have to stick to a small set of cues, they may as well be textual, because that way, they'll still be there if you print out the code, or open it in Notepad or vi, or when the IDE inevitably gets confused and can't work out what it's looking at either.

Of course the IDE can reduce clutter by providing info in popups. If it only shows you information when you ask to see it, it can keep the display nice and clean. In fact VS.NET does that now - hover over a symbol with the mouse, or use the "Ctrl-K, I" key sequence, and a tooltip describing the symbol pops up. This is great. It's certainly an improvement on having to work it out or look it up for yourself. But it's no substitute for the much more direct cues like colouring or lexical conventions. Popups are a valuable supplement, but when considering the speed at which a reader can take the information in, they're just not in the same class as lexical cues.

Because so much more time is typically spent reading code than writing it, there is, in my opinion, a very important place for instantly recognisable cues to tell you that you are looking at certain kinds of things. These must be used sparingly for the reasons listed above. But if the choice as to which items get these visual cues is made well, it can make it very much easier to read code, which can only be a good thing.

Copyright © 2002-2024, Interact Software Ltd. Content by Ian Griffiths. Please direct all Web site inquiries to webmaster@interact-sw.co.uk