Hi; Re: Best Practices: define variables at the top or just prior to when they are referenced?
We don't have a formal Coding Standard in the CM/Build team I'm a part of. We have Solaris, Linux and Windoze platforms, but (gratefully!) most of the scripting is with the UNIX/Linux side rather than the Windoze side. We have lots of Tcl/Expect and some Shell (bash/Bourne) that we slowly are replacing (not entirely) with Perl (circa 5.8). I try to code my scripts for optimal simplicity, readability and maintainability, not necessarily idiomatic Perl and most likely not performance-oriented. Arguments in code reviews come up frequently regarding the "correct" placement of variables; either at the top of the script or just prior to the point of use. I noticed in a recent thread that John W. Krahn stated: "It is usually better to define variables where they are first used instead of all at the start of code". The argument I get in code reviews is that this position leads to code that is not as easy to maintain as when variables are defined near the top of the script. I don't have a copy of Damian Conway's Best Practices handy and I don't recall if this topic is discussed there. Perhaps this is not even a Perl-specific question :-) I'd like to hear arguments for and against placement of variables at the top and placement of variables just prior to when they are first used. Thanks, Ken Wolcott