Framework Design Guidelines

A few months ago I finished reading the second edition of Brad Abrams and Krzysztof Cwalina’s excellent Framework Design Guidelines book. I just love how this book condenses the lessons learned in building the .Net Framework in areas such as design patterns and API usability into a set of easy to follow guidelines annotated by various luminaries from Microsoft and beyond.

For me reading the book provided valuable insight into .Net, reinforced my knowledge of design patterns and provided lots of useful advice on software engineering in general. As an example one guideline that I recall as I write this is “do prefer protected accessibility over public accessibility for virtual members”. This leads to the suggestion of applying the template method pattern as follows:

public Control {
  public void SetBounds(...)
  {
    ...
    SetBoundsCore(...);
  }

  protected virtual void SetBoundsCore(...) { ... }
}

The book also makes the valuable point that this pattern allows the base class to enforce that overloads remain semantically consistent by defining them in the base class in terms of one core virtual method that can then be overridden.

On the usability front a comment from Krzysztof has stuck with me since reading the book:

The number of customers that will use your API is inversely proportional to the number of new statements in your simple scenarios.

This reinforces the book’s suggestion of a scenario driven design approach where we are encouraged to write code for common scenarios before designing the API and run user studies early on with other developers writing code exercising these main scenarios.

Finally Framework Design Guidelines introduced me to FxCop which does the great job of encoding the guidelines in a static analysis tool thus catching any accidental slips in our APIs early on. This makes following a large number of the guidelines incredibly easy.

As Miguel de Icaza asserts in the foreword: this book will help you become a better programmer.

Haskell in less than five minutes

I have been playing with Haskell and the Glasgow Haskell Compiler today, refreshing my memory of the language. After spending the last two years coding mostly in C++ and Python with a smattering of C it is a rather pleasant experience. Note: the simplest way to try out these examples is with ghci or hugs; simply place the code in file.hs and load it from within the interpreter with :load file.hs and then call the function as normal.

To start with a simple example of the language, the following calculates the nth Fibonacci number:

fib n | n < 2 = 1
      | n >= 2 = fib(n-1) + fib(n-2)


The boolean guards n < 2 and n >= 2 determine what is evaluated. We can then map this function onto the natural numbers using:

fibs = map fib [1..]


and subsequently get the first 10 Fibonacci numbers with take 10 fibs which returns the expected list. Higher order functions such as map are simple to define, such as:

map f xs = [f x | x <- xs]


The expression on the right of the equals is an example of list comprehension. The generator x <- xs takes each x from the input list xs in turn and the expression f x is applied forming a new list. Multiple generators can be used, for example the expression [(a,b) | a <- [1..3], b <- ['a', 'b']] evaluates to the list

[(1,'a'),(1,'b'),(2,'a'),(2,'b'),(3,'a'),(3,'b')]


Making use of list comprehension gives us the rather lovely Haskell rendering of the quicksort algorithm:

qsort []     = []
qsort (x:xs) = qsort [y | y <- xs, y < x] ++ [x]
                     ++ qsort [y | y <- xs, y >= x]


Simple, concise and, in comparison to C or the pseudo code rendition in the textbooks I have to hand, very easy to understand. When supplied with an empty list the qsort function returns an empty list. When given a list (consisting of the first element, x, and the remainder of the list xs) we use the first element as the pivot and apply qsort recursively to all the elements less than x, concatenate that list, using the ++ operator, with [x] and then concatenate that with the result of qsort applied to all elements greater than x. We could even make this slightly shorter by replacing [y | y <- xs, y < x] with filter (< x) xs. Of course I have glossed over the fact that using the head of the list as the pivot is not a very good idea but it’s still a very aesthetically pleasing piece of code. :)