in many languages where shadowing is allowed it considered to be a bad practice, but I see that in The Rust Programming Language book it's being used in the first chapters and nothing said about whether it's bad or not.
why would it be bad ?
It makes it more difficult to read the code, because you need to check which variable exactly from those that you've declared you are using. E.g. you change a variable, you think that you change it in outer scope, but in fact you are changing another variable in inner scope with the same name. For example C++ has "default" warnings about using shadowing (and also has a rule for that in language core guidelines); go vet allows to enable such warning for golang projects, some IDEs give warnings for other languages
shadowing is fine in some cases, e.g. when you define a variable and then shadow it immediately with a variable of another type. but i agree that in the case you described it is indeed error-prone
Обсуждают сегодня