Live data from Hacker News

Functional programming in JavaScript is an antipattern

hackernoon.com

11–20 of 92 posts

Re: Functional programming in JavaScript is an antipattern

#11
Coming from Clojure to ClojureScript, I must say I have had some hard times with the dynamic, mutable, APIs that a lot of js libraries have.

Even though interop is possible and not difficult, I am so much more comfortable using cljs wrappers (there are a lot) or in my part of the world (cljs - land). One reason is that the clojure way (tm) is to use immutable data and pure functions. It bugs me to enact stateful changes.

One way out is to have a datastructure that stores the state of the entire browser (what url am i on, what's in local storage?) and change the datastructure while some unspeakable side effecting function observes and updates either localstorage, or browser history, or etc.

In Clojure 90% of my code is datastructures, manipulating them, and sending them back. Partly because servers tend to be way less stateful than UIs, and partly because it feels like there is more to explore in Clojure without going into java, compared to cljs/js.

Re: Functional programming in JavaScript is an antipattern

#13

I like Javascript. As a language. It's fun, enjoyable, and I'm productive in it. There I said it. I don't mind exploring interesting avenues like immutability, jsx, typescript, declarative or functional styles. I like that JS has enough flexibility to make all of these possible. I get the fatigue about the endless stream of new frameworks, libraries, approaches, and "new hotness". But I became much more relaxed about…

LISP isn't the ultimate programming language because you can write all others in it. You can write any Turing-complete language in any other.

Re: Functional programming in JavaScript is an antipattern

#14
I have a few large React apps that have been using mostly Immutable.JS from the very beginning, so I rarely run into issues where I need to think whether what I'm dealing with is an Immutable.JS object or plain JS objects. I can see an issue if you are incrementally converting an app to Immutable.JS and dealing with a mix of plain JS objects though.

I have heard criticisms of lisp macros as making it a write-once language. If you're not working on a team then maybe it's fine, but other people aren't gonna want to figure out how your macros work.

Re: Functional programming in JavaScript is an antipattern

#15
This post struck home with me. But I realized that the root cause for the problem is the many ways to do FP (and, notably, immutable) in JS. What doesn't help here is that all ways have some major downsides. ImmutableJS has a huge API that works pretty different from everything else. Ramda changes all the time. Lodash, by virtue of not changing all the time ever since underscore was first released, has some pretty weird and unexpected function names and behaviors.

If JS had more builtin functional primitives (i.e. well beyond Array.prototype.map and friends) this problem would be much smaller.

Similarly, I think another way out of this antipattern is to settle with the team on a single way to do FP. This disqualifies ImmutableJS by definition, because 3rd party apis tend to want arrays and objects. But most other options are open, I guess.

Re: Functional programming in JavaScript is an antipattern

#17

I like Javascript. As a language. It's fun, enjoyable, and I'm productive in it. There I said it. I don't mind exploring interesting avenues like immutability, jsx, typescript, declarative or functional styles. I like that JS has enough flexibility to make all of these possible. I get the fatigue about the endless stream of new frameworks, libraries, approaches, and "new hotness". But I became much more relaxed about…

When you say you like JS as a language what do you mean ? ES5 or ES6 or ES7 or transpiling ?

Re: Functional programming in JavaScript is an antipattern

#18
I wouldn't go as far as to call it an anti-pattern, but the lack of native persistent data structures does make functional programming a decidedly second-class citizen in the JS world.

We can either choose to use a library like ImmutableJS instead of regular JS objects and suffer the impedance mismatch and exponential increase in verbosity, or use regular JS objects and make full copies every time we want to change any piece of data.

It's a very unfortunate blemish on an otherwise surprisingly pleasant experience (functional programming in JS), especially when paired with a nice utility library like Ramda [1].

I tried to look into if native persistent data structures was on the roadmap for a later version of ECMAScript, but this random proposal on GitHub [2] was the only thing I could find, and I'm not familiar enough with the standardization process to be able to gauge how much traction that proposal is getting.

If anyone else is aware of similar efforts, I'd love to hear about them.

Until something like this makes it into JS proper, I'll be putting my weight behind ClojureScript.

[1] http://ramdajs.com/docs/

[2] https://github.com/sebmarkbage/ecmascript-immutable-data-str...

Re: Functional programming in JavaScript is an antipattern

#19
What has happened to Javascript is the same thing that happened to other fundamental high-level languages before them. It has changed from a high-level language to an assembly language just like C and Java.

Of course you canb choose to live in the past and keep on hand-rolling your Javascript just like others do with C and Java, but the world has changed. In today's world we write our code in a high-level language and have an optimizing compiler emit the assembly/machine code that our compute engine executes. The details are a bit different for different compute engines.

For C, the high-level compiler might use the same backend as the C compiler to emit machine code for x86 or ARM, but it will make integrating C libraries easy to do. For Java, the high=level compiler may emit Java class files directly but it will make integrating Java libraries easy to do. For Javascript you have Scala.JS and Clojurescript emitting optimized Javascript because the compute engine executes that directly.

As a developer it is worthwhile knowing all these low level details of internals because you will need to debug issues with that level of knowledge. But your main job should be to implement tested stable functionality for the users of your application. No matter how much you like C or Java or Javascript, it is always faster, more efficient and more reliable to write in a truly high-level language that incorporates the best knowledge of the latest research in Computer Science.

As a Javascript expert you may find that Clojure slows you down at first. That is the investment period. Then you start picking up the tempo, enter the payback period, and people start whispering about you being a rocket scientist or 10x developer.

Of course you could just stick woth Javascript but others who know how to roll with the punches will change with the times and win the 10X crown. Not because they are better, but because a true 10x developer is just a person who uses the best tools. They used to say that the suit makes the man. Nowadays it is the tools that make the 10X developer.

Re: Functional programming in JavaScript is an antipattern

#20
post #14

I have a few large React apps that have been using mostly Immutable.JS from the very beginning, so I rarely run into issues where I need to think whether what I'm dealing with is an Immutable.JS object or plain JS objects. I can see an issue if you are incrementally converting an app to Immutable.JS and dealing with a mix of plain JS objects though. I have heard criticisms of lisp macros as making it a write-once lan…

This is why a professional developer needs to document their code. If they don't do that, then they are NOT a professional.

Plus they are selling themselves short. If you want to fully learn something you must teach it. By documenting your code you are teaching the other developers on the team and this will lead you to much deeper understanding than the hackers who churn out code all day.

Post reply on HN