Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad practice to shadow variables of a callback? [closed]

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?

like image 264
hexacyanide Avatar asked Mar 10 '13 18:03

hexacyanide


1 Answers

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.

like image 180
DrC Avatar answered Oct 03 '22 01:10

DrC