in reply to Re: Syntax error - "my" within foreach loop
in thread Syntax error - "my" within foreach loop

I like being able to declare variables anywhere, especially if they are unimportant accumulators or iterators. However I will often predeclare varibles early in the code or block where they are needed so that I can group them and describe them in a comment block. YMMV.
  • Comment on Re^2: Syntax error - "my" within foreach loop

Replies are listed 'Best First'.
Re^3: Syntax error - "my" within foreach loop
by GrandFather (Saint) on Apr 14, 2008 at 21:31 UTC

    A comment on a variable indicates in one place what the variable is for. An appropriately named variable tells the reader every place it's used what it is for.

    A comment that explains a variables role in following code should be placed with the code, not where a collection of variable declarations may huddle together at the top of a block or subroutine.

    A key element of good commenting is to place the comment as close to the real context for the comment as possible and to only to explain non-obvious elements of the code (there may be some disagreement over what is 'non-obvious').

    There is a lot of art in naming variables and writing good comments. There is much less art required in figuring out where to declare variables - code structure and the "late as possible" mantra dictates that.

    Something related to think about: a useful guide is that the length of an identifier should reflect the scope over which it is used - short identifiers for small scopes and longer identifiers for larger scopes. $tl is ok for a variable in a small scope, but $topLeft is better in a bigger scope, and $mainWndTopLeft may be appropriate in a large scope.


    Perl is environmentally friendly - it saves trees