C# Dictionary.GetValueOrDefault()

This is one of those methods that you can’t believe was missed. If you have a C# Dictionary <string, OtherClass> myDict; and you then access dict[“dave”] where “dave” is not in the Doctionary, it throws an exception.

The natural call to have is one where it returns a null if that entry does not exist. So I added an extension class. Here it is:

/// <summary>
/// Get a the value for a key. If the key does not exist, return null;
/// </summary>
/// <typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
/// <typeparam name="TValue">The type of the values in the dictionary.</typeparam>
/// <param name="dic">The dictionary to call this method on.</param>
/// <param name="key">The key to look up.</param>
/// <returns>The key value. null if this key is not in the dictionary.</returns>
public static TValue GetValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey key)
    TValue result;
    return dic.TryGetValue(key, out result) ? result : default(TValue);

The iPad & Siri Changes Everything

I recently bought a Nissan Leaf (nice car). It has a dashboard display that is a touch screen about the size of an iPad. This incorporates GPS navigation, Bluetooth connection to my cell phone, radio/CD/USB player, and more. The system also includes voice recognition for control. The provided functionality is pretty much everything you will want in a car.

And it sucks.

By any standard from four years ago it’s amazing. And by a checklist of features it has everything you want today. But the UI is clunky and awkward. The voice recognition is merely a way to select from the menus – it does not provide shortcuts. And it’s not that good at determining what you are saying.

I think many of the people designing software for places other than the iPad, like automobiles, figure that they will be held to a lesser standard. I don’t think that’s the case. The advances in UX (User eXperience) on mobile and tablet systems has been incredible over the last several years. Consumers expect to see that same advanced UX in all devices they interact with.

It’s a reasonable demand. There is no technical reason Nissan could not have created as good a UX system for the Leaf. They would have needed to hire a Silicon Valley consulting group but that cost is minimal spread across all the Leafs sold. My guess is they didn’t think it mattered. They believed they would be compared to what the other car companies have created. But that belief is wrong – they’re being compared to what we all see every day using the iPad. They’re being compared to Siri.

Mark Andreessen famously said Software Is Eating the World. Very true. But a corollary of that, one that I think very few are aware of yet, is it is also making software key to the acceptability and value of every product that has a visible software component. This acceptability/value will be measured compared to all software used, not solely to directly competitive products.

In many industries the winners over the next 10 years will be the ones that realize that they need to become a software company that ships a product with their software. And that is a very difficult transformation for companies in industries where their focus for decades has solely been on the physical product. Like car companies.

The Nissan Leaf – my first week

Well it’s now a week since I got my Leaf. And yes I’m still happy I picked it (over a BMW 535). It’s not a better car than the BMW, not is it worse – it’s different. I’m happy I went with the Leaf because the only way to understand owning an electric vehicle is to actually own one. No amount of reading will lead to really understanding the difference.

What is the big difference?

It’s battery charging time. If the battery runs out you’re stuck – for hours. With a gas powered car running low on gas means a 5 minute stop at a gas station and therefore you pay no attention to fuel level until it gets low (which in my wife’s case is over ¼ still in the tank). With an electric you charge it full every chance you get. And you then face a hard limit on how far you can drive before you have a long wait while it recharges.

This is very similar to your cell phone or Kindle. Charging takes time and if the charge runs out – you’re not calling or reading anymore. And like both those devices, recharging takes time. The Leaf is a bit worse though as you can use a phone/kindle while it is charging. For the Leaf, that’s not an option (unless you have a really long extension cord). The bottom line is your car is tethered to the existing battery charge. And when you hit that limit, you want at least a 220 volt charging station (which takes just 7 hours to recharge vs. the 20 for a 110 volt plug in charger).

Over the next year we should see charging stations appear throughout the Denver metro area. I am guessing Xcel will do anything they can to increase sales of electric vehicles. It means more electricity for them to sell. And most of it will be overnight charging when they have excess capacity, so it will be electricity that is a lot cheaper for them to produce.

It will make a big difference when you can drive to a meeting or lunch and your car will be re-charged during the meeting. This will pretty much double the range of the cars where you can drive the full range to a meeting, then have a full charge to drive the full way back. Of course, this assumes a charging station is available – arriving somewhere to find them all taken would be really bad news.

The bottom line is, because of the charging time, even if there are charging stations everywhere, you are always charging when you can.

So how does it drive?

The key words are instantaneous and silent. It’s very weird (but good). Push the power button and it’s on and ready to go – but there is no noise. (There is a lot of flashing on the dashboard for a second as all the power-on tests run.) Set it to drive (or reverse) and again no time or noise – it’s just in that mode. Press the accelerator and you’re moving – instantly. And again with no noise.

And stop isn’t idling like you get with a gas powered car, it is truly stopped. Nothing is moving, nothing is using electricity (except the heater and radio).

A very real problem is you tend to drive faster in the Leaf. With a gas powered car you hear and feel the acceleration and speed. The engine gets louder as you go faster, the car vibrates from the engine and drive train, and you feel the jerk when it shifts. The Leaf has none of that. There is no noise from the engine. There is no shifting. There is no vibration from the drive train.

And it all happens immediately. My previous car was a BMW M3 which is by many measures the most perfect race car every offered as a production vehicle. It’s fast. But engaging a clutch takes time (it’s a stick). There is a slight delay from pressing the gas to increased RPMs, because fundamentally it’s a mechanical system. The engine has the most power is a sub-range of RPMs. The M3 responds incredibly quickly, but it’s not instantaneous. The Leaf is instantaneous and while the difference in how quickly you go from 0 – 60 is not that great, it’s very different in how it feels. (I still have the M3 and one weekend morning we’re going to get up early and go to the Foothills Highway and see which is faster doing 0 – 60.)

The suspension on the Leaf is your standard family sedan suspension. In other words it sucks compared to a BMW. But… it has a ton of weight along the floorboards from the batteries so it actually corners pretty good. It has a shape sort-of like a Jeep but it corners like low-end sports car. On the really negative side, you need to slow down for speed bumps and dips while the BMW took that in stride. (And yes, the day BMW has an electric I’ll probably be upgrading.)

The Problems

All of these issues are of secondary importance at best. But they show a lack of attention to getting it right on the associated parts. The car itself appears to be superb. Everything else, not as much. (And this makes sense as a car company will have its primary attention and expertise focused on the car itself.)

  1. The dashboard display will show charging stations on a map, except they have a total of 1 station listed in Colorado (there are lots more). They don’t even show the location of the Nissan dealers that have chargers (That’s sad). This shows a total lack of attention & effort to encouraging the installation of stations. Yet sufficient stations are key to broad acceptance of electric vehicles.
    1. The CEO of Nissan should call the CEO of Xcel and work out a means to place 480 volt charging stations (30 minutes for a full recharge) every 50 miles on I-25 from Pueblo to Ft. Collins and I-70 from Denver to Grand Junction. With this 95% of the potential car purchasers in the Front Range portion of Colorado could use an electric vehicle for 100% of their driving including the occasional trip across the state.
  2. There are two clocks displayed, one by the speedometer and one in the touch screen. They are individually set and therefore often disagree by a minute. Not a big problem, but it shows that they didn’t put in the effort to get it right.
  3. The iPad changes everything. Four years ago the dashboard touch pad and voice recognition would have been groundbreaking. But fair or not I compare what they have with my iPad and the Leaf system is clunky. The UI is clunky even by the standards of Windows 3.1. The menu structure is ordered by who the underlying code is structured, not by how a driver would use it. The number of buttons on the dashboard and steering wheel follow the model of more is better (true for bank balances, but not for user interfaces).
    1. The voice recognition to place a call takes about a minute to dial a number (cascading menus and requests for repeats). Not worth the hassle – just call using the iPhone itself.
    2. And it plays music on a USB stick – you just plug it in (great idea). However, it only plays songs who’s filename uses the Western European alphabet. So the majority of my music (Russian Pop) – can’t play it. (My wife and daughters think this is a good thing about the LEAF.) Again, lack of attention to getting it right.
    3. The Leaf iPhone app is better, but still poor by the standards of well-designed iPhone apps.
  4. The shifter (more a mode switch) – you push it forward to go backwards. And you push it backwards to go forwards. What the fuck?
  5. The Owner’s Manual is about 350 pages. And once you have read all of that, the Navigation System Manual is another 240 pages. The “quick” reference is 20 pages. This is unusable. Part of this is due to the poor UX design in the navigation system and umpteen million buttons on the dash and steering wheel. But part of it is also due to not taking the time to explain the system using the fewest words possible.
  6. Every time the car starts the navigation systems puts up a disclaimer that you have to press OK on. No way to permanently accept the terms and conditions so it does not reappear. Don’t let the lawyers tell you where to put disclaimers – all it does is honk people off and generally adds no additional protection.

My hope is now that the Leaf is shipping, Nissan can take the time to get these associated details right. My main suggestion to them is to OEM the iPad, plus Siri, as their navigation system. And then hire one of the top Silicon Valley UX design groups to implement the Nissan specific programs. And calling the CEO of Xcel would be a really good move too.

But while it’s not perfect, it is a really good car.

National Intercollegiate Programming Playoffs – local and national playoffs, all in one day

Work Locally, Compete Globally

Up till now, for most competitions, you have local competitions, often spread out over several weekends. And then the winners of those competitions fly to meet for the national competition. But there’s no reason to do this for a programming contest. And there are some very good reasons to run the entire event in 1 day with everyone competing from their school. Just as programming teams can be composed of people all over the globe, so can competitions be among contestants all over the globe.

With the entire event in 1 day, we only have to create a single problem for the contest. This year for The Windward National Intercollegiate Programming Competition it is writing the A.I. for a computer game. If the contest was spread out, we would have to create a different game/A.I. challenge for each day. And creating each game is a lot of work (figure about 4 man/weeks go into each game for programming and testing).

This also reduces the time commitment for each student participating. It is 8 hours once to create the A.I. and then running that A.I. later in the day for the national play-offs if they win the contest at their school. That’s it. No multiple days of programming as they advance each round. No flying on a plane with the additional time required. It’s all in one day, yet they go all the way to the national contest in that one day.

The Internet is your friend

So what are the mechanics behind it? The game uses a client/server model where the game is run on the server and each A.I. is a client. The original reason for this model was to support the A.I. being written in any computer language so each team can use the language they are most comfortable with (we created examples using C#, C++, Java, & Python). When we decided to run the contest across all the top Computer Science schools, this model made the approach workable.

The client/server communication is over TCP. The client creates a connection with the server to join the game. The server then sends requests as XML packets to the client and the client replies with its turn as an XML packet. The server sends a turn request to all clients, then waits N seconds (2 for local games, 6 for the national) for a response. If a response does not occur within the allowed time, then the server creates a random turn for that player for that turn.

The clients are given 1 second to determine each turn. The additional time is to allow for the delay in sending a message over the net, to the client and then the return. The server is totally asynchronous using an event timer to run a turn and receiving incoming messages as events. However the sample clients are written to respond to an incoming message by determining their move and replying in the thread that receives the message. This is the most efficient way to do this – if the A.I. always computes a turn in under a second.

The system is also set up at both ends to automatically restore a connection if the connection is dropped. At worse a dropped connection will mean that client has 1 turn randomly created by the server. We tested the system thoroughly and it works great.

The initial rounds are played at each school. The game takes up to 12 (number may change) A.I.s at once. It plays the game 10 times keeping a total of each A.I.s score across the games. If a school has over 12 teams, they can have multiple playoffs and then have the top 2 from each round play for the school championship.

We then take the top 2 teams from each school (and the top scoring 3rd place teams as needed to fill up to 12 A.I.s per round) and then do the same thing for the national championship.

The Future for Competitions

There’s no reason to go through the additional time and inconvenience in spreading national competitions of this type over weeks and flying people to the same physical location. As time goes on more and more will switch to this decentralized approach because it works better for all involved. This also opens up competitions of this type to everyone, because location doesn’t matter. For The Windward National Intercollegiate Programming Finals (All the excitement of March Madness, without the sweat!), if it works well this year, then next year we will open it up to all schools – worldwide (which will make it the “worldwide” instead of “national”).