Mitch Etter’s Weblog

November 5, 2010

F# vs. C# lines metric

Filed under: .NET — mitchetter @ 5:52 pm

I’ve been reading documentation on F# lately. It sounds really cool, but it just rubs me the wrong way when people start comparing it with C#. I understand the differences that F# is a functional-orientated language and C# is object-orientated. Each have their own advantages and disadvantages in various situations.

But what really gets my goat is when people compare lines of code between the two because they inflate the C# line count. One, they introduce unnecessary methods, properties, etc. Two, braces on their own line are included in the line count. Three, they suddenly forget about anonymous delegates and lamda statements. Let’s look at this F# example (I got this one from http://2009.scandevconf.se/db/FSharp-Intro-v1.ppt):

let data = (1, 2, 3)

let f(a, b, c) =
    let sum = a + b + c
    let g(x) = sum + x * x
    g(a), g(b), g(c)

Now, let’s write it in C#:

var data = new[] { 1, 2, 3 };

static void F(int a, int b, int c) {
    var sum = a + b + c;
    var g = delegate(int x) { sum + x * x; };
    g(a); g(b); g(c);
}

F# does have the ability to be more concise than C# (I do have to admit that), but don’t try to skew the numbers!

Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.