♫ Oh, jingle bells, jingle bells
Jingle all the way
Oh, what fun it is to ride
Oh shit a blog deadline ♫
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
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.
6 thoughts on “Why I like F# over C# – Less Noise”
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.
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….
I get that I think, the point I was trying to make was not the code, but the noise around it. The code doesn’t make too much sense I agree, it a bit pseudo.
I think c# isn’t that much more complicated?
record RenameFile(string FileName, string newLocation, RenameFileStatus status, DateTime dateTaken);
const string SOURCE = @”C:\Temp\Source\\”;
const string DEST = @”C:\Temp\New\\”;
void FileCopy(string s, string d) => File.Copy(s, d + Path.GetFileName(s));
void MoveFiles(string s, string d) => Directory.GetFiles(s).Select(x => FileCopy(x, d));
void ProcessFiles() => MoveFiles(SOURCE, DEST);