Alt.net Coding Breakfast – June 2013 Edition

Two weeks ago on Wednesday morning, I went to the latest edition of Alt.Net Coding Breakfast, hosted by Damien Thouvenin of CLT Services. It was my first time there, and I was very pleased with it. The principle is quite simple: several .Net coders meet in the morning, and practice their skills by attempting a Coding Kata during an hour. We then review together what was done, what went well and what went wrong. It is an exercise that I find very useful (and fun), that I usually do during Arolla’s Code Jam on Thursdays. This coding breakfast’s main advantage is to meet with other .Net practitioners (as usually the Java guys are predominant) and that might be the only place in Paris where you can see several people coding in F#. That was also true last month, as you can see on Robert Pickering’s blog.

This month, the kata was about printing user-supplied pictures:

of any size (in pixels),

in a given format (10×13 cm, 11×13 cm, 13x15cm, 15×18 cm, 18×24 cm…)

with a given filling mode (“full-paper” or “full-image”)

The printer always prints at 300 dots-per-inch, so the images must be resized in order to fit in the available printing space.

The expected output data, needed for printing the images, would be:

the output printer (13 cm width or 18 cm width printer),

the rotation applied to the picture (0° or 90°, the direction doesn’t matter),

the expansion/reduction ratio to apply to the picture,

the position where the picture must be drawn.

The kata’s description also involved some drawings in order to explain the meaning of the filling modes. There was, for instance, a picture of Winston Churchill. Unfortunately, there was not a single kitten picture. I fixed this, in order to provide samples illustrating the two filling modes.

Here is a train picture in full-image mode:

The same picture in full-paper mode, where the red areas will be cropped at printing:

And the same with the kitten, full-image:

And full-paper, with the cropped areas in red:

The exercise chosen by Damien for this kata is really an interesting one, as it is in fact quite simple, but there are many ways to misunderstand it and make it more complicated than it really is. Although we were not so many coders, there were several different techniques, problems and outcomes:

some people paired, some worked alone,

some worked in TDD, some didn’t (but we all wrote tests!),

some of us didn’t consider the print format as an input but rather as an output (trying to determine the most appropriate format for a given picture),

some of us took much time trying to define data types, and in the end had not enough time to handle the core business problem of the image resizing,

some others (amongst the C# devs) used dynamic types for prototyping, so they were able to write the logic first, rather than focusing on the types.

As to myself, I had come to this breakfast with the goal to code in F#, so this is what I’ve done. I paired with Peter Even, who I hope is now addicted to F#! In order to focus on the core problem, we stated that:

selecting the good output printer given the print format is trivial,

selecting whether or not we must rotate is not very complicated (although we could get it wrong),

the ratio computation is the tricky part.

Here is what we came up with (with the French words translated) for the ratio computation. First the unit tests:

determines, from the filling-mode, which operator to use in order to compare the ratios,

performs the comparison with the operator and determines from that which side of the image must be used to get the correct ratio (but the return type here is directly a function, where a less-functional approach would have returned a boolean for instance),

uses the resulting function to extract the length and compute the ratio.

It sounds more complicated (to my mind it is, in fact), but I still find that beautiful from a functional perspective!

While writing this blog post, I could not resist the will to go further and investigate the other parts of the problem, because I so much wanted to be able to pipe my functions! In the end, the assumptions we made during the kata were not wrong, but reasoning about the position where the image must be drawn is not as simple as I first thought!