BDD With C#, F#

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)

  1. 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
  2. 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] |> 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!

2 thoughts on “BDD With C#, F#”

  1. En espérant revoir la nouvelle version du site bientôt, voici les commentaires que j&v1782;a#ais essayé de publier dans le post effacé :Ce design est vraiment top. Plus épuré que le précédent, les widgets sur le coté sont mieux organisés Un petit bémol, il y a une ligne blanche qui apparaît à droite de certains titres de widgets … (block-t.png)

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see