Thoughts on "Never mutate parameters"
See original GitHub issue1- 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:
- Created 8 years ago
- Reactions:4
- Comments:18 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
Reassigning parameters doesn’t do that. Mutating parameters do that.
Mutating parameters is wrong, I have no objection to that rule.