June, 2009 Archives
Jun
Book review: Code Complete 2nd edition
by Mikael Lundin in Technicalities
In Sweden we have a common view on American technical literature to be bloated, pages full of words but little real content. Somewhere I heard that American writers are paid per page. I don’t know if that’s true, but most of the books I have from American writers could be cut to a quarter without loosing content.
So, when I got hold of Code Complete 2nd edition, my first reaction was, “oh, another 800 pages that could be cut down to 200 pages”, and it really isn’t a book that you bring with you on the commuter train to work. A reason for you to bring it with you would be as a weapon when you pass through the park late at night. One hit with the Code Complete could make a grown programmer unconscious for hours.
The review
The first thing I ask myself after reading Code Complete 2nd edition is, what target audience did Steve McConnell intend for this book? First I think that the target audience must be people that have never seen a line of code in their life, because this book describes programming in a way even my grandmother would understand. Then again it takes for granted that you know linked lists and is written in a way that suggests that you have probably met this problem before.
Some of my colleagues gave the suggestion that this book could find its place as an introduction to programming at the university, but I must disagree. When you’re going to learn programming you need to get your hands dirty with code at once. You need to focus on one language and learn that language well. When you are able to craft most of the things you can think about, you could move on to the principles of software development to tune your skillset. By that time this book does not bring anything new to the table.
So, where should I place this book? I could use it as a reference when a junior developer does something stupid and I need to prove to him why you should not write programs as he does. You could use this book as a base for coding conventions, but that would require you to agree with everything in this book, which I say that I cannot. Chapter 9, Pseudocode Programming Process, was very amusing but not really something I would recommend someone using.
I would not say that this book is useless, but I find it hard to read because it never gets to the point (quite common with this kind of American technical literature). If it does have a target audience, I’m quite sure that I was not part of that target group. It does not inspire me to become a better programmer, but it is a good back reference to all the wrongs we all did becoming good software developers.
Summary
I have huge respect for Steve McConnell, for his many years in the IT business and the amount of experience he has. I also heard that he has written a good book about software estimation that I’m sure to check out. However, I will not recommend Code Complete 2nd edition to any of my colleagues.
Jun
Mocking log4net calls
by Mikael Lundin in Programming
For the first time ever, I found the need to test logging calls. Normally I would call that stupid but I had a situation that looked very much like this. (don’t ask why)
public Customer RetrieveCustomer(Guid id)
{
Customer result = null;
try
{
result = dataAccess.GetCustomerByID(id);
}
catch (DbException ex)
{
Log.Error("An exception was thrown from the database", ex);
}
return result;
}
This code is very much simplified for this blog post.
We have a very delicate situation where the RetrieveCustomer method swallows all database errors on the spot and return a default value. I would like to pass the exception on and let it surface and handled in the view, but right now that is not possible. Since I’m writing code that I’m not comfortable with, I’m going to write a test to confirm what is going on.
[TestMethod]
public void ShouldSwallowDbExceptionButLogError()
{
/* Setup */
var exception = new TestDbException();
var da = MockRepository.GenerateStub<DAL.CustomerDataAccess>();
ILog log = MockRepository.GenerateStub<ILog>();
var repository = new CustomerRepository(da, log);
/* Arrange */
da.Stub(dataAccess => dataAccess.GetCustomerByID(Arg<Guid>.Is.Anything))
.Throw(exception);
/* Act */
repository.RetrieveCustomer(Guid.Empty);
/* Assert */
log.AssertWasCalled(logger =>
logger.Error(Arg<string>.Is.Anything, Arg<Exception>.Is.Same(exception)));
}
This was done using MsTest and Rhino Mocks. I just love that AAA syntax.
Jun
NCover 3 – Review
by Mikael Lundin in Technicalities
I’ve promised to review this software that I’ve been using intensly for the last month. So, here goes.
NCover – What is it?
This is a tool to help you as a developer to see what code your tests went through. It will help you to write better tests that aims to be more specific, and it will help you increase your code coverage. It is also useful for managers to keep track on what parts of the application is covered by tests.
I think this screenshot pretty much says it all.

The cool things
- It is obsessively fun to work with this tool. After writing a test, I can’t wait to see how it affected my coverage.
- You can see trends and easily spot where you loose coverage. (also a lot of fun)
- Visualization is great, and being able to see how many times a certain code line was run, helps you find the “trunk” of your programs execution tree.
- The architecture of NCover with the analysis tool as a command line executable makes it easy to integrate with other tools (CruiseControl.NET).
- Hooking up the IIS process running the analysis engine on my acceptance tests, totally blew my mind! Awesome!
…and things that could be better…
- It’s not exactly plug-and-play for the beginner. It’s rather “read-documentation-and-play”.
- Visual Studio integration could have been better. As far as I know the only VS integration is TestDriven.NET. Maybe the NCover team does not want to step on Jamie Cansdales turf.
…and in comparison…
I’ve been looking at the coverage tool that comes with VSTF, and even if it got full integration with Visual Studio, it can’t be compared to NCover. The visualization is not close enough and the only thing you can measure cover on (as far as I know) are MsTest unit tests. I would pick NCover above the VSTF coverage tool.
NCover is a tool that every developer should invest in, because it is an investment that will pay off. If you’re serious about testing, this tool cannot be ignored and should not, since it will increase your productivity by magnitudes.
Jun
From list to delimited string
by Mikael Lundin in Programming
I just read a blog post from Johan Olofsson where he describes how he uses a extension method to create a string representation of any list. I found it very interesting since I’ve written something very similar.
I wanted to share my own version here, last modified 2008-06-28, almost a year from now, if we should trust the date stamp.
public static class EnumerableExtensions
{
public static string ToDelimitedString<T>(this IEnumerable<T> list, string separator)
{
return ToDelimitedString(list, separator, DefaultConverter);
}
public static string ToDelimitedString<T>(this IEnumerable<T> list, string separator, Converter<T, string> converter)
{
// List can't be null
if (list == null)
{
throw new ArgumentNullException("list", "Tried to create a string from a list that was null");
}
if (separator == null)
{
throw new ArgumentNullException("separator", "Must specify a separator even if it is string.Empty");
}
Contract.EndContractBlock();
// If converter was null, we probably wanted default converter
if (converter == null)
{
return list.ToDelimitedString(separator, DefaultConverter);
}
// Start the process of creating the string
var sb = new StringBuilder();
foreach (var o in list)
{
sb.Append(converter(o));
sb.Append(separator);
}
/* Remove last seperator (if there is one) */
if (sb.Length > 0)
{
sb.Length -= separator.Length;
}
return sb.ToString();
}
private static string DefaultConverter<T>(T item)
{
return item.ToString();
}
}
The main difference between mine and Johans solution is that I’ve chosen to remove the last delimiter after the foreach and Johan does it in the for loop. It really does not matter for small n and I don’t believe that this would be a good aproach for big n, anyway. (thinking Parallel Fx)
This is how you use my extension methods.
public static void Main(string[] args)
{
// Simple example
string[] names = { "Joe", "Jack", "James" };
names.ToDelimitedString(",");
// A bit more complex
var books = new Book[]
{
new Book { Isbn = 1590598504, Title = "Expert F#", Price = 614},
new Book { Isbn = 735619670, Title = "Code Complete", Price = 324},
};
books.ToDelimitedString("\n", book => string.Format("ISBN={0}, Title={1}, Price={2:c}", book.Isbn, book.Title, book.Price));
}
Why can’t you just override ToString() on the Book class? While that would be an alternative, it requires me to supply a format that would be able to recreate the instance through a Parse method. (ref. Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries) This time I might just want to output the contents of this book-array to the screen.
And sometimes you’re just not the owner of that class you’d like to output.
Happy coding!
Jun
My history of computing
by Mikael Lundin in Technicalities
My family bought a computer for the first time in 1988. I was six years old, home alone with my mom and I badly wanted to play those funny games that I were playing the day before. Though, my mom did not know how to operate the computer and I was not allowed, the urge got too much and I decided to give it a try. I had seen the procedure before and I knew what to do. I had just never done it myself.
So, I flipped the power switch and waited for the screen to show

Heart was pounding in 200 bpm as I selected the disc that should play my favourite game (The Great Giana Sisters). I inserted it into the floppy and the game started to load.
Fast forward in memory…
At twelve of age I sit at home during the summer on my fathers work laptop (Cyrix 586), playing Doom 2 he lumps a thick book in my knee. - It is time for you to learn how to write your own games, he says, and I start the long and tedious task of learning Turbo Pascal.

After 6 months I had written a music library that used a linked list of structs for in memory handling and could both save and load the library from disc. Debugging linked lists was something I would get back to 12 years later as I was relearning this in my university studies.
Fast forward in memory…
Nineteen years old I moved from my parents house to my own apartment where I spent the nights building my own Linux distribution and the days learning Lisp at LiTH. It was a fascinating language and the school had a fascinating nerd culture.

Infinite recursions made my head spin and in the evenings I would do some relaxing PHP3 coding, creating communities that were totally unmaintainable. I also had a short fling with Java, but the compiler errors made me irritated. Especially after a short course in ADA with compiler errors like ‘You misspelled your method name in line 123.’
Fast forward in memory…
As I studied computer science at KTH Stockholm around 2005, there was a choice to be made between Java or .NET technologies. Since all my friends and close relations were Microsoft haters, I choose .NET as a platform because I wanted to experience something new. I was astounded by the ease of getting started with C# and I felt I could be productive right away.
Today I work mainly with ASP.NET 3.5 web apps and is quite happy with that. It would be interesting working on a heavy client software application sometime, but I like the problems that web applications present. Heavy integration and scalability with thousands of users.
I feel very flexible, so who knows what technology I’ll be working with tomorrow. Maybe Ruby, F# or Scala?