Take the asynchronous Node function fs.stat()
for an example. If I need to use fs.stat()
on a file, then do it again later, the result is shadowed.
fs.stat(file, function(err, stats) {
fs.stat(file, function(err, stats) {
});
});
The err
variable, as well as stats
variable is shadowed - does this even matter if I won't be using the first callback inside the second? Is it better practice to rename the second callback variables?
Does overwriting these variables, once, or multiple times have any performance impact?
Matter of opinion, but I'd say yes - bad practice. In any case, there are two problems here
It can be confusing for the next person (which could be you) who needs to work on this code.
At some future time, you may need to access the outer values to provide some new functionalities (for example, want to make sure the two file objects have the same type?). Making that change will require an annoying and error-prone edit of the entire code block.
Use different names now to prevent problems later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With