[Dub-pm] 'Shortening' code.

Sean O'Riordain seanpor at acm.org
Sat Apr 24 03:23:45 CDT 2004


I suppose it depends... beauty is in the eye of the beholder... and the 
intended audience...

In C the line
while ((c=getc())!=EOF) {
is a common idiom - but if you haven't seen it before it takes a bit of 
reading and once you're used to this idiom, then it becomes a single 
chunk of information in your head (or maybe just two)... there is a fine 
line between being overly obvious and overly clever...

All languages have their own idioms, and perl is no exception, ie we 
often write
while (<>) {
which is not at all obvious to those coming in from another language, 
but completely obvious to us - isn't it? :-)

Joseph Hall's "Effective Perl Programming" has a chapter on "Idiomatic Perl"

The other day, Tim Bunce gave us the very neat
push @abz, \($cache{ $aref->{bzone} } ||= $aref->{bzone});
which took me a while to figure out - and when putting it into my code, 
I'd definitely have to put in a comment before it explaining it's "intent"

While I haven't read it in a while, Steve McConnell's "Code Complete" 
discusses these sorts of issues and others - for those who haven't seen 
it, it is >800page tome on coding - definitely a good read!

cheers,
Sean

And yes - I do read too much and don't code enough :-)

Dave O Connor wrote:

>http://www.perl.com/pub/a/2004/03/12/ioall.html
>
>This is just a personal rant from me, but does anyone else feel that
>'shortening' code, linewise, at the expense of execution time is something of
>a fallacy? The above mentioned module seems to do just that, and it makes my
>nerd blood boil.
>
>Anyone have any other particularly nasty examples of this? I know I've seen
>some terrible examples of readability in my time (most of them under NDA, so
>not reproducable here :)). I would consider myself a great opponent of the
>'one-liner', that ends up looking like line noise, in favour of readability
>and the ability for your successor to take it up, read it, and understand it.
>
>	- DoC
>_______________________________________________
>Dublin-pm mailing list - Dublin-pm at mail.pm.org
>http://dublin.pm.org/ - IRC irc.linux.ie #dublin-pm
>
>
>
>
>  
>



More information about the Dublin-pm mailing list