Developing in F# impressions

Introduction

I am a C# developer and I have nurtured a wish to start developing in F#. I have read some books on F# and written some test, demo and lab code, but I want to write a real program that solves a real problem. Last week I got a chance to finally do this. This blog post describes my experience with switching from C# to F#. I will not talk much about the languages per se, but more about other things involved in developing.

The Good

  • Writing code in F# makes you think about how you write C# code.
    When I write some F# code I think “could I write this in C#?”, and the answer is often, yes. I will definitely write my C# code in a more functional style from now on.
  • Unit testing F# code is easier than unit testing C# code.
    Testing code that has data in, data out and no side effects is often very simple. Now, of course there are F# code with side effects, but that is more the exception than the rule, and it is very clear when it is side effects you are testing. I recommend using FsUnit, https://github.com/fsharp/fsunit, for your tests.
  • The F# Interactive window (FSI) is great!
    Ever wondered things like “what will the result be if I call this function with this parameter?” or “Will I get the correct result if I write the code this way?”. Just write the code in the FSI and run it. No need to start up the debugger, set breakpoints and modify values. The FSI is like unit testing on a micro level, it really saves you time. More info about FSI here: http://sachabarbs.wordpress.com/2014/02/25/f2-fsi-environment/ and here:  http://msdn.microsoft.com/en-us/library/dd233175.aspx

The Bad

  • No ReSharper support!
    I use almost every ReSharper feature there is when I write C# code. ReSharper does not support F# and I feel handicaped. As a substitute I use “Visual F# Power Tools” https://visualstudiogallery.msdn.microsoft.com/136b942e-9f2c-4c0b-8bac-86d774189cff. It is a very good plugin to Visual Studio and has some, but far from all, of the ReShaper features.
  • No code snippets!
    I use code snippets quite often when I write C#. Visual Studio does not support code snippets for F#, and I really miss that feature.

The Different

  • Type inference.
    In F# you often don’t have to write the type of your variables. The compiler can figure it out. This works great most of the time, but it can be very confusing when it doesn’t. Especially as IntelliSense and the compiler uses different methods to determine the type and might come to different conclusions.
  • Syntax check while you type.
    This is a nice feature, but it doesn’t work between projects. You have to recompile to make it correct.

Conclusion

The good parts about developing F# is that it makes you Think about code in new ways.
The bad parts is in the tooling. With the increasing popularity of F#, the tooling will soon get better.
The different parts are actually good, you just have to get used to them.

 

This Post Has 7 Comments

    1. No boilerplate code in F#? I find myself writing stuff like this all the time

      [<TestClass>]
      type ”MyFunction with 2 and 3” () =
      [<TestMethod>]
      member x.”should return 5” () =
      MyFunction 2 3 |> should equal 5

      A code snippet for this code would be nice.

  1. I use ReSharper live templates with F# and they work well for code snippets… You just set them to Global scope…
    For example:
    fx+tab gives me (fun x -> )
    test+tab gives me some boilerplate test code

    I need to add a lot more and am glad you posted this and reminded me…

  2. I am curious about what kind of project you worked on, in which F# felt natural and beneficial. Please explain the characteristics.

    1. The project was a small program that takes a datafile exported from an external system. It formats and filters the data in a few different ways and writes the result to an excel-file. No UI, just pure data manipulation. Perfect fit for F#

    2. I have used it in a financial application where the application had to talk to a bank api and send some data and consume some. Data fetched would have to be validated, computed and then ingested in the database base in multiple relational tables. I used F# as it was shorter, easier to iterate and I wanted to learn it. It did the job better. I liked the async operations but it has improved in C# as well. I used recursive approach for some logic but it was not really needed and could be done in C# easily too.

Leave a Reply

Close Menu