My response to the Cycle Superhighway 2 Extension consultation

If you’ve been paying attention to the twitters recently, you’ll have seen that TfL have released their plans for the Cycle Superhighway 2 extension. I think they’re rather good, and wanted to tell TfL. Here’s my response to their consultation:

Wow! I am really excited to see these plans, and hope that they are the beginning of the “step change” in planning that Boris Johnson recently promised.

I live in West London and work in Shoreditch, so these changes won’t directly affect my daily commute; however, they will make a real difference to my experience coming to Stratford, and I hope they will be a great success, and act as a blueprint for future developments.

I have visited Stratford by bike, and was struck by the lack of pleasant, convenient routes to get there, having a choice either of a circuitous waterside route (pleasant but inconvenient), or the fast traffic on the High Street (convenient but unpleasant). If this new route had been in place, it would have been an easy decision.

I am particularly excited by two elements of this proposal:

a) The segregated cycle paths. I am a man in my mid 30s, and a reasonably fast, confident cyclist. I am absolutely capable of taking primary position in traffic, and of negotiating several lanes of vehicles. However, this is not to say that I enjoy doing this, and I find it stressful to constantly have to deal with the aggressive and dangerous behaviour of some drivers; furthermore, I don’t see why I should have to put myself in the way of aggression and danger just to get across town on my bike. I believe these segregated paths are a really important element in making cycling more pleasant and less scary, which in turn will encourage more people to get on their bikes.

b) The bus-stop bypasses. It is great to see these in the plans, and I hope they prove a success. Despite the long-established habit of telling cyclists to use bus lanes, buses and bikes really don’t mix very well: playing leap-frog with a bus as it overtakes you, only to have to overtake it in turn when it stops 100m down the road is a frustrating experience, and I have had several unpleasant experiences with bus drivers cutting me up or passing too close to me. Even when the cycle path is not on the road, it often gives up at bus stops, and cyclists either have to rejoin the road at the bus stop, or enter a “shared use” path behind it. Introducing bus-stop bypasses, which I’ve seen in several European countries, seems like a bold and sensible step.

All in all, I think these are very exciting proposals, and I hope this consultation receives constructive feedback to make them even better. My great hope is that these changes will prove a great success, and will lead to this quality of road planning being introduced across London in the near future.

Thank you!

What features of natural languages should programmers investigate?

We watched a presentation by Ola Bini on Expressing abstraction—Abstracting expression, and the language geek in me got very excited when he introduced some natural language examples of different modes of expression.

But I was soon disappointed by his choices:

  • simile
  • metaphor
  • redundancy

Now, these are perfectly worthy topics for consideration, but if we really want to explore alien paradigms, how about we venture further afield. Here are a few:

 

How Wiggins’s Crash Puts a Lie to Tribal Cycle Infrastructure

Two stories came into my inbox this morning:

First is the news that Bradley Wiggins has been hit by a van while on a training ride.

Second is a blog post criticising the crap new shared-use paths on Shepherd’s Bush Green.

A sentence in this article is very telling:

Do the transport bods have any idea of the danger that would pose to people on foot, particularly those not nimble or agile enough to dart out of the way of the idiots that whizz through the Bush on bikes when they should be on the road?

My initial reaction to this is to protest that cyclists should be on these paths: they’ve been designated as routes for cyclists, so it’s pretty unfair to criticise cyclists for using them. Of course, the author’s commends do highlight the absurdity of expecting cyclists and pedestrians to ‘share’ space.

But perhaps the author is highlighting the problem presence of fast cyclists on these paths, and this may go some way towards explaining why this was seen as acceptable infrastructure.

There’s a tendency [I’ll find citations for this] for planners to divide cyclists into tribes: the Fast Confident Cyclists, versus the Timid Unconfident Cyclists, and to assume they have different infrastructure needs. If it’s assumed that Fast Confident Cylists are happy, or even keen, to Share the Road with white vans, tipper trucks and juggernauts, then we can ignore them in any infrastructure provision. This then means that we can provide ‘shared’ paths for the Timid Unconfident Cylists, who will be grateful for the crumbs of crap, disconnected infrastructure that keep them out of the way of fast-moving traffic, but who don’t move fast enough to be a problem for pedestrians.

Wiggins’s crash shows that this is wrong: even Fast Confident Cylists, accustomed to riding in the most challenging of situations, are still put at risk on our roads. We need to stop throwing crumbs to the Wobblies and start putting decent infrastructure in place before more people—Fast Confidents or Timid Unconfidents—get hurt and killed.

***

Edits:

  • Changed reference to Wiggins riding in a group: subsequent reports make it clear he was riding to meet a group.

My response to the Lambeth Bridge Northern Roundabout Consultation

I’ve sent a brief response to TfL’s consultation on ‘Safety Improvements at Lambeth Bridge Northern Roundabout‘. Here’s what I said:

I am a confident and experienced cyclist who uses this junction daily. Nonetheless, I do not find it an enjoyable junction to negotiate, and this influences my route through the surrounding streets. I believe the proposed changes would make the junction *less* pleasant to use, contrary to the aim of this project.

I usually approach this junction from the north along Millbank and turn left to cross Lambeth Bridge. There is frequently heavy traffic using the roundabout ahead of me, so I usually take—admittedly slightly illicit—advantage of the cross-hatched area to make my left turn. There is usually someone crossing the zebra on the approach to Lambeth Bridge, and as I pause to let them cross, I can re-enter negotiate my way back onto the carriageway to cross the bridge.

After the proposed changes, I will no longer be able to do this manoeuvre, as the cross-hatched area will have been removed. Instead I will be faced with a choice: either cycle on the pavement, or wait for a gap in the traffic to turn left in the carriageway section of the roundabout.

I am unlikely to make use of the pavement, as this looks like it would involve an awkward manoeuvre to get onto the pavement, and another to re-enter the carriageway on Lambeth Bridge. Furthermore, doing this would bring me into conflict with pedestrians, who, quite understandably, are rarely enthusiastic to share their space with faster-moving cyclists.

Hence, my journey through this roundabout will be made somewhat more stressful and less convenient by the removal of the cross-hatched area: the opposite of the desired effect.

On some occasions, I approach this roundabout from Horseferry Road, and continue across it onto Lambeth Bridge. On these occasions, I wait in the outside lane so I can join the roundabout in the appropriate lane. Because of the heavy traffic on this roundabout, this can take a while, and I often find myself amongst buses and coaches waiting to join the roundabout. Once a suitable gap has appeared, I have to cycle out swiftly and confidently to get round the roundabout. This manoeuvre is perfectly possible, but not very pleasant, so I usually plan my journey so I approach from the north, rather than the west.

The proposed improvements would do nothing to improve my journey across the roundabout from Horseferry Road: for the reasons given above I would not use the shared-use pavements, so I would continue to have to use the main carriageway as I do now.

I believe these proposed changes would make the roundabout less pleasant for cyclists to use, and I cannot see how they would constitute a safety improvement. I suggest you revise your plans and provide a proper Dutch-style roundabout with segregated cycle lanes separate from both the pavement and the carriageway; such a layout would be a joy to use, and would greatly improve this junction. The Cycling Embassy of Great Britain, Rachel Aldred, and the Dutch Cycling Embassy have all given details of how this could be done, and it would be a significant day if you could be true to the Mayor’s promise to Go Dutch by heeding their advice.


27 October 2012: Updated with links; removed link to consultation, as the deadline has now passed.

Team Depression: a recipe for first aid

An interesting parallel occurred to me the other day during a conversation at work: the mood of a team is subject to changes, just as that of an individual, and sometimes depression can set in. So, just as we can learn techniques to fight of depression in ourselves, maybe we can do the same within our teams.

It became clear to me a few weeks ago that my team was in the doldrums: we had come to the end of several significant pieces of work, but had released this work with very little fanfare, which led to a feeling of deflation; furthermore, our future workload was both daunting in size and vague in scope, with few clear short-term goals, which gave us a sense of listlessness; in the past few months, several really talented team members had left us, and we hadn’t yet managed to to find a new dynamic for the team, so we had lost the buzz that comes when collaboration becomes second nature; finally, a raft of factors beyond our control meant that we kept finding our work blocked, which just added another layer of frustration.

I had recently taken on the role of team lead, which meant not only that these issues became a particular problem for me, but also that I had an opportunity as primus inter pares to do something about this. In working to find a way out of this morass, I came to a realisation: I had been here before, and I already knew how to deal with it.

A rich seam of depression runs through my family, and it has been part of my life since childhood. In the past few years I have pushed it into remission, and it rarely raises its head now. However, from time to time I do catch the onset of a spell of depression, and I have identified a set of first-aid techniques that I can use to stop it developing further. Many of these techniques—regular exercise and fresh air, early nights, cutting down on alcohol, making time to read and relax—seem simple and obvious, but these are just the habits that can get sidelined when depression sets in, so I have to treat them as a strict regime and push myself to follow them. The result of this is incredibly effectively for me.

Here are my key actions in fighting the signs of depression:

  • I have identified and look out for the early warning signs, so I know when I need to take action
  • When I spot these signs, I acknowledge that the situation warrants special behaviour, even if this means putting other priorities on hold
  • I have identified specific behaviours that I know help me recover
  • I stick with these behaviours until I am back on an even keel

This brings me back to my team’s situation. Of course, the ideal is to develop good everyday habits that keep depression at bay, but while we work on that, we may well find ourselves slipping into the doldrums occasionally. It seems to me that all four of these actions can apply to a team just as readily as to an individual.

I would be interested to hear other people’s experiences of dealing with lapses in team mood, as well as thoughts on how these ideas fit with various codified working patterns. Please share your ideas!

Should the HSE investigate deaths of cyclists?

I have just read a shocking post by Mark (@AsEasyAsRiding) which lists the cyclists who have been killed or seriously injured by lorries directly connected with the construction of the Shard in London Bridge and asks whether the hopelessly inflexible management of this building project has led to the (literal) cutting of too many corners.

We recently also heard the result of the inquest into the death of Svitlana Tereschenko, who was killed on Bow roundabout by a lorry driver on his way to the Olympic site, who was talking on his phone (hands free, so technically legally), and had failed to indicate before cutting across her. This verdict is reported by The Cycling Lawyer, and prompts a very interesting observation in the comments, that had Svitlana been killed on the Olympic site, the HSE would have taken immediate action, but that because her death took place off site, responsibility fell to the CPS, who are consistently shite at following up traffic crimes.

This leads me to wonder whether we should be campaigning for the remit of the HSE to be extended to cover commercial vehicles no matter where they are. Ideally we would have a prosecution service that actually took the carnage on our roads seriously, but perhaps the involvement of the HSE would at least offer a chance of civil redress, and force these companies to take cyclists’ lives seriously.

Addison Lee and Licensing

At this evening’s die-in at the Addison Lee offices, the ever entertaining predictable John Griffin deigned to address us the broadcast media, only to rehash his previous comments about cyclists needing licences.

Now, Addison Lee has a licence as a private hire operator, yet that doesn’t stop Griffin inciting his staff franchisees to break the law.

Addison Lee drivers have driving licences, but that doesn’t stop their constant infringement of traffic regulations.

Griffin and his company are a prime example of the ineffectiveness of licensing.

So why the buggery does he hold so much stock by the idea?

The State Pattern, explored through the medium of cake!

At 7digital, we are running a weekly discussion group on design patterns, and I have been creating some small projects to exlore the issues raised in these discussions. The other week my colleague Emily presented the State Pattern, and it soon became clear that this is one of the more controversial patterns, as it seems to ride roughshod over the SOLID principles.

In this post I will give a bit of background, and then discuss the various approaches illustrated in my project.

The State Pattern

The classic case for the State Pattern is when an object’s behaviour changes according to the state it is in. A vending machine can only sell when it contains products, a shopping basket can only be checked out when it has products in it, a person can only get divorced if they are already married.

This pattern is implemented as an extension of the Strategy Pattern: the context object (vending machine, shopping basket, person) has a dependency on a state object, which in turn implements the behaviour (sell, check out, divorce) in an appropriate manner. Unlike cardinal examples of the Strategy Pattern, in which the strategy is set once and left alone, the State Pattern lets the strategy change during execution — often as the result of a method call on the state itself (if the vending machine sells the last product, it then becomes empty; if the only item is removed from the basket, it cannot be checked out; if the person gets divorced, they are no longer married).

Smelly

An immediate criticism of the State Pattern is that it leads to a system that is fairly tightly coupled. Not only is the context coupled to the state in order to make method calls, but a mechanism is then needed for updating the state, and a classical implementation of this is for the state to be coupled back to the context — a two-way coupling that is already a code smell.

Other implementations are possible: the context can update its own state after each state method call, or the state method can give the successor state as a return value; however, putting this question aside, it is the State Pattern’s lack of SOLIDity that makes it most problematic.

VAPID

Consider a context object that exposes several methods and has several states. Now consider that only some of these methods will be appropriate for any given state of the object (just as in the examples given above). Under the classic State Pattern, each concrete state must implement a method for every delegated method of the context object, even if it is not appropriate for it to do so (often implemented by throwing an exception).

First, this is a violation of the Single Responsibility Principle, as each concrete state now has responsibilities that it is designed to shirk. If we consider the ‘reason to change’ criterion, each concrete state can change because a) it changes the implementation of the stuff it does do, and b) it changes the implementation of failing to do the stuff it does not do.

More graphically, this is a violation of the Liskov Substitution Principle. This principle requires interchangeable objects (eg, those with the same interface) to behave in the same way, ie, given the same input conditions, they produce the same output conditions. The problem with the State Pattern is that it requires each concrete state to have different behaviour from its peers: violation of the LSP is seemingly baked into this pattern.

Finally, this pattern can lead to a violation of the Interface Segregation principle, insofar as a multiplicity of methods on the context class (which may be justified as being appropriate to that object) can then delegate to a a multiplicity of methods on the state interface, which, given their state-dependence, will no longer form a coherent collection.

Sharlotka

Let’s take a break from theory here, and look at my implementation.

I considered the stereotypical State Pattern examples, but let’s be honest: there are quite enough vending machines and shopping baskets in the world, and discussion of design patterns is not the context to be locking horns with Cardinal O’Brien.

So I thought I would use a culinary example instead.

Sharlotka (шарлотка) is a Russian apple cake. It is made by filling a cake tin with chopped apples, then pouring an eggy batter over the apples and baking in a warm oven till the batter has set. It is served dusted with sugar and cinnamon, and is really rather lovely.

It also makes a good, simple example of the State Pattern: each stage of cooking must be carried out in sequence, but there’s a nice loop in the middle, where you have to keep checking the cake until it’s cooked through before turning it out.

Classic Implementation

My first implementation follows the classic pattern.

You can see that there is one ISharlotkaState interface, which contains a method for each of calls that the Sharlotka context object delegates to its state. Each of these methods takes a reference to the Sharlotka as a parameter, so it can alter its state if need be. (The Sharlotka is passed with the interface IHasState<ISharlotkaState> to avoid making the State part of the public interface of the Sharlotka class itself.)

If you look at any of the concrete states (eg, ReadyToAddApplesState), you will see that most of the methods are left throwing a WrongStateException, as well as the mechanism for updating the context’s state. The _successor state is injected in the constructor, as this makes it possible to leave the mechanics of wiring everything together to the IoC container, rather than having to new up states within other states. In a small way this is reminiscent of the Chain of Resposibility Pattern, and does something to alleviate the tight coupling smell.

If you want to unit test one of the concrete states (eg, ReadyToAddApplesStateTests) then you are again left with a lot of boilerplate code.

This implementation highlights some of the deficiencies of the classic State Pattern implementation; however, it does still work, and may be appropriate for cases where it is not so much the behaviour of the context that is dependent on state, but rather its internal implementation.

Segregating the Interfaces

A more parsimonious approach has been suggested by Jan van Ryswyck, and I have implemented a version of this as the Small Interface project.

The key difference here is that rather than having a single, monolithic interface for all the states, the individual behaviours are broken into their own interfaces. When the context comes to call each state method, it first checks whether it can cast the state to the appropriate interface, and only then makes the call.

This implementation makes no further efforts to remedy the tight coupling, but does makes some improvements to the SOLID violations:

The Single Responsibility Principle is better supported, as each state is now only responsible for implementing the interfaces that are actually appropriate; we no longer have states also taking responsibility for throwing WrongStateExceptions, as this is done by the context object.

The Liskov Substitution Principle is better supported, as we no longer have sets of defective implementations of interface methods; instead we have used the Interface Segregation Principle to split out a set of atomic interfaces, each of which can more easily support the LSP. There are still opportunities for violation of the LSP, as it is entirely possible to implement the same interface in several states, and for each implementation to be different, but this problem is no longer inherent in the implementation.

This implementation makes good steps towards SOLIDity, but still has a major flaw, which is intrinsic to the state pattern: you can call methods on the stateful object that are simply not appropriate to its state; to continue the sharlotka example, you can call Serve before you have called Bake. Whilst doing this will throw an exception, it should arguably not be possible to do so in the first place.

A Fluent Implementation

A third possibility came up in our discussion — I think it was Greg who raised it —: you can implement your stateful class with a fluent interface.  You can see my version of this in the Fluent project.

Like the Small Interface implementation, this uses several atomic interfaces instead of one monolithic one; unlike that implementation however the interfaces are directly implemented by the Sharlotka class, rather than being delegated to a state object, and at any point in its lifecycle the Sharlotka is only considered as an implementation of the interface that is appropriate to its current state, ie, you never have access to methods that are not currently supported.

The trick in implementing this is in the fluent idiom: rather than calling

sharlotka.AddApples();
sharlotka.AddBatter();
sharlotka.Bake();

we want to chain the methods like this:

sharlotka.AddApples()
	.AddBatter()
	.Bake();

We can do this by making each method return this, but with a return type of the appropriate next interface.

For example, TurnOut is implemented like this:

ICanDustWithSugar ICanTurnOut.TurnOut() {
	return this;
}

which means that the next call must be a method of ICanDustWithSugar.

This implementation does away with the smelly tight coupling of the State-Pattern examples, as state is represented by the return type interface of each method, rather than as a property of the Sharlotka object. The application of the Single Responsibility Principle is rather less clear in this example, as the methods are stubbed out, rather than being implemented in any meaningful way. It is quite likely that in a real system the implementation of each method would be delegated to another object in order to honour this principle; this would look rather similar to the Small Interface implementation, with the crucial distinction that the implementation would not be responsible for updating the Sharlotka’s state.  The Liskov Substitution Principle ceases to be a question here, as we have moved to small interfaces with single implementations, and this fact also supports the Interface Segregation Principle.

Where this implementation is not so suitable is in cases where the state of the object after a method call is not clearly determined. For instance, withdrawing money from a bank account can leave the account in credit or overdrawn; in such a case the trick of returning a particular interface is not sufficient. A small example of this in my implementation is the Bake loop, and I have overcome the problem in this particular case by checking the return type until it is not null. However, this technique is already a significant departure from the fluent idiom, as it relies on runtime checking to make sure that the code actually works.

There is another danger with this implementation, in that it relies on the consumer knowing to use it fluently. There is no protection against storing the return value of any method in a variable, and calling the same method more than once (indeed, this is what is done during the Bake loop), and any person coding with this interface needs to know that methods must be chained. However, the fluent idiom is fairly commonplace now, and neither of these considerations is one of code quality.

& Messe it Forth

These three implementations illustrate different approaches to the same problem. The Classic implementation is unlikely to be the best in most circumstances because of its tendency to produce Liskov-violating defective methods; the Small Interface implementation overcomes these problems, and is probably most suitable for situations where the state of the object does no change in easily predictable ways; the Fluent implementation is handy when a particular sequence of methods should be called, but less idiomatic when the sequence can branch at runtime.

There are also tantalising prospects for implementing this type of system with monads, but I’m going to leave that for another day.

Response to Islington’s consultation on Bunhill Row

Islington are consulting on continuing the cycle contraflow down the whole length of Bunhill Row. This is a road I use frequently, so I have given my response:

I live in Shepherd’s Bush and work on Scrutton Street in Shoreditch, just over the border in Hackney.

I commute to and from work by bike, and really appreciate the good cycle route along Skinner and Lever Streets, which I always use when I approach Shoreditch from Bloomsbury, and its continuation to the top of Bunhill Row, which means I can avoid the unpleasant gyratory at Old Street roundabout.

A southbound continuation of the route along Bunhill Row would provide an invaluable missing link, letting cyclists approach the city without having to negotiate Finsbury Square and Moorgate, which are poorly laid out for cyclists at the best of times, and really nasty at the moment, thanks to the Crossrail works. Moorgate was the site of a the recent death of a highly experienced cycle courier, which says something about the hazards of this particular stretch of road; a convenient back-street cycle route would help mitigate the poor infrastructure on these trunk roads.

Your proposals look excellent, and I wholeheartedly support them.

Yours,

Matthew

PS, while I am commenting on infrastructure in this area, could I voice my dislike of the cycle-pedestrian crossing of City Road between Featherstone Street and Leonard Street. Cyclists are expected to wait for the lights on the pavement, and then cross the road simultaneously with pedestrians, while moving diagonally across their path; this means that the crossing is unpleasant for both cyclists and pedestrians to use, as no one is clear who is moving in which direction. I would strongly suggest you look into altering this crossing — separating cyclists from pedestrians either physically or in time — to reduce this conflict.

If you use this road, please respond to their consultation.

ActionInvoker method sequence

In one of the projects I’m playing with, I’m doing a bit of a hack over .NET MVC 3.

I’m providing my own implementation of IActionInvoker, currently by extending ControllerActionInvoker, and as part of this work, I’ve done a quick audit of the methods of this class, and the order they are called in.

I’m reproducing them here, in case it’s useful to anyone else:

  • InvokeAction
  • GetControllerAction
  • FindAction
  • GetFilters
  • InvokeAuthorizationFilters
  • GetParameterValues
  • InvokeActionMethodWithFilters
  • InvokeActionMethod
  • CreateActionResult
  • InvokeActionResultWithFilters
  • InvokeActionResult