-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 =pod Perl Golf has a bad reputation, but sometimes thinking long and hard about a block of code can really improve it. (Perl Golf takes this a little too far, which is why it has the bad reputation.) As an example, I just wrote this code in C: lang:Perl if(!$loop_killer) { if($args{warning} && $args{warning} eq '1') { print {*STDERR} "OMG SOMETHING BAD HAS HAPPENED NOES"; } elsif($args{warning}) { print {*STDERR} $args{warning}; } } There are a number of problems with this code. The first one we notice is that C<$args{warning}> shows up way too many times. I golfed this down to: if(!$loop_killer) { my $w = $args{warning} if($w && $w eq '1') { print {*STDERR} "OMG SOMETHING BAD HAS HAPPENED NOES"; } elsif($w) { print {*STDERR} $w; } } Shorter, but calling it C<$w> is not a great improvement. (BTW, the C<$w &&> construct is to avoid the "Use of uninitialized value $w in string eq" warning when C<$args{warning}> is not passed in. C<$w &&> is less typing than C.) Let's rewrite the if statement: if (!$loop_killer) { if(my $w = $args{warning}) { if ($w eq '1') { print {*STDERR} "OMG SOMETHING BAD HAS HAPPENED NOES"; } else { print {*STDERR} $w; } } } This is much clearer. We don't repeat anything; making sure C<$w> is defined and true is a condition of both cases, so we factor out the test for that from each case into its own loop. It is still pretty deeply indented, so perhaps we can combine the two conditions for printing a warning and write: if (!$loop_killer && (my $w = $args{warning})) { if ($w eq '1') { print {*STDERR} "OMG SOMETHING BAD HAS HAPPENED NOES"; } else { print {*STDERR} $w; } } We can still take this further. (I was happy with my real code at this point, but since I am blogging about it...) We are at the end of a function when all this happens. So we can remove even more nesting, perhaps like this: my $w = $args{warning}; return if $loop_killer; return if !$w; if ($w eq '1') { print {*STDERR} "OMG SOMETHING BAD HAS HAPPENED NOES"; } else { print {*STDERR} $w; } Then, we can finally eliminate the remaining C block: my $w = $args{warning}; return if $loop_killer; return if !$w; $w = "OMG SOMETHING BAD HAS HAPPENED NOES" if $w eq '1'; print {*STDERR} $w; Basically, thinking about the code and trying out different ways of writing the same thing can help you find something more readable. The first thing that came to mind got the job done, but it was pretty verbose. As we eliminated the repetition and nesting of conditions, we made the code nicer looking and perhaps even easier to maintain. Golfing is actually good for your code! -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkpzzh0ACgkQ2rw+dVvzZm1dIQCgjrAuoWAOFdqioWKzqXlPdgx7 e2MAni2fOTzU+nA5QwdURw5oDUVDYoH9 =CC4m -----END PGP SIGNATURE-----