So I'm reading some Perl code. I usually stay away from Perl as much as I can, but I'm trying to figure out some undocumented behavior of the MusicBrainz RDF server so I have to UTSL. Anyway, I get to this line:
return ("No artist name or artist id given.", $data, 0, undef)and I get confused, because I saw the artist ID check but not the artist name check. So I start reading backwards to see if I missed something; maybe the artist name check happened in some non-obvious function call? I go skimming through various other files to see if some other code is filling in the artist ID or something. Eventually, I give up and come back to that line, and I notice that the next line is:
if ($data->{artist} eq '');and I smack my head. I'm willing to chalk this up to bad style (and I hope people would agree this is bad style), but now I'm wondering: when is it ever good style to put the conditional guard after the code it's guarding? Or, more generally, when is it ever good style for the order of code (in the same basic block) to be the opposite of the order of execution?
From:
no subject
From:
no subject
I don't get the last example, though. Wouldn't the post condition version be:
From:
no subject
(!open(my $fh, $filename)) and die "Can't open $filename: $!\n";
Switching back to the main topic of conversation, the one thing in Python I occassionally really really want is a 'repeat..until' loop for cases where I want some code executed at least once but likely more.
From:
no subject
From:
no subject
From:
no subject
From:
no subject
but I really don't like that in most cases.
Basically, I put the most important part first in any long construct. In the case of the open(), that's what's really key. Think of it in English. Are you saying "Unless it's raining, we'll have a picnic", or do you say "We'll have a picnic, unless it's raining." The picnic is usually the most important part.
From:
no subject
From:
no subject
Python is particularly curious; from the get-go it had "lambda" but it was so horribly weak it was obvious the designer didn't even understand the languages he was so blantantly copying from. (I believe he copied from Ruby, which got closures in the Scheme sense right; which Ruby copied from Smalltalk).
At OOPSLA, the creator of Smalltalk, Alan Kay, said "My language represents an improvement, particularly among its successors". (He was quoting what was originally attributed to the creator of Algol.)
Alas, people like you and I who know better (i.e., know Scheme) are stuck with languages like Perl sometimes or Python/Zope. Meanwhile, the wonderfully designed languages like Dylan have no user base or libraries. I'd probably do a lot more web-programming if Dylan were somehow the language of choice for it.
From:
no subject
From:
no subject
Anyway, it's just a fact that sometimes the trivial dominates over the fundamental. I used to be the same way until I studied programming languages at Northeastern under Mitch Wand, Matthias Felliesen, and Karl Leiberherr.
From:
no subject
From:
Exactly...
From:
no subject
From:
no subject
As for the perl fragment in question, the camel book actually talks about when it's good style or not. However, it's at work and I'm at home. I think it's something like "put the important clause first"
As for my weapon of choice, when I'm not being paid, it's almost surely Ocaml. It's mostly functional, has a powerful, terse syntax and has a really good native code compiler.
From:
no subject
From:
no subject
From:
no subject
From:
no subject
From:
no subject
From:
no subject
From:
Quoting from the perlstyle man page
open(FOO,$foo) || die "Can't open $foo: $!";
is better than
die "Can't open $foo: $!" unless open(FOO,$foo);
because the second way hides the main point of the statement in a modifier. On the other hand
print "Starting analysis\n" if $verbose;
is better than
$verbose && print "Starting analysis\n";
because the main point isn't whether the user typed -v or not.
From:
Re: Quoting from the perlstyle man page
From:
Re: Quoting from the perlstyle man page
From:
Re: Quoting from the perlstyle man page
From:
Re: Quoting from the perlstyle man page
Python is the other way around.
From:
Re: Quoting from the perlstyle man page