I like spinach.
Plowing through the copy of Damian Conway's "Perl Best Practices" I got at OSCON, I've been pleased to find that nearly all of the recommendations are both healthy and palatable. In places where my code doesn't adhere, I can generally see the advantages of what he advocates, and I'll be willing to adapt for both my own good and the sake of convergence. Mmmm, spinach!
There is one section, though, that I'm having a lot of trouble getting down: the recommended naming conventions for variables, specifically for references and hashes. Damian advocates tacking '_ref' onto the end of any scalar that contains a reference...
... The primary rationale for this is that if you use $vegetables_ref->{spinach} instead of $vegetables->{spinach}, there's no chance that if you accidentally leave off the dereferencing arrow you will access a %vegetables array that happens to exist in the same scope: $vegetables{spinach}
However, it seems to be accepted wisdom that having scalars, arrays, hashes, etc in the same scope with the same identifier is a really bad idea. I don't append _ref to my reference variables and yet I cannot recall ever having encountered the problem he describes, because I never count on the sigil alone to differentiate program elements. Of course I leave off the dereferencing arrow all the time, but that's a fatal compile-time error that is trivial to debug.
The thing I find most objectionable about this recommendation is its interaction with another, much more important guideline: use descriptive variable names, like $cancelled_transaction_number instead of $i (to quote Damian's own example). Such names can get pretty long, and appending _ref to them adds noise, possibly pushing a line over 78 characters and necessitating a line break. The code becomes less readable, and less of it fits on screen -- all for the sake of solving a problem that's only a problem if you're doing something asinine to begin with.
Damian also recommends naming hashes in the singular, the idea being that individual accesses seem more natural: $vegetable{spinach}. It's weird to me that a collection of key-value pairs representing, say, %foo_file_defaults ought to be named %foo_file_default under his guidelines, but hey, I'm a team player and I could adapt if someone said "do it this way". Still, the guideline is internally inconsistent: since he recommends that arrays be named in the plural, individual accesses for array elements still use the plural: $stories->[0]
A long time ago I came across the P5EE style guidelines, which spell out variable-naming conventions which avoid all these problems. Not sure where that project's at these days, but the guidelines have served me well. Here's the relevant section, and a link:
Arrays and hashes should be plural nouns, whether as regular arrays and hashes or array and hash references. Do not name references with "ref" or the data type in the name. @stories = (1, 2, 3); # right $comment_ref = [4, 5, 6]; # wrong $comments = [4, 5, 6]; # right $comment = $comments->[0]; # right
http://www.officevision.com/pub/p5ee/software/htdocs/P5EEx/Blue/perlstyle.html#names
Update: A missing dereference operator is only a compile-time error under use strict -- otherwise it's a runtime error. Thanktalus, Tanktalus.
Marvin Humphrey
Rectangular Research
http://www.rectangular.com/
In reply to Perl Best Practices for naming variables by creamygoodness
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |