Re: bad code to good golf

[email protected] (Uri Guttman) Sun, 09 Dec 2007 13:23:11 -0500
Newsgroups perl.fwp
Message-ID <[email protected]>
>>>>> "MGS" == Michael G Schwern <[email protected]> writes:

  >> even better than just be a poorly written constructor is that it is
  >> inside a 'factory' module that is inherited by 80 other modules!!

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

well it is a live system so it must be 'right'. i was hired to review it
and the double copy of the hash data is what bothers me. as this is
called zillions of times by all those modules it is a quick and easy
speedup.

and they are hiring more outsource indians so you won't be contacted. :(

  MGS> sub new {
  MGS> 	my $class = shift;
  MGS> 	return bless( {@_}, $class );
  MGS> }

my clean version is:

sub new {
	my ( $class, %self ) = @_ ;
	return bless \%self, $class ;
}

i don't like using shift for args if i can help it.

  MGS> sub new {bless{@_[1..$#_]},shift}

that is the same as my shorter golf answer. maybe i counted mine wrong.

  MGS> sub new {$a=shift;bless{@_},$a}

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

that is nice. the other replies beat it though!

uri

-- 
Uri Guttman  ------  [email protected]  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org