Functional Programming in C# with Language-ext

1.8k Views Asked by At

I have recently started working on a new DotNet Core project. I had noticed that someone (developer) used the Language-Ext library in this project. It was a new thing for me so I got to know that it is for Functional programming. It was quite confusing for me to understand it. I googled it and found benefits of it (Functional programming) :

  1. Less Code (main point)
  2. Easier Debugging
  3. Easier testing

For a OO programmer, functional programming is quite different. So, my question is do we really need to use Language-Ext (functional programming) in C#? Are there any other benefits which I am missing?

2

There are 2 best solutions below

0
Marc Gravell On

"do we really need to use Language-Ext (functional programming) in C#?"

of course not; it looks like some people choose to do that; this is IMO a tiny minority of C# users. If you wanted to embrace functional style, you'd probably look at F# (while C# does offer some minor nods to functional programming, it isn't the speciality of the language). If language-ext adds just that bit more that you want... great? But this is entirely subjective and contextual.

2
stb On

So, my question is do we really need to use Language-Ext (functional programming) in C#?

If you want to do functional programming in C# you don't need LanguageExt, but IMO this is the best library for this use case.

Are there any other benefits which I am missing?

I'm biased because I use LanguageExt for some years now and it's my favourite library. If I create/edit C# projects without this library it feels a bit like working with an ancient C# version.

What more benefits are there?

  • Getting rid of (many1) bugs
  • Getting rid of much boiler plate code
  • Immutable data types
  • Avoiding side effects (easier parallel processing)
  • Easier refactoring
  • More type safety
  • Can be combined with non-FP C# code (use where useful)
  • Better refactoring skills (get another perspective)

LanguageExt creator Paul Louth created a great introduction showing some benefits: https://github.com/louthy/language-ext

The library now has a lot more features and if you get into this (and functional programming at all) you will find many gems.

(On the premise that you want to stay with C# -- and not use F# or Haskell or ...)