Sometime last year, I wrote an article demonstrating BDD with C# and F#. The accompanying source codes were hosted on github.
Now I’ve been doing some more functional programming recently and took an opportunity to revisit the F# code I had written then. As it turns out, I had written the first version in Object oriented style in F#. Switching to functional style has made it even more elegant and concise. The difference – 55 lines of code, versus 120 earlier (versus some 200 odd lines of code in C# for the same functionality).
Some of the differences –
Using Records instead of Types to encapsulate data (e.g Date)
Using base types directly wherever it makes sense (for e.g. int for Year instead of a separate Year type)
Using independent functions rather than enclosing them within types
These three go together – functions as first-class citizens means that we no longer need to define types just to house them.
Using simpler syntax – for instance, avoiding unnecessary braces
Using more powerful language features – using type inference, pattern-matching, higher-order functions a lot more aggressively
Some cheating – smaller variable names, fitting more on one line (both, only where it doesn’t affect readability).
I find the new version a lot simpler to understand, with very little distractions such as instantiating objects or trying to access their properties.