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

In reply to Re^3: Syntax error - "my" within foreach loop by GrandFather
in thread Syntax error - "my" within foreach loop by Klammer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.