Why I like F# over C# – Less Noise


Oh, jingle bells, jingle bells
Jingle all the way
Oh, what fun it is to ride
Oh shit a blog deadline ♫

Introduction

I did my first ever session a few months ago, I did a session called “Why you should learn F#”. I spent 30 minutes sweating and stuttering about pipelines and discriminated unions, people reactions were “Ok nice, but why is it better than C#, why do I need this, show some more code comparisons”. At first, this is what I NOT wanted to do, go the F# is better then C# blablabla way. I don’t hate C#. But I would like F# to be the language I use for projects at work, so if this is the way to convince people/companies to use F#, so be it.

I’m not a playa I just F# a lot – Big Pun

Succint Code

So in this blog I’ll start with a simple example why I like F# over C#. It’s how small and clean the code looks, because there’s less ceremony. This is because of:

  • Less curly brackets
  • No semicolons
  • Records instead class
    • Not getters &setters
    • Immutable by default
  • Not every fart in a separate file

To show the difference I simplified the code from a tool I wrote in F# and translated that to C#. I kept the C# in the same file, that removed some more code you normally had to add if you follow the C# code guidelines. The screenshot says it all I think, you can see how many, unneccesary, noise is added to the C# code.

Code from FolderTimeLine.fs to make sense of the code in screenshot 😉

That’s all for now, this is just one argument of the F# over C# story. Sorry I had to squeeze this one out a little, last year when I saw the fsadvent and I was like “Next year, I’m going to write an awesome blog about how I learned F# by blogging this year”. Well I learned some F# and did some blogging, but waaaayyy less then I hoped. So next year, I will again try to make the world safe for fsharpocracy.

5 thoughts on “Why I like F# over C# – Less Noise”

  1. There’s one mistake in your F# code: you meant to write processFiles as a function, but you wrote it as a value instead. Which means it will be evaluated once, at the moment your program runs, and then never evaluated again. To make it a function, give it a parameter: turn let processFiles = ... into let processFiles () = ... and it will be a function.

  2. I’m a big fan of F# myself, but from an old C developer, most of the C# guff can be dispensed with. Make the props public fields, and put the methods into a single static class. Removed the 2 unused variables. You could also put the opening braces on the same line as the class def….

    F# is undoubtedly better in the functional paradigms it makes “successful” developers use. Just non of this smancy source code comparisons that are not 100% equivalent…

    … just saying….

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.