Changing the value of your parameter is a code smell
See original GitHub issueChallenge 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:
- Created 7 years ago
- Comments:12 (12 by maintainers)
Top 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 >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
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.
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.
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.