Mike Cramer on 12 Jan 2004 14:32:47 -0000

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: What use is the Schwartzian Transform?

Mark Dominus wrote:
Michael Cramer <cramer@webkist.com>:
Fair enough.  But why doesn't a quick glance tell you at least that
much about C, which has

@sorted = sort { $date{$b} <=> $date{$a} } @filenames;

??  I would think that a quick glance at C would strongly suggest that
the filenames are being sorted by date, since the word 'date' is right
there in the sort comparator; there isn't anything similarly
documentative in B.

This is a serious question.

Yes, but the rest of C is what obscures it. Three lines before and one line after, all of which I have to be sure are really in the service of the sort, not some code that just happened to butt up against it. And if you already knew you could ignore those lines, it probably doesn't matter which idiom you use.

No, the S.T. is not self documenting -- and the code itself is not obvious even on a slightly closer read -- but we're talking about maintainability. And because the S.T. is so compact, and with few red herrings or side effects, the obscurity is not that important.

But to be honest, I think I've used the S.T. maybe once or twice in the years since I first came across it. And both times were probably a few days after I read about it on a mailing list or in a book somewhere. As much as I'm defending it here, in the real world -- or at least *my* world -- it's a cute curiosity. Which I'll probably use somewhere this week, because it's on my mind.

for (@filenames) { $date{$_} = -M $_ }

. Would you have liked it better?

They are both fine. My gut reaction though is your initial version was less obvious. It is not a pattern you use frequently, so it tends to slow me down as I scan the code.

Mike Cramer
http://www.webkist.com/ | AIM: MikeWebkist

**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**