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.

Thoughts on "Never mutate parameters"

See original GitHub issue

1- This rule’s “Do not reassign parameters” part should be split into another rule, because reassigning a parameter isn’t actually mutation.

Plus it sounds like no-param-reassign rule also covers mutation, but it doesn’t. It would be nice to easily see what gets covered by ESLint and what doesn’t.


2- Why shouldn’t we reassign parameters?

Overwriting parameters can lead to unexpected behavior, especially when accessing the arguments object.

Accessing arguments is already discouraged. Should we really care about breaking something we aren’t supposed to use? What are other unexpected behaviors can this cause?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:18 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
ljharbcommented, Dec 29, 2015

Reassigning parameters deoptimizes in many engines, specifically v8 - it’s a horrible idea to do it. Variables are free, and creating new ones rather than reusing old ones makes code much clearer.

(In addition, nothing in JS is passed by reference, everything is passed by value, where objects are a form of “reference value” - this is a good read on the subject)

A PR would be welcome if there’s verbage that could be cleared up.

8reactions
AlicanCcommented, Dec 29, 2015

Reassigning parameters doesn’t do that. Mutating parameters do that.

const myObj = {
  adele: 'hello',
};

function myFunc(myParam) {
  myParam = {};

  myParam.adele = 'rolling in the deep';
}

myFunc(myObj);

console.log(myObj); // { adele: 'hello' }

Mutating parameters is wrong, I have no objection to that rule.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why you shouldn't mutate parameters - SeanMcP.com
Mutating parameters. When you pass a variable as an argument, you are handing a reference to the function – not a value.
Read more >
Washing your code: avoid mutation - Artem Sapegin
Avoid mutating operations · Beware of the mutating array methods · Avoid mutation of function arguments · Make mutations explicit if you have...
Read more >
Good practice to avoid mutating method parameters in Ruby?
Ruby's "good practices" generally boil down to not mutating data you don't own. The problem is that ownership can be somewhat hazy, ...
Read more >
When is it OK to mutate arguments to functions, in terms of ...
For procedural style programming, the Clean Code book advocates for never mutating arguments passed to functions.
Read more >
Mutation isn't always bad in JavaScript - DEV Community ‍ ‍
Mutation is an excellent tool if localized (e.g., the mutated object never escapes a function). Top comments (21) ...
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