Alt.Net Coding Breakfast – April 2013 Edition

Last Wednesday I got the chance to attend a “Coding Breakfast” hosted by Damien Thouvenin of CLT Services. The concept is simple, turn up a little before 8:30, enjoy some coffee and croissant, then we attempt a Coding Kata for about an hour and finally we get together to review the results. I was pleased to see the great and the good of the Paris coding scene there, in the form of Yann Schwartz and Jérémie Chassaing.

The chosen Kata was the “Parking Meter” problem. Create a function to calculate how much time the coins you’ve inserted into the machine buy’s you. The parking meter only accepts 10, 20, 50 cents and 1, 2 € coins. Parking costs 1€ 20 min, 2€ 60mins, 3€ 2 hours, 5€ 5 hours 8€ 1 day. You only need to pay for Parking between 9h and 19h and parking is free on Sundays, public holidays and in august. The problem seems fairly straightforward but the various rules mean there’s actually a fair bit of code need to cover all the cases. Myself and Jérémie choose to do the problem in F# and the others coded in C#. I was the only one to finish in the allotted time (although I did notice and fix a small bug later on) but the other weren’t too far from finishing. I didn’t practice strict TDD which the other’s did, but I did produce a test script that I ran from F# interactive.

I the discussion at the end a couple of interesting points came up, how to handle ‘DateTime.Now’ correctly and whether to store current time paid for and start from this when adding new coins. I believe these are both problems caused by trying to model everything as objects, trying to group things into behaviour plus state, rather than just creating functions that make calculations. The correct was to handle ‘DateTime.Now’ is not use it, you’re function should take DateTime representing the start time and all calculations should be based on this parameter. It’s true you’ll need to observe ‘DateTime.Now’ at some point, but this should be at the last possible moment, probably when you’re handling the “AddCoin” event, or whatever. There is no need to store the current time bought, you just need to calculate it and display it or send it to the ticket printer. The only thing you really need to store is the total of the coins entered, I think it’s good practice to try and store the minimum amount of state possible. The function the calculate the amount of time paid for can be called each time the values is needed, the calculation is so quick it’s not going to cause performance issues, plus you’ll always get a version that has been updated for the current time.

I won’t describe the approach I took, but I’ll show you two version of the problem, the first was what I managed during the allotted time, the second is the result of a 20mins tidy and commenting session, and the various steps of the calculation are explained there.

One last thing before for I show you the code, a big thanks to Damien for organizing the event, I’m looking forward to the next one.

Link to the first version (as I can seem to get github to do an embedded version of a previous revision)

Cleaned up version: