3 ms·
> Predeclared variables are just as prone to typos, which will also break your code -- and just as prone to broken scoping. By accidentally shadowing an externa
by joesb 15y ago
> Predeclared variables are just as prone to typos, which will also break your code -- and just as prone to broken scoping. By accidentally shadowing an external variable you make it unreachable for the remainder of the current function, and all nested functions within.
You declared a variable once and use it n times. If you accidentally shadow a variable, via typo, then you only have one place to check for the typo. The compiler/interpreter will complain if you don't happen to also consistently made that typo everywhere you use the variable.
In language with implicit declaration, everywhere you use the variable is as likely to have typo that the compiler/interpreter cannot check for you.
1 place to check vs. n places, is a no-brainer which one is more error prone.
> By accidentally shadowing an external variable ...
In language with explicit declaration you have to accidentally declare same name AND use it with that accidental name. If both doesn't happen, the interpreter will complain.
In language with implicit declaration, one accidental typo is all it takes to accidentally shadow a variable.
You have just made a case against implicit declaration.
> There are no situations where shadowing a variable is desirable. [...] if you accept that shadowing is undesirable, and choosing a more descriptive name for the inner variable is preferable
So, to name a local variable "list", you have to look up on every outer variables involved to make sure nowhere else also use the name "list".
And let's give up on naming an outer variable name "list" because, who knows where some inner function might decide to use such name for local variable.
> choosing a "more descriptive name" for the inner variable
More like "more redundant name". Implementing "date_diff" function? Forget naming different of it "delta", let's name it "date_diff_delta", or else it might conflict with some outer variable name.
Congratulation, you have successfully remove concept of namespace from your language.
> if you accept that shadowing is undesirable
Then you should consider other job than programming.