F# means well-designed code


The conciseness and clarity of F# means that non-technical users can contribute to the design process, leading to faster turnaround and less rework.

Complicated requirements can be sketched out up front, and entire designs can fit on one page.

If you are doing Agile with an on-site customer or creating a DDD style ubiquitous language then why mess around with UML diagrams and glossaries? Why not just use a programming language that supports domain modelling directly?

For example, here is some real F# code (not pseudocode) that is a design for a card game. Do you think a non-technical person could understand it and contribute to the design?

module CardGame 

type Suit = 
  Club | Diamond | Spade | Heart

type Rank = 
  Two | Three | Four | Five | Six 
  | Seven | Eight | Nine | Ten 
  | Jack | Queen | King | Ace

type Card = Suit * Rank

type Hand = Card list

type Deck = Card list

type Player = 
  {Name:string; Hand:Hand}

type Game = 
  {Deck:Deck; Players: Player list}

type Deal = 
  Deck -> (Deck * Card)

type PickupCard = 
  (Hand * Card) -> Hand

And here is some F# code that is a part of a design for a contact management system. Could you have fit 7 classes on one page with C# code?

Can you tell what's the maximum length of the last name field? And which fields are optional? Could you have done that with C# code?

type Contact = { 
  Name: PersonalName
  PrimaryContactInfo: ContactInfo
  SecondaryContactInfo: ContactInfo option}

and PersonalName = {
  FirstName: String50
  MiddleInitial: String1 option
  LastName: String50 }
    
and ContactInfo = 
  | Email of EmailContactInfo
  | Addr of PostalContactInfo

and EmailContactInfo = 
  | Unverified of EmailAddress
  | Verified of VerifiedEmail

and EmailAddress = 
  EmailAddress of String100
    
and VerifiedEmail = 
  VerifiedEmail of EmailAddress

and VerificationService = 
  (EmailAddress * VerificationHash) 
      -> VerifiedEmail option

For more on domain driven design using F#, see these slides from my talk at NDC London 2013.


F# means correct code


The strict type checking of F#, combined with its clarity and concision, means that most errors get caught during development. The type system acts as a "compile time unit test". Testing can then focus on important business cases rather than time-wasting things like checking for null references.

The built-in features that prevent common errors include:

  • Optional values instead of nulls, so no NullReferenceExceptions.
  • Pattern matching everywhere, so no accessing empty lists by mistake.
  • "Choice" types for creating alternatives, with checks to see that you handle every possibility, so no forgetting to handle new cases when the requirements change.
  • High level collection functions, so no off by one errors in loops.
  • Immutable values by default, so you can never have an object become invalid or be unexpectedly changed.

This list is just the tip of the iceberg. There are other, more profound reasons why bugs don't like F#, but they won't fit on a bulleted list!


F# means less code


In F# the amount of code that you need to write is much lower than the equivalent in C#, often by a factor of 5 or more. Less code to write means higher productivity, fewer bugs, and lower maintenance costs.

Here's a real example. The pie charts below show the number of lines for two similar projects, one written in C# and one written in F#. The C# project has less functionality in it than the F# one, but has 5 times as much code, as you can see by the relative sizes of the pies. Moreover, in the F# project, over 80% of the code is useful, compared with C# where almost half of the code is "noise".

C# code size
F# code size
C# project F# project
Useful lines 4610 (50% of total) 1436 (84% of total)
Null Checks 195 (2%) 4 (0%)
Blank lines 1386 (15%) 94 (6%)
Brace Lines 2686 (29%) 11 (1%)
Comment lines 290 (3%) 164 (10%)
Total 9167 1709 (5x less than C#)

For some independent evidence, using a completely different project, see this post by Simon Cousins.


F# means simpler code


The overall structure of an F# program is simpler than an equivalent C# program. F# programs tend to be written in a linear style, so there is no spaghetti code. And there are many fewer classes, so the overall structure of the program is simpler .

Here are two diagrams showing the overall structure in an F# and C# program that are roughly equivalent in functionality, with the relationships between classes indicated by arrows.

The first is the C# project (actually only some of it). You can see that there is a tangle of relationships between the classes (and this is good well-factored C# code by the way)

The second is the F# project. You can see immediately that there are many fewer dependencies, and the relationships are much more linear. Which project would be easier to maintain, do you think?

For more on these two diagrams, and to zoom in on the raw data, see this post by Phil Trelford.


F# supports information-rich programming


We are entering a new information-rich world, one that provides huge opportunities for programmers to explore and create exciting applications. F# is a leader in this area.

F# type providers allow you to talk directly to data, combining the power of dynamic code generation, the safety of static types, and the ease of use of intellisense. Some examples are the XML type provider, the JSON type provider, and the SQL type provider.

And F# has powerful tools to analyze and process data, such as Deedle (for data and time series manipulation) and the R type provider which makes it possible to use all of R's capabilities from the F# interactive environment, including on-the-fly charting and data analysis.


F# will make you happier


Learning functional programming (via F#) is a lot of fun, and will make your team happier. The tweets below speak for themselves.

Yes, you can't have fun all the time. But if you are enjoying yourself, then you are more likely to go the extra mile when needed.

What's more, using F# may help you attract talented developers. After all, wouldn't you want to work with people who are having fun?


F# is a safe choice


F# is the safe choice for functional-first development on the .NET platform, and is rapidly becoming a popular language for all kinds of .NET programs, not just financial or scientific ones. F# is now number 16 in the TIOBE Index for January 2014, ahead of Scala and Haskell.

If you are worried about hiring, there is no shortage of developers who want to work with F#. The F# user group in London has almost 700 members and the New York one, almost 600.

F# on Windows is, of course, fully supported by Microsoft, and has been part of Visual Studio since 2010, so chances are, you already have it on your desktop. MSDN has excellent F# documentation and resources.

F# is also supported by Xamarin, which covers the mobile and MacOS platforms. Finally, the F# compiler is open-source and works with Mono on Linux. No matter what your platform, F# is available for it.

You won't be alone if you choose F# — there is a great community on Twitter and Stack Overflow which is very welcoming to beginners.


Choose your own adventure!


Almost convinced?

Not convinced?