For the love of…

This post is inspired from a code review with a colleague, covering the many ways we, as c# developers, can write code to simply call a method three times.


You want to perform an action three times. You could just call the statement three times

Console.WriteLine("Hello Reader");
Console.WriteLine("Hello Reader");
Console.WriteLine("Hello Reader");

Chances are, this may make you twitch a little. It’s not DRY and not very scalable – you might chose to do something like this

public void DoSomething(int count)
    for (int i = 0; i < count; i++)
        Console.WriteLine("Hello Reader");

What’s wrong with this? Nothing as far as the compiler is concerned and it will of course call your method three times (when a 3 is passed into the function). The picky part of me thinks this isn’t good enough though.
In particular, we wanted to call a method three times, so for this simple task why do we have to declare an integer? Why is it called ‘i’? Surely those days belong to the days of a fax machine?

C# is a pretty rich language. We can do better.

Who doesn’t love ForEach?

If I had a collection with three items, I could simply call ForEach, right?

private static void EnumerableRangeForEach(int count)
    foreach (var number in Enumerable.Range(1, count))
        Console.WriteLine("Hello Reader");

Looks a bit better, but I don’t need an ordered collection of integers really, just a collection with (3) things in it. Like this:

private static void EnumerableRepeatForEach(int count)
    foreach (var item in Enumerable.Repeat(typeof(bool), count))
        Console.WriteLine("Hello Reader");

At least our code doesn’t declare an ordered integer array now; now we have a collection of three booleans. However, we still have the annoyance of needing to declare var item to please the for loops gods. We can do away with the named variable (ish) if we add in the additional expense of converting it to a List

  Enumerable.Repeat(typeof(bool), count)
                      .ForEach(item => Console.WriteLine("Hello Reader"));

Everyone loves a one liner. At least the author of it does, reading someone else’s isn’t always such a joy. My main concern is that we had to create a collection, then convert it to a list in order to iterate over it, using List’s .foreach extension.

Please the recursive gods

Many years ago, I had no beard and when not sleeping through lectures at university learnt to program, functionally. The lecturers in their wisdom thought functional programming was the way forwards and once upon a time I was proficient in ML (F# has some of its origins here) and Prolog. Naturally I used all these skills, when I started out as VB programmer after graduating…

I digress. Our desire to do something three times, can be written like.

private static void RecursiveLoop(int loopCount)
    if (loopCount == 0)

    Console.WriteLine("Hello Reader");
    var nextCount = loopCount - 1;


Granted this isn’t the smallest method here. On the plus side, we are not forced to declare an implicit collection and we don’t have any variables that aren’t doing anything explicit. You can of course argue that we are still declaring a variable (nextCount) to keep track of where we are, similar to our var number sacrifice earlier.

If you are unfamiliar with recursive functions, I should point out that failure to declare a base case (if loopCount == 0 in our case) that will always be hit, will result in a StackOverflow. Similarly in some circumstances if you’re are looking at doing thousands of recursive calls and you are using tail recursion, then current limitations with the c# compiler will also result in a stackoverflow. Lots of posts about this on the web, this one ain’t bad:

What’s best

As ever in computing, it depends should be the right answer. If the business case strongly suggests that you only ever need to call something three times and it’s unlikely to change, I can see a case for the non dry example at the top. Very efficient and easy to read and I’ve seen code bases a lot worse in terms of dryness.

More realistically, this number three is likely to be changeable, so you want a collection and the ability to iterate over it. Personally I like the style of the recursive function and have some negative opinions on for (int i = 0; i< upperBound; i++) – just seems so primitive and unimaginative. However, the compiler’s going to reduce them to basically the same thing so as long as your method is clean, easy to read and easy to test, it’s all good.

What’s your preference?


Of course there’s a big except to my last paragraph. Not all for loops are the same when you really need to squeeze your method and get it crunching 1000s and every nano second counts. I’ll cover this in my next post.

Cycling attitudes

It was a dark and stormy night… and there I was cycling into town on an errand. It’s 5.30pm. I’m almost at my destination I signal my intention to go left. I’m turning left (going across the bike lane in the other direction) and then wham, he hits me. I didn’t see him (obviously). Neither did he see me. buckledwheel His impact caught my front wheel with a good deal of force, I don’t believe he even applied his brakes. My front wheel crumpled under the impact. He didn’t strike me with enough force to knock me off my bike, but the effect of coming to a standstill so quickly, combined with the shock of being hit, I lost my balance and came off the bike. Balance in a stationary position is tricky – especially when I have my four year old daughter on the back! The other cyclist also fell off his bike in the collision. No-one was hurt, my front wheel never recovered though. None of us was wearing cycle helmets.

If I were still in London and wrote the last sentence, people would think me very irresponsible to cycle without a helmet and especially to transport my daughter without one. However, I live in the Netherlands now and here, unless you’re on a racing bike, decked head to toe in lycra, you don’t wear a helmet. Cycling, for the non racing types, is more just an efficient way of walking. People cycle to work in suits…

I’m not against cycle helmets and clearly in the game of head vs pavement, I’d rather play it with a helmet on. However the game of head vs pavement should not be thought of as only for cyclists, as in practice this is not the case as @BezTeets tweeted recently:

Ben Goldacre posted this in the British Medical Journal, showing that there are a lot of factors that determine what makes cycling safer. What role the cycle helmet plays is hard to state.

So why don’t the Dutch wear helmets?

It helps that the infrastructure here is fantastic – plenty of segregated (physical curb or similar from road and pedestrian path) cycle paths, often only in one direction is a massive plus. However, there are plenty of roads, where the cycle path is similar to that in UK (painted along the side of the road) and of course many roads with no cycle path at all. It’s what happens here that I find interesting. On the road, cyclists have the right of way. So, I can be cycling down a narrow side street (no cycle lane) with a van coming the opposite direction. Often the van will slow down, potentially even stop to make it easy for me to ride past. If I need to turn left in a (non cycle paved) road, I can go into the middle of the road, signalling my intention and do it. In the UK, this in theory is also true. In practice, cars tend to sound horns, shout abuse at you and drive on the right or left of you as you might be costing them 5 seconds of their journey time.

Cyclists here are generally commuting, as are many UK ones I imagine. Here you do so in ordinary clothes and at such a pace as to not really break a sweat (helped by no hills of course!). In London my experience was mainly also commuting. However, many there interpret the word commuting as racing. Having these cyclists daily trying to improve on their personal best times, in rush hour is not a healthy thing. I’ve had the misfortune to cut some of these cyclists up, vis versa too I hasten to add. Wow, do some of these cyclists get real angry about such a nothing event. So much rage…

Here, along narrow bike paths, you’ll often come across two people cycling side by side chatting. If you want to go faster, ring your bell and they will allow you to pass. Cycling here is safer and more pleasant and slower. Everyone does it, not just men 20-45 in lycra, but women, grandparents and kids. Heck, even some of the 4 year olds in my daughter’s school ride to school (next to mummy/daddy of course), in the bike lanes/on the roads.

My answer

The answer lies somewhere between the Dutch have a good cycling infrastructure and attitudes. This result is cycling is safe and pleasurable here. I started this post by explaining how I was hit by an irresponsible (in my opinion) cyclist who was going too fast (by Dutch standards) based on traffic conditions. Collisions happen. However, if those collisions are not at high speeds and the parties involved are comparable sizes, then chances are serious injuries are much less likely. Think about that time you were walking on the pavement and bumped into someone; it was a nothing event, right? Now if only cyclist collisions could be like that, outside of the Netherlands.

How Google Tests Software – my review



James Whittacker, Jason Arbon & Jeff Carollo

First published 2012

What’s a developer doing reading a book on testing? Isn’t that something for the Tester to read?

…was how I once thought. These days my mindset is along the lines that a good developer is more than just being able to write good code. It’s more about being part of a team that delivers software. We need to be involved with defining the story of what our customer wants, writing the code using TDD techniques and being able to work with a tester to verify that we are delivering a piece of quality to our customer.

This book is focussed on the roles of the people who make Google’s software. All roles require some developer nous. Each part of the book goes into some detail, explaining what that role covers within Google. This book assumes you are familiar with development and testing practices.

The four main roles are:

  • The Software Engineer (SWE): A developer who delivers features. Of course done using TDD and also ‘participates in Tests’.
  • The Software Engineer in Test (SET): A developer focused on testability, quality and risk. Works closely with the SWE to help create test code, such as mocks and stubs. The book goes on to say they even create Testing Frameworks. Their goal is to test the developer and ensure code is of a high standard and consistent with the rest of the codebase.
  • The Test Engineer (TE):  Focussed on testing the user. i.e. ensuring that what’s getting delivered is what the user asked for and is of a good level of quality. They write test automation scripts. They are product experts. They are Quality and risk analysers.
  • The Test Engineer Manager (TEM): Primarily responsible for coordinating TE and SET. A take away for me was that these people are really product experts on top of really knowing their people.

Crowd Sourcing

Google also has the clever use of getting the public to tests its software for them. They call it crowd sourcing, it’s a natural extension of dog fooding (which they also do). The idea is to get users to run through tours, execute, specific testing and perform exploratory testing. They even have incentives for  bug finders! The Test Engineer’s role as I understood it was to coordinate this, to try and get a good level of coverage and facilitate how bugs are fed back to the developers.

The Future…

The final chapter is disappointingly not about killer robots, self driving cars or transportation devices. Whittacker waxes lyrical about how he sees the testing landscape at Google evolving over the next few years. Like combining SET and SWE roles. This was a thought I also had when reading the book –  if you’re a strong SWE – surely a lot of what the SET does are tasks you would do yourself. Especially writing mocks and stubs – for me an important tool I use when writing my code with a TDD approach. They are also using automation tools like Selenium and encouragingly he sees that having SWE/SETs contributing to open source frameworks such as Selenium is much more beneficial as opposed to the cost of innovating and maintaining an in-house testing framework.

Test engineers are reducing all the time. One key reason here is feedback. Making use of continuous deployment mechanisms they can get software to the customer (live, crowd testers, dog fooders) fast. Bugs can be raised fast and developers can fix these bugs and get them back out to production, fast. I read from this that TE will become more involved with orchestrating crowd tests and writing automation scripts and less involved with manual testing.

Google – just another software department?

This was an enjoyable and easy read but for me didn’t rock my world so to speak. It’s nice to know that the guys at Google aren’t doing anything significantly different to what I’ve seen in other good IT departments in terms of their approach. There is a focus on testing the ACC (Attributes, Components, Capabilities) of its software as opposed to test plans. I think this could read as Specification by Example, as the focus is on what problem the software is solving without getting bogged down in how it’s doing it. If this interests you, should read the book.