Live data from Hacker News

Functional programming in JavaScript is an antipattern

hackernoon.com

71–80 of 92 posts

Re: Functional programming in JavaScript is an antipattern

#71

Earlier quoted context omitted.

I was taught in university that functional programming means using functions as first-class-values, so function can get other function as a parameter. That's all. You can write functional code with assembler if you want. Immutability is just another design choice with its pros and cons.

The immutability comes in with the definition of 'function', which, in some circles, means something more specific than an arbitrary unit of code that is callable and which may or may not return a value. While neither 'function' nor 'functional programming' can be used in computing with any precision unless they are qualified, there is a distinct paradigm in which functions have no side-effects and are the only mecha…

Mathematically a Function is a Relation that has two special properties: uniqueness and existence.

(these properties names can be wrong as I've not study calculus in English)

So on the machine, Immutability plays an important role on the first piece, uniqueness, so it tries to guarantee that results are not just "equal objects, but different instances", which breaks the function definition.

Of course that functions that return a new object for the same arguments, even if it's immutable, are not true math functions.

Re: Functional programming in JavaScript is an antipattern

#73
post #70
post #28

I've been doing FP in JavaScript fulltime for a few years now. I find it much easier than the OOP approach. My advice to the author is: choose ImmutableJS, Ramda, OR lodash. Don't choose all three. I think that is his anti-pattern. > I could be more productive if I didn’t have to wonder things like ... “Should I mutate this variable?” If you're doing FP the answer is likely 'No.'

Or? Ramda doesn't even have the same functionality as Lodash.

There is however enough overlap that using both ends up being a mess.

Re: Functional programming in JavaScript is an antipattern

#74
post #30

Funny, but HN oldsters probably remember when functional programming wasn't at all tied to immutable data structures, and when Lisp and derivatives where considered enough functional programming, without every discussion of the topic requiring strictly requiring purity and immutability -- just first class functions, map, fold, and the like.

Yeah, and---literally, not to be snarky---times change.

FP isn't a technical definition. It's a cultural, family-resemblance definition that's driven by some mixture of fashion and genuine exploration into a niche of ideas in computing and formal languages.

The best you could hope for would be a set of shared values of the FP culture and then justifications for why various things that call themselves FP feel that are upholding those values. Even with a definition like this though, the values will shift and waver over time.

Here's my FP values:

    * Simple over easy.

      Rich Hickey's old egg. Favor abstractions with few moving parts,
      reduced interactions, and more parsimonious overall models over
      ones driven by metaphor or target use case alone. Result is 
      tools which are perhaps harder to get started using but have
      better complexity scaling properties.

    * Mathematics has been doing this a long time.

      PLs are just formal languages and mathematicians have been working
      seriously on formal languages for at least 150 years: steal their
      ideas. From this we get ideas around comparative linguistics, 
      semantics, various proof/reasoning mechanisms and types.

    * Readability counts, but not just in the sense of syntax.

      Really this means "legibility" or even "static legibility". It's 
      another hint toward types, immutability, and general state space
      reduction. It's a strong push away from "emergent behavior" to the
      greatest degree possible. Things should endeavor to do what they
      say on the tin... and to to the greatest degree reasonable say things
      on the tin in such a way that the information is available from the get-go.

    * Tradeoffs between power-to-construct and power-to-analyze.

      This is everywhere in formal languages/PL, but it's especially strong
      in FP since there's a focus on (simple over easy) semantics. It opens
      the door for there to be mathematical semantics as opposed to just
      operational semantics and this makes for rich opportunities for on-the-tin
      static reasoning (equational reasoning, changing interpreters, embedded DSLs).
 
I think these values more or less were in place back in the old Lisp-y FP days, but they really are taken to new places by "more modern" takes on FP.

Re: Functional programming in JavaScript is an antipattern

#75
post #55
post #30

Funny, but HN oldsters probably remember when functional programming wasn't at all tied to immutable data structures, and when Lisp and derivatives where considered enough functional programming, without every discussion of the topic requiring strictly requiring purity and immutability -- just first class functions, map, fold, and the like.

Language is a tool for communication, and the great strength of English is that it is defined by consensus. Words shift in meaning.

Which is neither here nor there regarding my original observation, which didn't deny that "words shift in meaning" but called to attention (or to memory) a particular shift in meaning.

That said, and being a pedant, I must say that while English (or any language) does indeed change, it doesn't do so by any kind of consensus. People don't stop and give consent to a change in meanings, they roll along with it. There are some mechanisms in this shifting of meanings (needs to describe new concepts, fashion, immigration, etc) but consensus is not one of them.

Re: Functional programming in JavaScript is an antipattern

#76

The author asks WHY hasn't clojure caught on if it is great. I think I have a better answer than his conclusion that it aint popular because people shun things that aren't popular. (1) coding when EVERYTHING is immutable by default is a royal pain in the arse. (2) Coding directly in Abstract Syntax Trees is not pretty, there is a reason why most programming languages don't look like lisp.

Coding in nearly the cleanest possible notation for abstract syntax trees is simply breathtaking.

The reason a lot of programing languages don't look like Lisp is that most programming languages don't have anything interesting semantically going on. Their authors' primary source of intellectual pride is the work that went into the syntax.

Part of it is CS education. It is drilled into the heads of CS undergraduates that programming language design involves tokenizing, and parsing with LALR(1) or what have you. The idea that you're going to have those pieces there if you design any language for any purpose is deeply ingrained, like the idea that no matter what you will be coding, you're going to have modules with functions that have local variables, and that there will be a edit-compile-debug cycle, and so on.

Re: Functional programming in JavaScript is an antipattern

#78
post #71

Earlier quoted context omitted.

The immutability comes in with the definition of 'function', which, in some circles, means something more specific than an arbitrary unit of code that is callable and which may or may not return a value. While neither 'function' nor 'functional programming' can be used in computing with any precision unless they are qualified, there is a distinct paradigm in which functions have no side-effects and are the only mecha…

Mathematically a Function is a Relation that has two special properties: uniqueness and existence. (these properties names can be wrong as I've not study calculus in English) So on the machine, Immutability plays an important role on the first piece, uniqueness, so it tries to guarantee that results are not just "equal objects, but different instances", which breaks the function definition. Of course that functions t…

Mathematical functions aren't too great a metaphor for computer functions. They share a lot of similarities—domain, range, inputs, outputs, composability. But mathematical functions just are, there's no notion of changing things—however, the most common purpose of computer software is to change things. So it breaks down, or you come up with some way of defining a change without anything changing, which adds a layer of abstraction to everything you try and do.

Re: Functional programming in JavaScript is an antipattern

#79
Don't use immutability in JS. Though, use Ramda, since it's much better than plain JS.

This, then, would solve almost all of the problems mentioned in the article, yet remain functional friendly.

Ramda functions never mutate the object by always returning a new object, this is very simple to reason about, and it's pretty close to immutable programming without the overhead yet only one API.

Re: Functional programming in JavaScript is an antipattern

#80

I hate the arrogance of announcing that thousands of developers' work is an antipattern. This post contains valid discussion of some of the shortcomings of FP-in-JS, and presents alternatives. That's fine. But everyone has their own priorities and tradeoffs. The downsides of adopting Clojurescript in my current situation would outweigh the benefits. > Clojure compiles to Javascript... So any Javascript job could be a…

Why ClojureScript and not PureScript?
Post reply on HN