>>>>> "KW" == Kenneth Wolcott <kennethwolc...@gmail.com> writes:
KW> Arguments in code reviews come up frequently regarding the KW> "correct" placement of variables; either at the top of the script KW> or just prior to the point of use. KW> I noticed in a recent thread that John W. Krahn stated: "It is usually KW> better to define variables where they are first used instead of all at the KW> start of code". KW> The argument I get in code reviews is that this position leads to code KW> that is not as easy to maintain as when variables are defined near the top KW> of the script. you need a better advocate at these code reviews. if you have people claiming declarations at the top are better, they probably aren't good perl (or any lang) coders. there are several wins to declaring as you need to. first, it is usually shorter. most times you can declare and assign a variable in one line vs two with predeclaring. shorter code is almost always better code as you have less to read to understand it (there are other benefits to shorter code as well). next, declaring as you need it means the variables will usually be in tighter scopes (blocks, subs) which means their names will only be visible in those scopes which is good. it also means they will be freed when those scopes exit but usually predeclared vars are at the top of a script and will be around until the script exits. so it is more efficient to keep things declared as needed. it is also easier to debug code this way. you just have to scan up the scopes to find the variable and it will usually be pretty close, possibly on the same screen. with predeclare you end up with a long list of vars and finding it can be a pain. sure, searching helps but it will almost always be on a different screen. KW> I don't have a copy of Damian Conway's Best Practices handy and KW> I don't recall if this topic is discussed there. it doesn't matter what pbp says on this. KW> Perhaps this is not even a Perl-specific question :-) it is most definitely not a perl specific question. this is an issue in any lang which requires variable declares and has scoping. KW> I'd like to hear arguments for and against placement of KW> they are first used. the arguments supporting predeclaring are vacuous, illogical, unsupportable, and stupid! :) does that help? uri -- Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com -- ----- Perl Code Review , Architecture, Development, Training, Support ------ --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com --------- -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/