in reply to Question Marks in Subroutine Names

? in subroutine names seem like a bad idea, it adds amibguity. This is valid syntax:
print is_contact_male?'zut':pickupLine;

It seems like your style of coding might be better done in OO such as $obj->sex.

--
perl -pew "s/\b;([mnst])/'$1/g"

Replies are listed 'Best First'.
Re: Re: Question Marks in Subroutine Names
by pdcawley (Hermit) on May 08, 2002 at 09:08 UTC
    It's not legal now, but it may well become so in perl 6 since ?: is becoming ??:: in order to free up the colon (Larry hasn't told us what's going to happen to the question mark, but I wouldn't have a problem with making it a valid character in an identifier. I'd love to be able to write
    given $employee { when male? {...} when female? {...} default { "Ooh... neuter"; ... } }
    I tend to think that 'question form' methods/functions sit best in a functional or objective programming style; they seem to jar slightly when you're writing procedural stuff. But that could just be me.

    Probably what will really happen is that Larry will show us some utterly clear and really useful trick that can be done with '?' as an operator and we'll gleefully use that and forget about using ?s in our identifiers.

      Eep. This style of subroutine naming would do my head in, I see the 'when male?' and the natural-language part of my brain takes that as a stand-alone sentance and asks something like "I don't know, when are they male?".

      It's the different between 'When raining, put on a coat' and 'When raining? Put on a coat'. The first sounds natural to me, and the second like a bad translation.

      Maybe my brain's wired wrongly though.

Re: Re: Question Marks in Subroutine Names
by kappa (Chaplain) on May 08, 2002 at 14:18 UTC
    if ($person->sex) { print "True sex\n"; } else { print "False sex\n"; }
    :)))
    And seriously, everybody has probably seen way too many boolean sex variables to understand that each if sex then construct confuses reader a lot.
Re: Re: Question Marks in Subroutine Names
by dug (Chaplain) on May 08, 2002 at 03:20 UTC
    Hmm.
    This certainly doesn't effect the way that I think about the question that I posed. A subroutine declaration like sub sex { # do stuff }; won't exist in my style of coding. And you're telling me that I'm adding ambiguity? :-)
      hein? The sex method would return a value indiciating whether the object/individual was male or female.

      --
      perl -pew "s/\b;([mnst])/'$1/g"

        Sure, it could.
        If you go back and read the original post, this is about readability.

        ## # sub sex() # returns qw( yes no maybe somtimes male female # only_on_tuesdays when_i_get_older ) or a miryad of other # possibilities.

        That said, $obj->sex? could get interesting :)

        As a true or false question goes, sex will probably always return true, so no question mark needed, if that's what you were trying to say.