views:

3030

answers:

9

I write client-server based business applications using .Net and C#. Given this, how would F# enable me to write better code? "Better" in any sense, e.g. faster coding, faster execution, better reliability, more flexibility.

In short, why should I use F#?

+1  A: 

The generic answer to this question is: learning a new style of programming will make you a better programmer.

The more specific answer is: F# will help a lot for certain classes of applications, but may not do much at all for line-of-business apps. F# is well suited for math and science applications. The functional nature of F# can also help you write more reliable concurrent programs. Given the increasing number of multi-core processors, this might be quite valuable to you.

If you expand on your question, I can provide you with a more detailed answer.

MrKurt
+1  A: 

F# has two benefits: the general benefits of functional programming and the ability to use .NET libraries.

Functional code minimizes state, and that makes it easier to test and easier to run concurrently without errors.

You could write C# and promise yourself that you'll minimize state, but the F# language enforces that promise and makes you be explicit about introducing state. It also makes it easier for you to use functional techniques, though again you could do the same in C# with lots of discipline (and more verbose code).

John D. Cook
+1  A: 

Microsoft has hinted strongly that they will be giving parallelism via the compiler for functional languages in the near future. It turns out that compiler-driven parallelism is a much easier problem to solve with functional programming. If you get to know your functional, you enable yourself for parallelism when the feature becomes available.

Brian Genisio
-1: "Microsoft has hinted strongly...It turns out that compiler-driven parallelism is a much easier problem...". Err, no. Two researchers at MSR claimed this would be easy because they hadn't done their research. Nothing of use ever materialized.
Jon Harrop
+8  A: 

F# is a remarkable language for prototyping for the following reasons:

  1. F# has an interactive mode allowing you to evaluate blocks of code directly, without compiling your entire project.
  2. Type inference helps keep code small, and makes refactoring your type hierarchy relatively painless. This may not be so important in production code, but I found that to be very valuable during prototyping.
  3. F# integration with .NET makes it easy to prototype extensions of your existing products. In the all-too-common case when a prototype becomes a product (due to time constraints), it's also easy to integrate your F# code within your .NET product.

If prototyping makes up a significant part of your overall development process, then F# can really help you speed up your coding.

I don't think F# will produce code that is significantly faster than other .NET languages. The functional style of programming, in particular purity (no side-effects), can be applied to other programming languages, meaning it is just as easy to write concurrent programs in other languages as well. It does however "feel more natural" to do so in F#.

F# has the Option type, which can be used in place of null values. Code reliability with respect to null-pointer exceptions can be guaranteed at compile time, which is a huge benefit.

Finally, be advised that F# is still in development, and suffers issues, some of which may disappear over time, but not all. See for instance what devhawk and Oliver Sturm have to say about it (in particular about linear scoping and interdependent classes, other issues like overloading, better Visual Studio integration have already been addressed).

Joh
After trying F#'s type inference, writing C# becomes a PITA.
Mauricio Scheffer
+1  A: 

I personally use F# just because it is interesting for me. People oftentimes forget that interest in learning something new can be perfectly valid reason and is a very strong driving force. If this is the case, also consider learning Lisp as it does not suffer some issues F# does (it is still work in progress).

petr k.
A: 

I'm from a C# background, but I took the time to play with F#, read a few books, etc - and I'm glad it is. I don't happen to work in any of the areas in which F# particularly excels - and so in reality I don't ever expect to do any serious work with F# - however, the experience made me think a bit more about how I use C#.

The good thing is that C# can already be used in a functional way in many cases - especially now with lambdas to make the syntax easier. It isn't quite the same, and some of the tricks aren't avialable or as convenient, but C# can hold it's corner well enough.

The C# team could make immutability easier, but we're getting there. With the Parallel Extensions moving into .NET 4.0, and named parameters to make immutable constructors more versatile, there are plenty of reasons not to ditch C#.

Marc Gravell
I'm curious what areas you work in where F# is not a significant advantage?
Jon Harrop
+2  A: 

I write client-server based business applications

In addition to all of the usual benefits (interactivity, algebraic data types and pattern matching, first-class functions, type inference etc.), your particular domain should benefit enormously from F#'s asychronous workflows.

Jon Harrop
+2  A: 

client-server based application

I think you might want to have a look at the following video of Luca Bolognese's presentation.

It is in my opinion a good overview of where it should be useful for you.

Especially with asynchronous execution handling and parallelization.

JSmaga