As we all know, in some languages (the most known example is javascript) variables are global scoped by default. That means that if one wants to declare local variable, he should write var, local, my
or whatever.
I'd never thought about the implementation costs of this, but it turns out that it could be not just a matter of traditions. For example, check this link. My question is - is local scope-by-default architecture beforehand more pricey than global-scope-by-default. Just kinda of, don't know, selection sort beforehand requires less swaps than bubblesort, in that way "beforehand".
Besides, I would appreciate if somebody will edit this question to add appropriate tags. I just don't know which one fits better he开发者_如何转开发re.
A summary of some points which is better (local-by-default or global-by-default) for the Lua language can be found on this wiki page. Maybe neither-by-default is the best answer, but we programmers want to save some typing ;)
Some quotes from the wiki page:
- "Local by default is wrong. Maybe global by default is also wrong, [but] the solution is not local by default." (Roberto Ierusalimschy, architect of Lua)
- "[The current local variable scoping rule] is the single biggest design flaw in Ruby." (Yukihiro Matsumoto, Ruby designer)
- "I dislike the implicit declaration of lexicals because it tends to defeat the primary use of them, namely, catching typos...Declarations should look like declarations...I believe that declarations with my are properly Huffman encoded." (Larry Wall, Perl Designer)
At compile time, the costs of local-by-default and global-by-default are the same. You still have to completely traverse the list of all active local variables when you find a name that hasn't been seen yet. At run time, local variables are usually faster to access.
精彩评论