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.

Changing the value of your parameter is a code smell

See original GitHub issue

Challenge Factorialize a Number has an issue. User Agent is: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36. Please describe how to reproduce this issue, and include links to screenshots if possible.

I don’t believe we should encourage beginners to change the value of a parameter of a method. In the proposed code, the function factorialize returns “num”. It would be better form to create a var “result” to return it at the end - teaching good coding practices.

My code:


function factorialize(num) {
  var result = 1;
  for(var i=1;i<=num;i++) {
    result *= i;
  }
  return result;
}

factorialize(5);

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
raisedadeadcommented, Jul 5, 2016

I disagree that modifying input param is a bad practice, unless some one can point me to a concrete reference.

For instance, someone can rightly argue that each variable you declare is adding a new reference to the same object on the stack.

var actualVariable = 10;
var myVariable = actualVariable;
var yourVariable = myVariable;

All the above point to the location where 10 is stored, just with different references (variable names);

This is rather, waste of resources (not in this challenge), but when you do any memory intensive computing (visual renders, etc.)

Edit: Missed a point, that you could copy the data within a variable only when necessary, only when it is essential to work on a copy whilst preserving the original, else it is waste of assigning more variables than required.

IMHO modifying input param should be completely acceptable. JavaScript lets us do that and leveraging it should be the best practice not the other way round.

2reactions
Manish-Giricommented, Jul 5, 2016

While I agree with @BerkeleyTrue’s point that the aim of the challenge is to help the camper to develop an algorithm and not necessarily talk about best practices in general, I don’t think it’s a good idea to modify method parameters. Here’s an interesting article that I found on this context.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does Modifying an Incoming Parameter Count as a Code ...
Does Modifying an Incoming Parameter Count as a Code Smell? · It can change the variables' original value, · It reduces the code's...
Read more >
Are "in" parameter modifiers considered a code smell?
No, using the keyword in to make an argument passed by reference to a method read only is not a code smell. Modifying...
Read more >
Code Smell: Output Parameters - Medium
Output parameters are the kind of code smell that is sometimes difficult to see, but when they crop up they can cause a...
Read more >
Code Smell — Too Many Parameters - DEV Community ‍ ‍
Code smells are often related to each other. Too Many Parameters can often be seen next to "Long Method" or "Large Class" and...
Read more >
Java static code analysis: Parameters should be passed in the ...
Java static code analysis. Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your JAVA code.
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