This will probably interest .NET folks more than others – I have created a couple of github projects (here are the links for C# and F#) and uploaded the code – (you will have to install Specflow and F# for the samples to check these out, or you can just browse it at github)
- BDD using C#, with SpecFlow – there are still some Unit tests, but BDD provides the overall acceptance tests. ATDD (Acceptance Test Driven Dev) is really a step forward from TDD. If you open and read the BDD scenarios (*.feature files), you will see that they also double up as a spec for the library, and can be easily reviewed by BAs/Customers directly
- The same sample ported to F# – the functionality can probably be much better written in F# However, even with just porting same logic, the amount of code written is almost one-third that of corresponding C# code. (Same C# test harness is used in both the projects). I am still new to F# so with some experience we could probably write much more efficient code than this (in which case the difference will be still higher!)
For those who don’t have time to go through the samples, this following line demonstrates the power of functional programming –
let daysInBetween = [this.Year.AD + 1 .. secondDate.Year.AD - 1] |> List.map(fun p -> (new Year(p)).DaysCount) |> List.sum
(means: for all years between this dates’ year and second date’s year -> for each of them create a new year object and get days count -> sum it all up)
As the tests show, the types can be easily consumed from another C# layer as classes (say for eg, an ASP.NET MVC website, which currently does not support F#). Also, contrary to popular belief, OOP is a subset of a functional programming, since you can still define types (similar to classes) but you can also have functions without having a class to house it. And Besides just being pretty, functional languages also improve concurrent programming and hence let your code derive maximum benefit from multi-core processors.
You can also mix and match – for instance it is just as easy to write the BDD test expressions in F# for testing C# code instead of vice versa.
Hopefully these samples will encourage more people try out F# especially where a lot of list based computation is involved, as well as try BDD wherever possible in your next project!