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:

Bookmark
dotnetkicks+, digg+, reddit+, del.icio.us+, dzone+, facebook+

Print | posted @ Saturday, April 27, 2013 2:08 AM

Comments on this entry:

Gravatar # re: Alt.Net Coding Breakfast – April 2013 Edition
by Rui at 4/27/2013 3:30 PM

Glad to see that you enjoyed this coding breafast, i miss that because of the idddtour, and be sure that we'll do many others in the future ;-)

Just about the Datetime.Now. It's clearly an important point that most people don't consider. I think you're advocating the right way for managing that. But maybe it's too functional style for most people and also may add more complex function calls.
Btw, most of the times, I achieve that with creating a simple configurable class that I use instead of Datetime.Now and most of the work is done, it should looks nealy like that:

public class ApplicationTime
{
private static Func<DateTime> configuredTime = ()=>DateTime.Now;

public static Func<DateTime> Configure
{
set
{
configuredTime = value;
}
}
public static DateTime Now
{
get
{
return configuredTime();
}
}
}

cheers!
Gravatar # re: Alt.Net Coding Breakfast – April 2013 Edition
by Mathias Brandewinder at 4/28/2013 10:11 AM

That sounds like a fun format, except for the 8:30 am part! As a result, I registered to the Meetup group, hopefully one of these will take place next time I stop by Paris.

Your comment:

(Note: all comments are moderated so it may take sometime to appear)

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 1 and 6 and type the answer here: