SPUG: Readable, well-written code (was: Best One-Liners and Scripts for UNIX)

Richard Anderson richard at richard-anderson.org
Sat Apr 19 20:11:40 CDT 2003


I've avoided commenting on the "minimal keystrokes" style of coding that
we've seen examples of, but Bob's statement that "I feel the
understandability of perl quickly slipping away when I (try to) read stuff
like this" has brought me out of the woodwork.

My view is that fluent Perl takes advantage of the Perl idioms that enhance
cohesion and readability and avoids Perl idioms that obfuscate for the sake
of producing terse code.  An example of this is the grep function.  Any code
that uses grep can be rewritten more verbosely as a foreach loop.  However,
grep enhances readability because when the reader sees grep he knows the
code is selecting members from a list, whereas the loop is less immediately
obvious.  Grep has more cohesion than a loop because its function is limited
to selecting members from a list.

Compressing code using Perl's convenience features can (and often does) go
too far.  When I find myself wondering how fancy to get with some Perl
feature, I ask myself "Would an intermediate Perl programmer find the
resulting code to be clear and readable?"  If the answer is anything but an
obvious yes, I (usually) resist temptation and write the code using the
simple, "unclever" style.

Writing clear, readable code that has high cohesion and low coupling is hard
work.  I admit it is fun to play with some of Perl's obscurities and I enjoy
reading the Obfuscated Perl contest winners.  Code that is posted to a
discussion list shouldn't have to be of the same quality as production (or
even CPAN) code.

I hope that Bob and others are not assuming that there is anything about
Perl that leads people to write bad code - this is a function of the
programmer, not the language.  (However, if anyone wants to slam awk or
shell scripting as being inferior languages, I'll be right behind you.)

Richard Anderson
richard at richard-anderson.org
www.richard-anderson.org
----- Original Message -----
From: "Bob Hiltner" <bob at hiltners.com>
To: <spug-list at pm.org>
Sent: Saturday, April 19, 2003 1:22 PM
Subject: Re: SPUG:Best One-Liners and Scripts for UNIX


> Seeing all the one-liners (not this post particularly) brings to mind one
of
> the great things about PERL.  You don't need code obfuscation utilities
for
> IP protection.  It's done automatically at the source code level! ;^)
> What's the other way to say it?  Write Once, Read Never?
>
> On a more serious note... that is one of the things that does make me shy
> away from getting into perl in a serious way... too much magic.  This
stuff
> that (perhaps) makes sense to the initiated and the high priests is like
> reading binaries to the casual perler.   I've been doing more .Net stuff
for
> a while, and I feel the understandibility of perl quickly slipping away
when
> I (try to) read stuff like this.  I don't see the same issues at all when
I
> get away from, say, C for a while.  Is that just the idioms of the
language?
> There's plenty of cryptic stuff available in C too, but I don't see it
much
> in real code.
>
> So what's the balance?  One can be very efficient in use of precious
memory
> at the expense of the sucker who has to follow the code, or one can write
> verbose cobol, and the follower can settle in for reading a nice long
> bedtime novel.  So the question is, if writing clear and readable perl is
> possible, which I believe it is, is it just my perception that the idioms
> are toward opacity over readibility?  Isn't that a bit of a shame?  How do
> you get to the inherent coolness of the language without all the
unreadable
> magic?
>
> It might just be me.  I tend to have the same problem with regexes, which
I
> think are the coolest thing going.  If you don't use em for a while, it's
a
> bit of retraining each time.  It'd be nice to have that kind of power with
> some better readibility.
>
>     -Bob
>
> ----- Original Message -----
> From: "SPUG-list-owner" <tim at consultix-inc.com>
> To: "Brian Hatch" <spug at ifokr.org>
> Cc: "Tim Maher" <tim at consultix-inc.com>; "David Dyck"
> <david.dyck at fluke.com>; <spug-list at pm.org>
> Sent: Saturday, April 19, 2003 10:37 AM
> Subject: Re: SPUG:Best One-Liners and Scripts for UNIX
>
>
> > On Fri, Apr 18, 2003 at 06:33:02AM -0700, Brian Hatch wrote:
> > >
> > > > Ouch! That's too complicated for my "Minimal Perl" tastes.
> > >
> > <SNIP>
> >
> > > I also wanted the use of the '..' operator - you're not going
> > > to take that away, are you?.
> > > --
> > > Brian Hatch                  Language, it's a virus.
> >
> > Here's a version that honors the range operator.
> >
> > #! /usr/bin/perl -wlna
> > # show_fields2c
> > # (C) 2003, Tim Maher        www.TeachMePerl.Com
> >
> > BEGIN {
> > # first argument contains comma-separated field numbers
> > # order of numbers determines order of printing fields
> > ($fields = shift) =~ /^[\d,.]+$/g or
> > die "Usage: fields='2,1,3,4..7' $0 [ file ... ]";
> > @fields = eval " ( $fields ) "; # 5,2..4 => 5,2,3,4
> > }
> >
> > @F or next; # skip blank lines
> > # Let user specify from #1
> > unshift @F, "(No zeroth field! first is #1)";
> > print  "@F[ @fields ]";
>
>
>
> _____________________________________________________________
> Seattle Perl Users Group Mailing List
> POST TO: spug-list at mail.pm.org
> ACCOUNT CONFIG: http://mail.pm.org/mailman/listinfo/spug-list
> MEETINGS: 3rd Tuesdays, U-District, Seattle WA
> WEB PAGE: www.seattleperl.org
>
>




More information about the spug-list mailing list