in reply to Re^3: OO Perl: Nested classes question
in thread OO Perl: Nested classes question

I think we're now on the same page on most of this, but there is one point we're not.

Also what is the reason that you're afraid of using unless?
I strongly dislike the action appearing before the condition. I find it to be a lot more prone to causing confusion (for me) and decreasing readability. (And yes I know there's plenty of people who argue it does exactly the opposite.)
This isn't what I was talking about. I asking about writing
if (! defined $self->{_condition}{$condition}) { $self->{_condition}{$condition} = Condition->new(); }
versus
unless (defined $self->{_condition}{$condition}) { $self->{_condition}{$condition} = Condition->new(); }
There are arguments both ways, I was wondering what your arguments were. (Basically my belief is that if you're going to offer advice, you should be able to back it up.)

Replies are listed 'Best First'.
Re^5: OO Perl: Nested classes question
by Crackers2 (Parson) on Sep 16, 2006 at 02:58 UTC

    Note that I never specifically gave advice about if versus unless (not intentionally anyway)

    Silly as it may seem, I've always assumed unless could only be used as a post-condition.

    That being said... even now that you've shown me, I'll probably still keep on using just if, for a few reasons:

    • if is more universal. If I start using unless in Perl, it's one more thing to remember not to use in bash or C. Now of course you can extend that argument and say "well then why do you use hashes, they aren't there in C either", but the difference there is that hashes are one of the core strengths of Perl, where unless is really no more than syntactic sugar.
    • The strong visual cue that ! provides. Maybe it's because I don't use unless much, but I always have to think twice when I see it to remind myself that the following condition is essentialy negated
    • English style. This may be a regional or native language thing (my native language is Dutch), but I don't think I've ever started an English sentence with "unless"; whenever I use it it's in the "<something> will happen unless <condition>" kind of format.

      For me ! is not a useful visual tool at all. The rest sounds very reasonable.

      Allow me to add to the list the observation that humans don't do a good job of doing De Morgan's Laws in practice. When you're writing it, it is easy to make the statement mean what you want it to mean. But when you are tracing through code later, it is surprisingly difficult to figure out whether you enter a condition that reads unless (A or B) {...}

      And yeah, when I was first told that I thought it was nonsense. It was not until I was having trouble debugging something at 11 PM that I realized it was true. Therefore I use unless very, very sparingly.