question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What is the recommended way to deal with no-param-reassign in reducers?

See original GitHub issue

This problem has been discussed before, but I haven’t seen a general agreement on the best way to solve it.

Although this reduce() operation (as a whole) is pure, the callback within it is not, so it breaks no-param-reassign:

const arr = [['fred', 24], ['bob', 32]];

const obj = arr.reduce((accumulator, item) => {
  accumulator[item[0]] = item[1]; // ERROR! no-param-reassign
  return accumulator;
}, {});

// obj === { fred: 24, bob: 32 }

Option 1 – suppress the linter:

const obj = arr.reduce((accumulator, item) => {
  accumulator[item[0]] = item[1]; // eslint-disable-line no-param-reassign
  return accumulator;
}, {});

Option 2 – make a new object each time:

const obj = arr.reduce((accumulator, item) => (
  { ...accumulator, [item[0]]: item[1] }
), {});

Neither option feels great.

  1. Suppressing the linter should be restricted to unusual edge cases imo, not everyday idiomatic JavaScript like .reduce().
  2. Creating a new object on every iteration creates extra garbage. A programmer can take one look at the operation and see that it’s pure. I realise that technically it’s possible that arr.reduce might refer to something other than Array.prototype.reduce, so the linter can’t make a special exception here. But it feels wrong to write slower code just because of a limitation in the linter.

What’s the best thing to do?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:10

github_iconTop GitHub Comments

22reactions
ljharbcommented, Mar 21, 2017

“creates extra garbage” is a memory concern, and in a memory-managed language, that should be none of the programmer’s. Create a new object on every iteration, which makes the reducer pure (as well as the overall reduce, which is pure in both cases).

However, ignorePropertyModificationsFor is already enabled in the next release of eslint-config-airbnb-base, so if your accumulator is named one of the things it’s enabled for, it will be allowed.

0reactions
ljharbcommented, Mar 11, 2019

You’re not describing something a JS developer should be thinking about. GC is negligible most of the time - you don’t need to “code really precisely” (something most competent developers do on the web), you just need to write clean code.

If you close a tab and your RAM doesn’t go down, then clearly that’s a bug in the browser - no amount of attempted memory management in JS can fix that.

You clearly prefer to write your JS code as if it was C, tightly controlling memory usage. Go for it! This guide does not endorse or encourage that mindset, nor will it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to avoid no-param-reassign when setting a property on a ...
As this article explains, this rule is meant to avoid mutating the arguments object. If you assign to a parameter and then try...
Read more >
no-param-reassign - ESLint - Pluggable JavaScript Linter
Rule Details​​ This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.
Read more >
What the Heck is the deal with no-param-reassign
The first thought when people see the no-param-reassign error is to copy the parameter value into a locally scoped variable. view plaincopy to ......
Read more >
Redux Toolkit | Codetain - end-to-end software development
Long story short - in this file, first define the initial state of the reducer with the correct type. Then use the createSlice...
Read more >
Writing Reducers with Immer | Redux Toolkit - JS.ORG
Many ESLint configs include the https://eslint.org/docs/rules/no-param-reassign rule, which may also warn about mutations to nested fields. That ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found