I feel very strongly that F# offers a major step forward in productivity and effectiveness compared to more traditional programming languages such as C#. In fact, if you are a business that relies on custom software development, I am convinced that using F# will be a competitive advantage for you.
These are strong claims to make, but I hope that this page will present you with some compelling evidence.
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.
This might change how I write serious software forever. Period. Thanks @ScottWlaschin #ddd #fsharp http://t.co/LQjWK1JFID
— Michael Seidel (@mpseidel) December 8, 2013
Wrote down an algorithm. Almost every line maps to a s/ingle line of #fsharp. Code is just as readable as text, amazing!
— Roger Kirchwood (@_____C) January 31, 2014
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:
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!
write complex code for 6 hours, compile, run, all works perfectly first time. This happens often. Love you #fsharp
— Ross McKinlay (@pezi_pink) September 6, 2012
@theburningmonk @c4fsharp #fsharp more often than not seems to fulfill: "If it compiles, it works" ;)
— Torben Rahbek Koch (@torbenrkoch) January 26, 2014
I can’t emphasise enough how amazing #fsharp pattern matching is. Moving to a language without it is heinous.
— Dave Thomas (@7sharp9) January 23, 2014
Programming with F# feels like sculpting. Things change shape as you carve and form but you always feel like the code is "correct". #fsharp
— Ben Taylor (@bentayloruk) July 3, 2012
@neildanson: Mads Torgersen: "who here never writes null checks?". A Hand goes up. "Do you use #fsharp?". Yes. Yes I do. #ndclondon
— Neil Danson (@neildanson) December 6, 2013
> @davetchepak "What can C# do that F# cannot?" NullReferenceException :-)
— Tomas Petricek (@tomaspetricek) March 21, 2013
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# 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.
#fsharp software project milestones: 1. write a proof of concept; 2. delete code until it is production ready
— Simon Cousins (@simontcousins) January 15, 2014
"The code is not sufficiently generic." Got to love #fsharp. You'd never see C# or Java begging you to write better code :D
— Nathan B. Evans (@nbevans) January 20, 2014
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.
Behold the full glory of object-oriented spaghetti. Zoom out to appreciate. http://t.co/97earPe8o7 #ridiculous
— Don Syme (@dsyme) June 14, 2013
Fascinating:"... C# dependencies seem to grow linearly with project size, while the F# dependencies seem to be flat." http://t.co/12BBcnp76S
— Fawad Halim (@fawad) June 14, 2013
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.
Religious moment with #fsharp FSharp.Data today. Replaced thousands of lines of custom XML parsing code with two lines. Never going back.
— Darren Platt (@dplattsf) January 25, 2014
After working with F# type providers, I am really really resentful for having to go back and work with Entity Framework. #FSharp
— Jamie Dixon (@jamie_dixon) December 28, 2013
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.
RT @ch9: Understanding the World with #fsharp, #rstats, #Deedle. http://t.co/16cJtclqYv by @tomaspetricek
— Howard Mansell (@hmansell) December 2, 2013
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?
@ReedCopsey besides obvious technical benefits, one thing I loved about F# is how it totally reignited the joy of writing code. #fsharp
— Mathias Brandewinder (@brandewinder) January 30, 2014
@brandewinder @ReedCopsey agreed 100%. It has made programming enjoyable, again. Same kind of giddiness as when I first started programming.
— James D'Angelo (@jjvdangelo) January 30, 2014
@jjvdangelo @brandewinder ... and that's a major benefit that's often overlooked - developer happiness is key to building good products.
— Reed Copsey, Jr. (@ReedCopsey) January 30, 2014
@jjvdangelo Agreed. I haven't been this happy programming in 10 years.
— chris keenan (@chriskeenan) January 26, 2014
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.
(For my $) #fsharp is less about FP/OO/whatever... more about being the best language in the CLR ecosystem (with an awesome community, too).
— Paulmichael Blasucci (@pblasucci) December 19, 2013
I need to reiterate - the #fsharp community is amazing. Great language, wonderfully supportive community - what more could you ever want?
— Reed Copsey, Jr. (@ReedCopsey) January 28, 2014
"I'm naturally conservative and I dislike learning new things. That's why I picked a career in IT." http://t.co/aPDGRZhlnx via @feedly
— Alpha D. (@dialalpha) April 14, 2013