PBP: 025 Reference Variables

The PBP suggests suffixing reference variables with _ref.  The book notes in a footnote that this is the only type of “Hungarian notation” suggested in the book.  That’s good, because Hungarian notation is a bad idea.

I don’t agree with this.  Firstly, it makes a lot of variable names needlessly longer.

Secondly, if you have strict and warnings on, it will complain.  There’s no reason to clutter your code because you might possibly forget something sometime.

Thirdly, with current Perls, several of the things you’ll do no longer need the dereference – keys, for instance.

If, as I suggested in another comment, you always use references for hashes and arrays, you don’t have the “what kind was this” problem, either.  It’s always a reference.

I don’t do this, and will resist the suggestion to doing so.  It is a waste of the programmer’s time and energy, and distracts from what your code is trying to do to make up for a perceived weakness in the interpreter.


3 Responses to “PBP: 025 Reference Variables”

  1. I’m largely in agreement here. I would suggest that it either goes too far, or not far enough. *_aref and *_href to distinguish arrays and hashes can be useful in some cases, but myself I try to use english plurals to indicate a reference, and singular nouns for actual scalars.

    foreach my $case ( @{ $cases } ) { … }

  2. Personally I prefer to just use $ for all variables, including arrays and hashes, and not see an instance of @ or % anywhere except as necessary dereferencing syntax e.g. to use ‘keys’ on a hash. Then, everything is either a reference or a scalar, and it is treated consistently everywhere including when passed to or from routines or whether used in an element or straight off. I know Larry likes the multiple sigils and I can see his point about grammatical elements, but I consider the use of multiple sigils based on type to be a misfeature personally.

  3. Aaron Priven says:

    “keys $ref” was given an experimental warning starting with 5.20.

Leave a Reply