• Blog
  • Archive
  • About
  • Contact
Sign in

Welcome to rickardnilsson.net

rickardnilsson.net is a weblog and the online home of web developer and father of three, Rickard Nilsson... More

Rickard blogs about creating software solutions using ASP.NET and agile practices.

Sites I've visited recently

  • Twitter
  • Facebook
  • Philip Wildenstam
  • Ninetech - Affärsnytta med IT
  • JetBrains ReSharper
  • Vimeo
  • dnrTV!
  • YUI Theater
  • BlogEngine.Net

Categories

  • .NET
  • Agile
  • ASP.NET 2.0
  • ASP.NET 3.5
  • ASP.NET MVC
  • BlogEngine.NET
  • C# 2.0
  • C# 3.0
  • CSS
  • Design by Contract
  • Design Patterns
  • JavaScript
  • TDD
  • Unit testing
  • User tip

Five most recent posts

  • Dependency injection in ASP.NET MVC with Unity IoC Container
  • ReSharper templates from the Code Kata Cast
  • How to unit test code which depends on HttpContext.Current.Server
  • Code Kata Cast
  • TDD Masterclass in the UK with Roy Osherove

Tag cloud

  • agile
  • ajax
  • asp.net 3.5
  • blog
  • blogengine.net
  • c#
  • code kata
  • css
  • dbc
  • dependency injection
  • design by contract
  • dom
  • douglas crockford
  • fakes
  • foto
  • getweekofyear
  • gregoriancalendar
  • highlight
  • html
  • httpcontext
  • humble dialog box
  • inversion of control
  • ioc container
  • iso 8601
  • javascript
  • jquery
  • jscript
  • julian bucknall
  • live template
  • metaweblog api
  • model-view-presenter
  • mvp
  • patterns & practices
  • photo album
  • picasa
  • recent posts
  • refactor
  • refactoring
  • release
  • resharper
  • rhino mocks
  • roy osherove
  • syntax
  • syntax highlighter
  • tdd
  • tdd masterclass
  • test coverage
  • testing
  • types
  • unit test
  • unity
  • web service
  • week
  • widget
  • word 2007
  • yahoo
  • yui

Recent comments

  • Code Kata Cast (8)
    Rickard wrote: @Marcus Eklund Classical music is royalty free sin… [More]
  • Code Kata Cast (8)
    Marcus Eklund wrote: http://creativecommons.org/ is a good place. Cl… [More]
  • Code Kata Cast (8)
    Rickard wrote: @Johan Lindfors Thanks! @Andrea I use TestDrive… [More]
  • Code Kata Cast (8)
    Marcus Eklund wrote: Nice one Rickard, A bit quiet though :P Used to t… [More]
  • Code Kata Cast (8)
    Andrea wrote: Nice one You can use the R# test runner and assig… [More]

Syntax highlighting in BlogEngine.NET

Saturday, 12 April 2008 00:22 by Rickard

BlogEngine.NET ships with an extension that automatically highlights source code in blog posts. All that is required is that the source code block is surrounded with [code:lang][/code] tags. The extension will markup the code with CSS classes and the default theme includes a default color scheme for code elements like keywords, comments, and so forth. The extension ships with support for HTML, C#, JavaScript, T-SQL, MSH, and Visual Basic.

In forums and blogs in the BlogEngine community issues with the syntax highlighter extension has been brought up. Some of it can be found here, here, and here. Many has complained about how hard it is to use and lack of proper documentation. To get the tags to be recognized by the extension you have to format your post in a really precise manor with a leading and trailing <p></p>. Besides taking up a lot of unnecessary space when editing the post, this is why so many has complained that they can't get it to work. If the block is not correctly surrounded with the right amount of line breaks two things can happen. Either the whole code block is masked or the code is shown but the tags are rendered as part of the code block.

My own experience with this is pretty much the same and the only way to find out how it works was for me to read and step through the code. Since the extension is open source there is no hinder for improving the code base, hence I've been working to improve it to meet the community need, as well as my own. In addition to the mentioned usability issues I missed highlighting of types in C#, that is class, interface, and struct names which we are used to see in Visual Studio colored in cyan.

New version

I'm about to present a new version of the extension which will include the following improvements:

  • Highlighting of types in C#
  • The code block is recognized anywhere in the blog post and does not require any special formatting before or after the code tags.

Leave a comment on this post if you wish to be notified when the new version is available.

Example of syntax highlighting in BlogEngine.NET using the improved version

ICustomer customer = new Customer("kalle");
RegEx regex;
ICollection<Customer> coll = new ICollection<Customer>();
Stack<Name.Space.Customer> stack = new Stack<Name.Space.Customer>();
stack.Put(customer);
customer.Age = 24;

[Serializable]
public class Customer : ICustomer , IComparable<ICustomer> {
    public Customer(string name) {
        this.name = name;
        person = new Person(name);
    }
    public int Age {
        get { return age; }
        set { age = value; }
    }
    IPerson _p = Person.CurrentUser;
    IPerson person;
    internal IPerson Person {
        get {
            return this.person;
        }
    }

    private ShopingCart cart = new ShopingCart();
    protected ShopingCart GetCart() { return cart; }
}

Related work
  • C# code format
    • by Jean-Claude Manoli
    • author of the syntax highlighter extension for BlogEngine.NET
    • Not the same code as shiped with BlogEngine.NET
    • does not support types in C# 
  • Syntax highlighter
    • by Wilco Bauwer
    • linked from the extensions list on dotnetblogengine.net
    • does not support types in C#

 

Tags:   syntax, highlight, c#, blogengine.net
Categories:   ASP.NET 2.0 | BlogEngine.NET | C# 2.0 | CSS
Actions:  
Share | Comments (17) | |

Week and strong contracts in Design by Contact

Monday, 31 March 2008 02:22 by Rickard

Answer to Fredrik Normén on Defensive programming and Design by Contract on a routine level.

"Design by Contract is about creating a contract between the client and the supplier. The idea of the Design by Contract theory is to associate a specification with every software elements. This specification will define a contract between client and the supplier. When a supplier writes a contract with the client, it should document the obligations and benefits"

There is always a contract between a software element and its client. The question is if the contract is implicit or explicitly stated. Lets look at an example from the .NET framework:

public virtual void Add(object key, object value) 
    Member of System.Collections.Hashtable

Summary:
Adds an element with the specified key and value into the System.Collections.Hashtable.

Parameters:
key: The key of the element to add.
value: The value of the element to add. The value can be null.

Exceptions:
System.ArgumentNullException: key is null.
System.ArgumentException: An element with the same key already exists in the System.Collections.Hashtable.
System.NotSupportedException: The System.Collections.Hashtable is read-only.-or- The System.Collections.Hashtable has a fixed size.

A short analysis conducts that the method's contract can be expressed like this:

Precondition:
 ○ true
Postcondition:
 ○ If key is null ArgumentNullException is thrown
 ○ If ContainsKey(key) ArgumentException is thrown
 ○ Else this[key] == value
 ○ And Count == old Count + 1
 
An analysis of the contract shows that the contract can be made stronger like this:

Precondition:
 ○ Key != null
 ○ !ContainsKey(key)
Postcondition:
 ○ this[key] == value
 ○ Count == old Count + 1
 
In the theory of contracts there is this concept of strong versus week contacts where the first is more demanding on the client than the last. Hence, the precondition is now more demanding on the client and this can be taken advantage of in the routine implementation. In the pseudo code below we can see the difference:

Week contract supplier

public virtual void Add(object key, object value) {
 if (key == null) {
  throw new ArgumentException(…);
 }
 if (ContainsKey(key)) {
  throw new ArgumentException(…);
 }
 this.buckets[hashcode(key)] = value;
}

Strong contract supplier

public virtual void Add(object key, object value) {
 this.buckets[hashcode(key)] = value;
}

Using the stronger contract we get a much cleaner implementation of the routine. How about the client code then? Well, take a look:

Week contract client

try {
 hashtable.Add(key, foo);
} catch (ArgumentException ex) {
 // oops, we made a mistake!
}

Strong contract client

if (!hashtable.ContainsKey(key) {
 hashtable.Add(key, foo);
}

Of course, the code for the strong contract client can be used with the week contract as well. The main point is that with the strong contract no error handling should be made because then there is an error in the client code which should be corrected, not handled at runtime. The routine only promise to satisfy the post condition if and only if  the pre condition is satisfied by the client. If the pre condition is not met the outcome is undefined and anything can happen from nothing at all to the system crashing. This is where frameworks that permit developers to express the contract in code comes in.

Design-by-Contract Frameworks
As Fredrik mentions Eiffel was the first programming language to have syntactic support for expressively stating contracts. For .NET there has been several attempts to do the same in addition to Spec#. Here is a list:
  • nContract
    • MS thesis by Wes Haggard
    • Example:

[FormallySpecified]
public class Test {
  [Pre("i > 0")]
  public void DoWork(int i) { }
}

  • ContractN
    • By Dave Sexton
    • Example:

public class Person : ContractN.ProgrammingByContract
{
  [InRequired, OutRequired]
  public string Name { get; set; }
}

  • Design by Contract Framework
    • By Kevin McFarlane
    • Example:

public virtual int B.Foo(int x)
{
  Check.Require(1 < x < 3);
  ...
  Check.Ensure(result < 15);
  return result;
}

I might find a reason to return with a more thorough review of the frameworks in the future.
Tags:   dbc, design by contract
Categories:   C# 2.0 | Design by Contract
Actions:  
Share | Comments (3) | |

Driving out a correct implementation of ISO week numbers using TDD #4

Tuesday, 18 March 2008 23:59 by Rickard
  1. Abstract 
  2. Refactoring
  3. Test coverage 

Proceeding with a solution there is no way of changing the implementation of the GregorianCalendar class to get the correct behavior so we have to go about this another way. One is to subclass the GregorianCalendar and override the GetWeekOfYear method. Another is to implement the GetWeekOfYear in a class totally separate from the .NET framework. I think that the first one is the way to go since the error that we're trying to correct is in the framework itself.

So, first we alter the help method in the test case to use our sub classed IsoCalendar (which we haven't written yet).

So, we are not in a compile state yet so lets do the simplest thing to get this running. I decided to subclass the GregorianCalendar class since what is wrong is the GetWeekOfYTear method of that class. I basically override that method like below.

We run the test a get a red bar. Fine, but now lets go ahead and implement the algorithm. However, this has been done over and over with varying elegantness. My favorite is the work of programmer and mathematician Julian Bucknall. His original implementation can be found at his blog. So, instead of returning -1 I call the GetIsoWeek method from Bucknall's implementation and take the modulus of 100 (since the result from the algorithm is of the form YYYYWW). We run the test and we get a green bar!

 

Now when we get a green bar there's just one more thing to add. The ISO 8601 week should only be returned when the parameters rule and firstDayOfWeek is set properly according to the documentation. In all other cases the GregorianCalendar implementation works just fine. We fix this with a guard clause at the top and we're done.

Summary

During this series we covered test coverage for the faulty implementation of weeks according to ISO 8601. We used Test-Driven Development to provide a fix using Julian Bucknall's really smart implementation. Next we should take a look at refactoring to design patterns.

Tags:   tdd, test coverage, julian bucknall
Categories:   .NET | C# 2.0 | TDD
Actions:  
Share | |

Driving out a correct implementation of ISO week numbers using TDD #3

Monday, 17 March 2008 00:10 by Rickard
  1. Abstract
  2. Refactoring
Our task at hand is to get as much coverage as possible when it comes to errors in the implementation. The ideal coverage is one where all dates are tested. The problem is that the number of dates is infinite. This is of course always a an issue when testing software but it may be more obvious here. If we test all dates there will we a great deal of dates that we know are correct. It should suffice to test only the dates that are incorrect. However, this number is also infinite. We will have to limit our coverage but still get as much breadth as possible. Basically we need to get back to the definition.

According to the ISO standard, in the period 4 January - 28 December the week number is always the same as the Gregorian week and the same also apply for all Thursdays. Thus, the mentioned dates can be safely excluded in our test coverage. Though we still have an infinite number of dates to cover, we are getting there. Finally, we can limit our test coverage to a set of years that can be reasonable argued to appear in the application which we are implementing.

To simplify it the resulting dates are non Thursdays 1-3 January and 29-31 December every year from 2005 - 2015.

Using the calendar in Outlook I compile a matrix of the week numbers in the year interval. The dates that are grayed are Thursdays which should not be part of the test.

  01-jan 02-jan 03-jan 29-dec 30-dec 31-dec
2005 53 53 1 52 52 52
2006 52 1 1 52 52 52
2007 1 1 1 52 52 1
2008 1 1 1 1 1 1
2009 1 1 1 53 53 53
2010 53 53 53 52 52 52
2011 52 52 1 52 52 52
2012 52 1 1 52 52 53
2013 1 1 1 52 1 1
2014 1 1 1 1 1 1
2015 1 1 1 53 53 53

The matrix can be thought of as a set of { date, week number } pairs which can easily be implemented in code.

I create a test which iterates over the set and makes assertions that the correct week was generated by the calendar. It turns out however, that only eleven of the dates generate a wrong week number. I refactor the code to only include the weeks that are wrong. I get a red bar and I see another opportunity for refactoring, the previous two tests can be integrated with this test by just adding the date - week pair to the set. The following is the resulting test:

 

Now that we have a test that fails we can come up with a solution to get the test to pass.

Tags:   iso 8601, getweekofyear, tdd, refactor
Categories:   .NET | C# 2.0 | TDD
Actions:  
Share | |

Driving out a correct implementation of ISO week numbers using TDD: Refactor

Wednesday, 12 March 2008 23:50 by Rickard

Abstract

So, now we make up another test. Looking at the calendar we see that new years eave 2001 should also belong to the first week of 2002 according to ISO 8601. We write the test much like the previous test and we get another red bar. Now its time to refactor. We need to remove the duplicated code in the two tests. First we start with extract method which results in the following method: 

 

 

Now, we can refactor the tests using the new method like this:




We still get two red bars thus we can be insured that we haven't changed any functionality. The next step should be to come up with another test to get more coverage of the errors in the implementation.
Tags:   iso 8601, week, refactor, tdd
Categories:   .NET | C# 2.0 | TDD
Actions:  
Share | |

Abstract: Driving out a correct implementation of week numbers using TDD

Tuesday, 11 March 2008 11:09 by Rickard

The implementation of weeks according to ISO 8601 (used e.g. in Sweden) is faulty in .NET Framework. In particular the GregorianCalendar and CultureInfo classes are faulty when it comes to ISO 8601.

So, how do we know that the implementation of ISO 8601 is faulty using TDD? We come up with a test that fails. By the ISO definition the first week is the week with the year's first Thursday in it. So, we know that e.g. 2003-12-31 is actually week number 01 of 2004 since it is a Wednesday and thus belongs to the first week. The following is a first attempt at a test:

 

Now we get a red bar and we know that the implementation is flawed. However, this is only a single test. If we get a green bar, is the implementation correct? No, of cource not. We need to write more tests to be sure.

Tags:   iso 8601, tdd, gregoriancalendar, getweekofyear
Categories:   .NET | C# 2.0 | TDD
Actions:  
Share | |
 
Copyright © 2008-2009 rickardnilsson.net