DEV Community

Pan Chasinga
Pan Chasinga

Posted on

Functional Languages are Fad

I don't understand the functional language fad. As much as those liberal nerds despise the term IT, it is my career as much as it is theirs. And in IT, you only use what works. Programming for fun? With passion? I have to put food on the table! I never get those who keep writing code in weird theoretical languages after work.

Let me put it in terms of code, because that's how we will understand the uselessness of learning a functional language.

Functional languages are concise

And that's the problem. In the IT world, especially with contractors, we get paid by the number of lines of code we deliver. Look at this beautiful code written in Java, my favorite language:


public static class SumOfSquaresHelper
{
   public static int Square(int i)
   {
      return i * i;
   }

   public static int SumOfSquares(int n)
   {
      int sum = 0;
      for (int i = 1; i <= n; i++)
      {
         sum += Square(i);
      }
      return sum;
   }
}

Enter fullscreen mode Exit fullscreen mode

That counts 16 lines of code. With a couple more classes and that's a payday. Here is the equivalent in F#, a .NET's own functional spinoff of one of the most hideous languages of all time, Ocaml (Frankly, Microsoft inventing this language to replace C# is downright disappointing):


let square x = x * x
let sumOfSquares n = [1..n] |> List.map square |> List.sum

Enter fullscreen mode Exit fullscreen mode

Holy moly! Two lines of code to do the same? How do you expect people like us to get paid, let alone anyone reading the code? You can even go further and make that a one liner with, ugh, lambda.


let sumOfSquares n = [1..n] |> List.map (fun x -> x * x) |> List.sum

Enter fullscreen mode Exit fullscreen mode

What abomination! What's worse is that language designers these days tend to get brainwashed by this functional cult and can't help hacking some functional features into their language. Even Javascript, which has been around since the birth of the internet, wants to be functional.


let sumOfSquares = n => [...Array(n).keys()].map(x => x * x).reduce((x, y) => x + y);

Enter fullscreen mode Exit fullscreen mode

Indeed, these are dark days to be a legit, serious programmer.

Inspired by Ten reasons not to use a statically typed functional programming language.

Top comments (31)

Collapse
 
eljayadobe profile image
Eljay-Adobe

With such concise code, where do you hide all the bugs?

Everyone knows the big consulting money is in maintenance of the monstrous monolithic shibboleths. Loquacious, garrulous verbosity is the cornerstone of How To Write Unmaintainable Code.

Collapse
 
pancy profile image
Pan Chasinga

Thanks for sharing. I like your first line. I feel the same. With a functional language, you either run it right (most of the time) or not. It's harder to run with bugs or copy paste solutions.

Collapse
 
eljayadobe profile image
Eljay-Adobe

I'm a fan of F#, and really enjoyed the tutorial The Book of F# by Dave Fancher. Did all my learning using Mono and Xamarin, on a Mac.

Fancher's book was much better than the other F# books I've read. I've not read Don Syme's Expert F# 4.0 book yet — reviews seem split between good and bad.

Thread Thread
 
pancy profile image
Pan Chasinga

Thanks a lot for the lead. I've been very interested in learning more about F#.

Collapse
 
jhbertra profile image
Jamie Bertram

You almost had me

Collapse
 
pancy profile image
Pan Chasinga

But really, these are mindsets I've heard.

Collapse
 
skittishsloth profile image
Matthew Cory

Got me at first too :) Though I will say, FP can make things complicated depending on how far down the rabbit hole you go. I've been working on a green field project, a little cluster of wrappers for some legacy SOAP services, using Java and Vavr - monad all the things - and I was super proud of the framework I'd built out. All exceptions handled using Try monads, all nullables handled with Options, lambda and functional interfaces used all over the place. Absolutely beautiful.

Then I had to teach it to some juniors fresh from college and some devs who - while good - don't care about pushing the envelope. If I had to do it again I really don't know whether I'd stick with FP - at least not that deep.

(For the record, they've been great at picking it up, as hard as it's been trying to explain it when I don't understand it that great myself.)

Collapse
 
pancy profile image
Pan Chasinga

You should definitely post about the story.

Collapse
 
igorganapolsky profile image
Igor Ganapolsky

It is interesting that you bring up this topic, Joe. I am an Android engineer, and am seeing a proliferation of functional-style programming with RxJava and Kotlin, etc. I think these technologies are great, but sometimes they complicate a project and slow down developers with steep learning curves.

Collapse
 
pancy profile image
Pan Chasinga

It's meant to be a satire, but thanks for pointing it out. I agree with you to a point.

Collapse
 
kspeakman profile image
Kasey Speakman

Haha. We have a lot of production code in idiomatic F# now. Love it. I was just discussing today how to serialize a union type to JSON legible by others. :)

Collapse
 
pancy profile image
Pan Chasinga

Wow, you should write about it. We never get tired of hearing production story in anything functional.

Collapse
 
kspeakman profile image
Kasey Speakman

I have sortof been doing so, though not necessary in the context of F#.

The thing with functional languages is, you can find a lot of articles and videos extolling their virtues. But they sound like the same promises every other language makes to try to get you to use them. So nobody really believes it. And on top of that you have extra things to learn like expressions-instead-of-statements, union types, immutability (this one is the same as "defensive copying" in OO, except it is the expected norm and there is language syntax sugar). So the idea does not make it through many devs built-in BS filter. I think it really takes someone you trust to tell you: "For real, there is something special about this. You should give it an honest try." For example, being told about type inference holds no comparison to typing out what you figure will be pseudocode, and realizing the compiler already knows the types. Or after a year, you come back to some code and do an epic refactor, but since you used pure functions it turns out to be easy and not risky. Southern saying: "Better felt than tell't."

So getting people to try it (like a real try, not "I gave up when it complained about a missing else.") is most of the battle. I don't know what I could say any differently from all those other videos and articles which talk it up. Just try it people, really try it. It's okay if you ultimately "get it" but still like objects or procedures better -- it's still a useful perspective to learn.

Thread Thread
 
pancy profile image
Pan Chasinga

What I think is really the first enemy of learning (anything, not just functional programming) is herd mentality. When you work at a place where the most people don't view learning new things as important, your learning muscle gets atrophied. When I got an equivalent of a scoff writing something in Ocaml, I faded away.

A specific enemy to learning a functional programming is math. The author of F# for Fun and Profit stated this very nicely, but functional languages need to ease down on math. Stop campaigning lambda calculus. It's not helping, because it attracts snobs who will keep putting functional programming on higher and higher pedestal ("Oh, really, you don't know what a monad is?") and scare away new developers.

Thread Thread
 
kspeakman profile image
Kasey Speakman • Edited

Yeah, I completely agree.

Re:math-focus. Maybe I'll change my mind one day, but this is exactly why I could not bring myself to deploy Haskell code. Even if I could be super productive with it, bringing on fresh devs seems drastically harder than something like F# because of the intertwining of category theory. I would never say this out loud, but since it is just you and me 😉, I have a theory that Haskell is more object-oriented than most OO languages. Because you kinda have to derive from the category theory objects to make canonical use of it. Whereas something like F# or OCaml or Elm, you can just pretend you are doing procedural programming + expressions + immutability + pure functions to get started and be productive. Then later notice that a lot of types use the same functions: map, bind/andThen, etc. So if you understand them once, you understand them everywhere. Then later you discover these operations are not just some dude's arbitrary contrivance, but based on provable math. mind blown. It is a great learning story IMO.

Collapse
 
lambdafalcon profile image
falcon • Edited

[EDIT: ok, so I took a look at your GitHub and realised what this post is. Good job!]

Collapse
 
pancy profile image
Pan Chasinga

Hehe, I managed to summon all the functional crowd here!

Collapse
 
euler2718 profile image
John Corley

By the way....I love your JavaScript range method ;).

Collapse
 
joelnet profile image
JavaScript Joel

Hearts

Collapse
 
rhymes profile image
rhymes

LOL

Collapse
 
bennypowers profile image
Benny Powers 🇮🇱🇨🇦

Came here to troll the author. Left with a spring in my step. Well played.

Collapse
 
pancy profile image
Pan Chasinga

Next time then!

Collapse
 
simov profile image
simo • Edited

Once I saw your functional snippets I knew you were joking. What's even more funny is that lots of people will believe in your article's title.

Collapse
 
tux0r profile image
tux0r

For readability, you should probably split the "two lines" as well. Counting lines might not be the best approach to compare languages...

Collapse
 
pancy profile image
Pan Chasinga

It's just for the sake of satire. :D

Collapse
 
nwpointer profile image
Nathan Pointer

Oh man, you totally got me!

Collapse
 
spencer36063327 profile image
spencer white

You may not see it as readable, but anyone remotely familiar with functional languages should understand it pretty quickly.

Collapse
 
rmoskal profile image
Robert Moskal

Funny!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.