Clever

Ovid on 2009-02-11T17:25:32

Well, that was clever.

my $_combination_info = {};
sub combination_info {
    my $self = shift;
    @_ and $_combination_info->{ $self->csv_file_name } = $_[0];
    return $_combination_info->{ $self->csv_file_name };
}

Clever is bad. I mean TIMTOWTDI and all that, but as I'm reading through code, I hate seeing a "clever" construct that slows me down and makes me search for meaning and then bugs. It's a waste of my time.


Not Clever, Just a Style

Theory on 2009-02-11T18:01:33

Personally, I hate the style where you test something at the beginning of a statement and then do something if it's true by using and. I much prefer using if or unless at the end of a statement:

    my $_combination_info = {};
    sub combination_info {
        my $self = shift;
        $_combination_info->{ $self->csv_file_name } = shift if @_;
        return $_combination_info->{ $self->csv_file_name };
    }

I use this style all the time. But I have to say, I don't think that your original example was clever, but indicative of someone who prefers a different style of handling one-line conditionals. I would even argue that it's not really idiomatic Perl, but it's not in any way clever.

--Theory

Huh?

Aristotle on 2009-02-11T18:54:54

I don’t see what’s wrong with that.

You probably won’t like the way I’d write it either:

my $_combination_info = {};
sub combination_info {
    my $self = shift;
    my ( $info ) = @_;
    for ( $_combination_info->{ $self->csv_file_name } ) {
        $_ = $info if @_;
        return $_;
    }
}

Re:Huh?

mauzo on 2009-02-11T20:45:20

5.10's given makes that a little less obscure to those unfamiliar with the idiom, or rather, since setting $_ is the only point of given, noone has to stop and think 'what's he looping over again?'

Re:Huh?

Aristotle on 2009-02-11T21:16:10

$ ~/perl/5.10.0/bin/perl -E '$x = 1; given ( $x ) { $_++ } say $x'
1
$ ~/perl/5.10.0/bin/perl -E '$x = 1; for ( $x ) { $_++ } say $x'
2

Now it's my turn to go 'Huh?'

mauzo on 2009-02-11T23:49:49

    perl5.10.0 -E'say \my $x; given ($x) { say \$_ }'
    SCALAR(0x28368370)
    SCALAR(0x283683c0)

It's a copy? Why on earth? Nothing in Perl copies its parameters. Doesn't that just make the whole feature hugely less useful? It's not as though given (my $tmp = $x) {} is that obscure, especially given its common use in conjunction with s///...

Oh, and look:

    perl5.10.0 -E'say \$_ given my $x'
    syntax error at -e line 1, near "$_ given"
    Execution of -e aborted due to compilation errors.

OK, I'll stop thinking given might be useful and start getting used to the idea of for/when statements instead.

</splutter>

Re:Now it's my turn to go 'Huh?'

Aristotle on 2009-02-12T01:02:36

I suppose it’s nice to be able to write given ( $foo ) in place of for ( my $copy = $foo ). But yeah, that threw me too. It’s a pity; a missed opportunity, even.

The fact that you can’t use given as a statement modifier doesn’t bother me.