in reply to Which is your favourite cmt-deserving Perl feature?

I would comment the following idiom:
# If not already done, initialize $something using whatever() $something ||= whatever();

Remember: There's always one more bug.

Replies are listed 'Best First'.
Re^2: Which is your favourite cmt-deserving Perl feature?
by Limbic~Region (Chancellor) on Jul 21, 2005 at 19:15 UTC
    jdhedden,
    This idiom can bite viciously. This will assign the value of whatever() to $something if it has a false value. A false value doesn't necessarily mean undefined.
    • 0
    • ''
    • '0'
    • undef
    Future Perl's will have a //= operator which means "defined or" but without a patch, if a defined but false value is valid - you need to write this more like $something = whatever() if ! defined $something

    This is likely a good case to comment since when debugging having a reminder that defined false values may be overwritten.

    Cheers - L~R

    See What is truth? (Curiosity corner) for more details
      This is likely a good case to comment since when debugging having a reminder that defined false values may be overwritten.
      You're perfectly right. But, if one is aware of what he is doing ||= does its job most of the time. Of course we all will welcome //= to cover the remaining cases.

      Occasionally I use ||= on purpose to overwrite a false value (with another "standardized" false value), e.g.:

      $u ||= ''; # or $v ||= 0;
      (Most often this is not necessary either, and -as usual- there are other ways to do it...)
Re^2: Which is your favourite cmt-deserving Perl feature?
by jplindstrom (Monsignor) on Jul 21, 2005 at 20:11 UTC
    If used (I do it often), that's something that would be spread widely in the application.

    So if it's used, it would be considered a known idiom and shouldn't be commented everywhere in the code.

    If in a team environment, this would be considered team lore and any newbie could easily just ask someone the first time it's encountered.

    /J

Re^2: Which is your favourite cmt-deserving Perl feature?
by adrianh (Chancellor) on Jul 22, 2005 at 11:47 UTC
    # If not already done, initialize $something using whatever() $something ||= whatever();

    I have to admit that this is the sort of comment I hate :-) It doesn't tell me anything that the code doesn't.