Review main Abandoned

88264[TASK] Improve code-style for JS `Array.prototype.reduce`

Ayke Halder CR 0 V -1 Easy +13 −12 updated 11/12/26, 21:02

Commit message

For `Array.prototype.reduce` the intended use of the
'accumulator parameter' vs. 'variable outside function scope'
must be clear.

Not easily readable:

```
const items = [1, 2, 3];
const newItems = items.reduce(
  (items /* <-- this is not `items` from outside scope */, oneItem) => {
    items.push(oneItem + 'test');
    return items;
  },
  [] /* <-- this is the init value for `items` inside `reduce` */
);

// prints [ 1, 2, 3 ] <-- This has never changed!
console.log(items);

// prints [ '1reduced', '2reduced', '3reduced' ]
console.log(newItems);
```

Better do not assign an already used name as 'accumulator'-param:

```
const newItems = items.reduce(
  (itemsAccumulator /* <-- unique name */, oneItem) => {
    itemsAccumulator.push(oneItem + 'test');
    return itemsAccumulator;
  },
  [] /* <-- this is init value for `itemsAccumulator` inside `reduce` */
);
```

Review

  • Code review CR 0
  • Verified V -1
    • core-ci -1 CI

Issues 1

Files 4

Review log 79

  1. Ayke Halder uploaded patch set 1 02/18/25, 14:19

  2. core-ci voted on patch set 1 V +1 02/18/25, 14:26 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/74319

  3. Ayke Halder commented on patch set 1 02/18/25, 14:41

    tree.ts:513

    @benjamin.kott@outlook.com What is the intention here: * Search inside accumulated nodes only? (Scope `Array.prototype.reduce` method: that's how the code actually works right now.) * Or search inside all nodes? (Scope `Tree.enhanceNodes` method)
  4. Benjamin Franzke commented on patch set 1 CR -1 02/18/25, 16:38

    I do not agree. Every developer should know that local variable scopes overwrite outside scope. The term accumulator does not help, it's like using type-suffixes/prefixes in the old days… like `fooArray`, or `aFoo`. We can call it `enhancedNodes`, if that helps.

    tree.ts:508

    I'm really not a fan of premature optimization. Why this bold statement? a) we're dealing with small chunks of data here (it's not the entire tree, only the subtree that has been loaded from the server, that is being enhanced here) b) `[...nodes, node]` allows the browser to apply optimizations, since it could GC `nodes`. This is something that is actively being worked on, see https://v8.dev/blog/spread-elements#conclusion c) Combining functional programming with mutable structures makes this code even harder to understand, it's better to avoid mutations in transformation pipelines.

    tree.ts:513 Reply

    Intention was to search in the already enhanced nodes (current code is correct), that means in the accumulator – that's the reason why `.reduce()` instead of `.map()` was used here. Background: nodes processing requires their parents to be processed in order to generate the recursive `__treeIdentifier`
  5. Ayke Halder commented on patch set 1 02/19/25, 10:56

    Reply Unresolved

    Unfortunately `enhancedNodes` is also already in use. So neither * const enhancedNodes = **nodes**.reduce((**nodes**, node) => {…})` nor * const **enhancedNodes** = nodes.reduce((**enhancedNodes**, node) => {…}) looks like a nice solution here. To make e.g. ESlint "@typescript-eslint/no-shadow" work, the name must be unique.
  6. Ayke Halder commented on patch set 1 02/19/25, 11:07

    tree.ts:508 Reply Unresolved

    Just to correctly understand the issue with mutable structs. Is the following example also a mutation, caused by the outside scope variable? Example using `map` instead of `reduce`: ``` const lastParentNodeByDepth: TreeNodeInterface[] = []; const enhancedNodes = nodes.map((node: TreeNodeInterface) => { node = Object.assign({}, this.settings.defaultProperties, node); node.__parents = []; const parentNode = node.depth > 0 ? lastParentNodeByDepth[node.depth - 1] : null; if (parentNode) { node.__parents = [ ...parentNode.__parents, parentNode.identifier ]; } lastParentNodeByDepth[node.depth] = node; return node; } ```

    tree.ts:513 Reply

    Acknowledged.
  7. Benni Mack updated the commit message in patch set 2 CR -1 V +1 04/22/26, 06:30

  8. core-ci voted on patch set 2 V -1 04/22/26, 07:00 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/100711

  9. Georg Ringer abandoned the change 11/12/26, 21:02

    abandoning the issue as no progress + see forge for more