Can someone comment on this from the linked tweet? I haven’t been a js dev in a long time. Is functional style dead?
Can someone comment on this from the linked tweet? I haven’t been a js dev in a long time. Is functional style dead?
No, the javascript ecosystem is just full of people who have taken some pattern that occurs frequently in functional programming, such as currying, to itself be functional programming, and thus write wrappers and overly complicated libraries to implement that feature and call it a functional wrapper or the functional version of the API, despite those features having very little to do with functional programming.
Idiomatic javascript and typescript are full of functional programming, far more so than other major programming languages. Avoiding functional constructs would, in fact, make your code unidiomatic. So the functional style is about as far from dead as it could possibly be.
Additionally, not only has the ecosystem been adopting a more functional style as time passes (especially due to features that have been added such as destructuring and spreading, which heavily encourage a more functional style), some of the core APIs in the standard library that used to make (pure) functional programming a bit more awkward by mutating their arguments now have pure equivalents [0][1] as well.
[0]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
The big deal about FP is that you can see more easily what the damn function will do when you call it. A weird and long-winded notation for multiple inputs contributes jack.
Still love map. But reduce can feel very "code golf" real quick.
"Easy to read" means "easy for others". Current efficiency is usually borrowed on future one.
Fold/reduce is fundamental. It’s weird to me that its use would be discouraged or outlawed somewhere.
The language I use has various flavours of `for`. It also has various flavours of map and fold. It also has recursion.
They each do different things. Those differences are meaningful and useful. Why would you limit yourself to just using `for`?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
The main value of “map” is to denote a 1:1 data transformation. Plus this gets very fraught with many langages mapping lazily, and some not guaranteeing the order of operation.
const idNameMap = itemList.reduce(toIdNameMap)
It should be intuitive that itemList is a list of objects familiar to the context of the code, and the result will be like: [{"id1":"Billy"}, etc.…But how?!
I can't see how anyone could build an intuition for this opaque line and the example of its apparent result. It's confusing enough that `idNameMap` actually refers to a list (or array), and not a map (or object).
In languages like JS that have both styles, the place you probably want to use for loops over reducers is for more complex operations where mutation can simplify the code. For example if you have 10,000 elements in an array that you want to aggregate into 1,000 properties on an object (with a non trivial relationship between array elements and object properties), it's probably going to look nicer in a for loop than it is in a reducer with a bunch of spread and ternary operators.
IMO .reduce calls should probably be one line. If not then it's time to either write a for loop or compose more smaller functions.
Avoiding them entirely is just FUD though. To compare summing an array in JS:
let sum = 0;
for (const n of numbers) {
sum += n;
}
const sum = numbers.reduce((acc, n) => acc + n, 0);
The latter is much nicer to read if you're used to both styles. What I particularly like about it is it clearly signals that 'sum' is a variable that we will be using further down. The for loop style leaves ambiguity as to whether 'sum' is going to be used later or if it's just some context for something that's happening inside the loop.Actually, to throw a hot take in the mix here, I think people should be more accepting of multiple statements across single lines in C-style languages.
let sum = 0; for (const n of numbers) sum += n;
That would be a perfectly fine way to signal to the reader that "this is a specific line of code to produce a sum value" (much the same as the reduce example), but people seem to have a dogmatic aversion to leaving braces off if/for statements or putting multiple statements on a single line.The first version is something most programmers could read and understand right away.
I also dont understand why the second version signals use of the variable any better than the classic version, esp if you declare the variable right ahead of it, but that is the classic "C" style.
How ever in a real system I would hope people use better names than sum. which would give it some comprehension of what the variable does / exists for.
What is the obsession with one liners? Perhaps if paper was quite expensive and it was printed out paper on a regular basis you could save a few sheets, I dont think one long line is easier to read than several short lines.
I suppose if everyone involved with the project prefers or is used to the second version it is fine.
Because you have no idea if the variable is used further down in the scope. Compare with something like parsing a csv file with support for quotes:
let rows = [];
let quoteMode = false;
for (char c of csvFile) {
...
}
rows is the end result, quoteMode is state only relevant to the process happening within the for loop. There's nothing past variable names to distinguish their intended use. Whereas: const rows = parseCsv(csvFile);
makes it totally clear.The reason something like reduce can make for nicer code is because it lets you do simple operations (probably not parsing a csv lol) inline in a way that makes the code just as clear as a function call would be, without adding a bunch of extra layers of abstraction (meaning more scrolling and flicking between tabs).
Really it's not about the reduce function at all, it's about the "const foo =". Once you see that, you can completely disregard the rest of the line if you've already read it and/or are confident you know what it's doing. It acts as a refresher in a way that a for loop doesn't. After you get past the inital shock of seeing a scary one liner (which get less and less scary as you see them more), it makes the next 20 times you read it much more pleasant.
Although this is indeed implemented with reduce. But is it really too much code? List(1,2,3).reduce(_ + _)