[vienna.pm] Programmierstil

Roland Bauer Roland.Bauer at mediaprint.at
Wed May 31 03:21:21 CDT 2000

* * * vienna-pm-list * * *

In der Mailingliste perl5-porters at perl.org eine Gegenueberstellung
zweier extremer Programmierstile:

A) Perl-typisches Arbeiten mit Defaultwerten (zB: $_, @_) und
B) alles ausprogrammieren, ohne die Perl-Besonderheiten zu nutzen.

Von: 	Tom Christiansen <tchrist at chthon.perl.com>
An:	Horsley Tom <Tom.Horsley at ccur.com>
Datum: 	26.05.00 17.11 Uhr
Betreff: 	Re: Thoughts on maintaining perl 

>Things like ... implicit $_ or @_

Used judiciously, implicit $_ produces more readable code
than explicit variable use.  It's a pronoun.  Imagine speaking
English without any pronouns.  But long stretches without
antecedents will confuse just as short ones without pronouns
will annoy.

For example, this is *incredibly* clearer:

    while (<>) {
        next if /^=for\s+(index|later)/;
        $chars += length;
        $words += split;
        $lines += y/\n//;

than using the the utterly obfuscated IMPLICIT NONE version:

	if (@ARGV == 0) { 
	    @ARGV = ('-');

	while (@ARGV != 0) {
	    $ARGV = shift(@ARGV);        
	    unless (open(ARGV, $ARGV)) {
		print STDERR "Can't open $ARGV: $!\n";
		next ARGUMENT;
	    while (defined($line = readline(*ARGV))) {
		if ($line =~ /^=for\s+(index|later)/) {
		    next LINE;
		$chars = $chars + length($line);
		$words = $words + split(' ', $line);
		$lines = $lines + ($line =~ y/\n//);

The former is incredibly easier to read, to write, and to maintain
than the latter, which is the kind of painful crap you'd expect a
Pascal or Python programmer who didn't actually know Perl to produce.

Use $_ as the implicit pronoun.  It's good for you.  Counteridiomatic
Perl is *not* helpful to anyone.

next ARGUMENT, please. :-)


You are subscribed to vienna-pm-list as "Roland Bauer" <Roland.Bauer at mediaprint.at>

More information about the Vienna-pm mailing list