in reply to Re: dereferencing question
in thread dereferencing question

communicating clearly with future developers

And also search tools, code prettyfiers and the occasional perl script to mass-update existing code bases for more modern Perl idioms¹. The more unique (and standardized across the whole codebase) a construct is, the easier it is to find, even with non-regex searches.

It's basically the same reason for naming a variable @customers instead of @c: Easier to understand, and much more important, easier to find.


¹ Converting to sub signatures, which (mostly) worked and saved me many days of manual editing.

PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
Also check out my sisters artwork and my weekly webcomics

Replies are listed 'Best First'.
Re^3: dereferencing question
by talexb (Chancellor) on Jan 22, 2025 at 17:07 UTC

    The only time I use single character variable names like $k is when the scope is going to be something really small like five to ten lines. For example,

    foreach my $k ( keys %DBhandles ) { $DBhandles{ $k }{ sth }->finish; $DBhandles{ $k }{ dbh }->disconnect; }
    And yes, I could minimize that to just using $_ instead of $k, but I try to never use that special variable, because it requires extra effort for the reader.

    Alex / talexb / Toronto

    For a long time, I had a link in my .sig going to Groklaw. I heard that as of December 2024, this link is dead. Still, thanks to PJ for all your work, we owe you so much. RIP Groklaw -- 2003 to 2013.

      Yeah, $_ (especially the implicit version) is something to generally avoid. It's not only harder to read and understand, but it's also easy to mess up a program when you have to re-shuffle some logic.

      As for single-character iterators, i mostly use them on C-style for loops. $i, $x, $y are my favourites, with $i for general stuff. $x and $y are always sensible choices when iterating over the pixels of an image, plus there aren't that many other variable names (english words) that start with an "x" or "y" that also make sense in a cash register application.

      PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
      Also check out my sisters artwork and my weekly webcomics