Uri Guttman wrote:
> i have had the pleasure of doing code review for a client and finding
> this gem:
> 
> sub new {
>     my $class   = shift;
>     my %params  = (@_);
>     my $self    = {};
>     my $package = caller();
>     for my $key ( keys %params ) {
>         $self->{$key} = $params{$key};
>     }
>     bless( $self, $class );
>     return $self;
> }
> 
> even better than just be a poorly written constructor is that it is
> inside a 'factory' module that is inherited by 80 other modules!!

If that's an example of "poorly written" code in your project, can I come work
there?  It at least gets it right, if verbosely.


> anyhow you have two challenges, if you desire. the first is very easy
> but amusing when compared to the original code. rewrite that sub (a
> minimal constructor that takes a key/value list and makes a hash based
> object with no extra checking or work) with the shortest but cleanest
> and clear code you can.
> 
> i have a clean 2 line version that would be understood by almost
> anyone. it was the one i benchmarked against the original.

sub new {
        my $class = shift;
        return bless( [EMAIL PROTECTED], $class );
}


> your second challenge is to golf the same constructor. the char count
> will be between the {} of the sub body as the name and 'sub' are
> constant.
> 
> i have two solutions of 33 and 24 chars so you have to beat those. the
> longer one is amusing enough to show it later.

sub new [EMAIL PROTECTED],shift}

Was my first shot at 23.  Then I figured I could knock off that range op...

sub new {$a=shift;[EMAIL PROTECTED],$a}

21.  And it's even strict clean. :)


-- 
THIS I COMMAND!

Reply via email to