Monday, October 27, 2008

Over the top... er, front

So I took my first spill on my bike riding into work this morning.

I was right at the end of my 12 mile ride: I had gotten all the way to campus, and was crossing the street. I got to the curb and tried to pop my front tire onto it... and missed. I don't know whether I was tired from the ride, or whether I just timed it badly, but I went right over the handlebars.

So I hit the concrete on my elbow and rolled, then my bike landed atop me. Oof.

My elbow and my ego took the brunt of the damage: I'm having trouble bending my left arm right now, but nothing's broken (I think)---it's just a little stiff. My helmet's a little dinged up, but I don't recall hitting my head... I'm hoping that's not a symptom.

I had to pick up my water bottle and bike light, and I decided to walk the rest of the way... all 100m or so to this building.

Mother Earth better be @#$%# grateful.

Sunday, October 26, 2008

Mt. Rainier

If you haven't spent time in this area, you might have trouble understanding the extent to which Mt. Rainier dominates the landscape. It doesn't dominate the view so much as it defines it. Ames took this picture a couple miles from the house:
From Mt. Rainier

You can see Rainier from almost anywhere here, except the house where we live: we have some tall trees right at the east end of our yard.

Rainier is about 30 or 50 miles from our house, so we decided to head on out and take a look. We didn't actually anticipate getting there, but we figured we'd head out to explore in that direction and take a look around.

There are some interesting things between us and Rainier: Alder Dam was impressive
From Mt. Rainier

We wound through several small towns and finally got to Mt. Rainier National Forest. A day pass is $15, a 12-month pass is $30. So we bought a year pass.

We never actually got to Rainier, which wasn't really a surprise. But we did manage to go for a walk in the foothills. The landscape definitely reminds me of home:
From Mt. Rainier

The "lonely road at the base of the hill" look sure takes me back to BC.

There are a lot of mushrooms on the west coast, and I ended up tagging some in photos:
From Mt. Rainier

From Mt. Rainier

It was terribly refreshing to walk in the cold damp air. That's a winter-on-the-Pacific-coast thing. We might not get a lot of snow, but the winter damp cuts like a knife. I've been out in the cold (I mean real cold, not just freezing temperatures), and it has its challenges; but there is a unique coldness to the damp air on the coast. To be sure I've never wintered in Cambridge Bay, but you get the point. Most places get dry in the winter: our winter humidity presents a unique cold.

From Mt. Rainier

Anyhow, we spent some time walking on a path that followed a creek up a foothill
From Mt. Rainier

From Mt. Rainier

The creekbed itself is bright orange, I assume that's clay washing down from deposits upstream, but I don't know for sure.
From Mt. Rainier

There were some really interesting branches along the path: trees apparently had some unique challenges in that forest:
From Mt. Rainier

From Mt. Rainier

From Mt. Rainier

We even found a hollow tree.

It wasn't an epic journey or anything, but it was certainly a nice way to spend a Saturday afternoon.
From Mt. Rainier

Friday, October 17, 2008

Perl Collections

I read this comment on Yegge's blog from February 2008:

For example, if you see this in Perl:

%x = map { $_ => 1 } @words;
@words = keys %x;

without a comment, you should fire, or at the very least yell at, whoever wrote it. (There are efficiency reasons for not writing that as well, but mainly it's a clarity issue.)

I've been mulling this over for the last couple weeks, and I've decided The Comment deserves comment. I would actually respond to this in the comments on that blog, but they've been closed a long time, and so I'm putting my comments here.

I doubt anyone but me cares about this: I'm just venting here.

Let's begin with the most obvious counterpoint: if you have an employee you're paying to write Perl, and he or she can't understand

%x = map { $_ => 1 } @words;
@words = keys %x;

you're getting ripped off. No-one getting paid to read or write Perl can be excused for not immediately seeing what that code does. That is not advanced code, the variables are clearly named, and the idiom is well-known. If you can't grok that code, you need to bone up on your Perl. If Perl is just another tool in your toolkit as a professional, you might not immediately see what it does... but in that case, you ought to expect to be opening the Camel Book fairly frequently anyhow. In that case you don't expect to immediately understand what every line of Perl does as soon as you see it; but you do expect to be looking things up.

I'm not claiming that's the best way to get a unique list (List::MoreUtils::uniq is much better, for example); but it's a very short solution that will work consistently. It might not be the fastest solution, but it's reasonable, correct, and terse. And frankly, it's also fairly clear.

Part of the problem goes back to what a former boss I highly respect told me once: people write bad Perl because they can. Perl is a simple language to learn, and it's very forgiving. One result of that is, there is a decided lack of "raising the bar" on Perl newbies. And frankly, one of the great things about Perl is the community that encourages newbies, rather than harassing them. So maybe ugly Perl is an acceptable trade-off. Maybe the benefits of a DWIM language and a welcoming community far outweigh any evils stemming from naive and/or verbose code.

But maybe there is a middle path, where newbies can be encouraged to write better Perl without being harrangued and beaten. Maybe it's possible to gently and casually get the message across that baby talk is great for babies, but inappropriate for adolescents. Naivety and inexperience are nothing to be ashamed of, but they're not something to be proud of either. I think that's what my boss was trying to tell me.

And incidentally, it was that boss that really forced me to learn Perl properly. I knew Perl before, but my Perl really sucked. He forced me to use it, and raised the bar on me whenever I started to make progress. I'm no Perl expert, but whatever small advances I've made started when he put the pressure on me to write better Perl, instead of stuff that actually ran.

I think there is a related factor here too: Perl is a huge language. It was designed that way, and there are nooks and crannies that can go unexplored for years. I'm no Perl guru, but I know a lot more Perl now than I did two years ago, when I had already written a lot of Perl professionally. It seems I'm constantly saying "Well, I thought I knew Perl before, but now I realize I didn't". This is part of the famous TIMTOWTODI: having many ways to do any given task implies a fairly large and flexible vocabulary. It also implies a learning curve that is very long, even if it might well be very steep at times. Perl is a language that can surprise you even if you've been learning it for years.

I've found my Perl has gotten simpler and less like line noise over time. I've been known to write comments here and there, and my variable names have gotten more descriptive; but that's not what I mean. I find I almost never use && or || anymore, preferring and and or. I frequently use a die unless... idiom now: I find having a die at the start of a line emphasizes this is a potential exit point much more strongly. I also prefer to use do_something() if... instead of if(...){do_something()}, as it just seems to flow better. And I avoid if not, using unless.

One place my Perl has decidedly changed is, I now use list operators much more frequently. Maybe that's because I've spent some time writing actual, working code in Lisp, maybe not. But one thing is certain, I use map, grep, split, and join a lot more now. I used to do things like

foreach $line (sort @lines) {
print "$line\n";
print "\n";

Now I write that as:

print join ("\n", sort @lines), "\n";

I'm not sure that's really an improvement, except I prefer to use fewer lines to do the same amount of work. One line to accomplish a task in a reasonably clear manner seems like a better ROI for my Carpal Tunnel pains than four.

But I'm not trying to argue I'm brilliant, or anything like it. I'm merely using my own Perl as an example: over time, my Perl has changed its form decidedly---for the better, I think. When I started writing Perl, my programs looked like I had messed up my terminal settings: they were full of cryptic symbols and complicated key words. Now it reads more and more like English. I think that's an improvement.

And particularly when it comes to list operators, I've found replacing a lot of my for or foreach blocks with map blocks has had the net effect of making it easier to follow the code flow in my head. It's not always the most readable, but there's a sense where readable is in the eyes of the beholder. I honestly find grep inside map more readable than nested loops. It lets me keep track of things like counting variables and nested scopes a lot more easily.

So I'm looking at the "bad Perl" example in The Comment, and I'm wondering exactly what the problem with it is. It seems there is a fear of list operations, which seems to be a particular instance of a fear of not explicitly shuffling variables.

Before I read The Comment, a friend had asked me how to do a word count in Perl. I have to admit my solution looked a lot like "bad Perl":

my %count = map { my $word = $_;
$word => scalar grep { $_ eq $word } @words } @words;

It's not terribly efficient to call grep inside map, I understand that. But you'll end up with a nested loop no matter what you do: mine is only two lines long, and seems reasonably clear. Isn't that the point? I assume if we're working in Perl, then performance is not the main consideration: C or Java or Lisp can all knock out a word count a lot faster. We generally choose Perl because it's a great language for thinking: it's a great language for ripping apart a problem quickly and simply. That's frankly why I came back to Perl from Java: I wanted to get stuff done.

So if we want a language to think in, why wouldn't we use as much of it as we can? Why wouldn't we pick up little idioms and bits of vocabulary that make thinking easier? Why wouldn't we sum up several lines of foreach into a single, short map? That's why we have things like synonyms, right? That's why we develop vocabularies in natural languages to encapsulate ideas that pop up. We use words in modern Physics now that Newton didn't use: that's because we've gotten new ideas, and they've required new words. Sure, Newton's English was Turing-complete (so to speak), but it's a lot easier to model modern Physics with our extensions to it, and our newer vocabulary is generally considered a good thing.

So I've decided that not only is The Comment inaccurate, it's actually a step back. Good Perl, like good English, is concise and accurate. Gratuitously using arcane vocabulary in Perl is bad, just like it is in English. But deliberately limiting your vocabulary at the cost of brevity and clarity in spoken languages is laughable (try it: carry on a conversation without using nouns introduced in the last century, and see how ridiculous you sound); and I've concluded it's the same in Perl. I've become convinced the Right Thing to do is to encourage wider vocabulary, both in my own code and in others'. And if that forces the poor sap who maintains my code to look a few things up in the Camel Book, well he can thank me later, when his Perl is a little more fluent and his vocabulary a little broader.

Thursday, October 16, 2008

Going Green

When we moved to the NorthWest, I purchased a bike specifically to ride to work. The new job is about 10 miles from the house, but the less direct route is about 12 miles long: longer, but the shoulders are wider and the roads not quite so fast. So today I rode it the first time.

Ames met me at the campus and drove me home, but I did 12 miles on the bike today. It took almost exactly one hour, so I averaged 12 mph. I expect that to get shorter as I get into better shape, but it was frankly better than I expected.

I'm very excited about the new job, by the way. I start Monday!

Wednesday, October 15, 2008

Home again, Home again, tiddle-lee-dee

"Well, I'm back" --Sam Gamgee

The trip to Charlotte was a success on any measurable level: we accomplished our goals, at least as well as can reasonably be expected.

I got to work with a new acquaintance who'd probably have been promoted to "friend" if I hadn't met him as I was leaving. He's a Solaris guru and a shell scripting genius, so I learned a lot. In return, I was able to share some Perl-fu. I was flattered when he told someone, "I thought I knew Perl until I worked with Ox."

But the high point was certainly seeing Ames and the kids again.

So I'm home, the new job starts Monday, and I'm very happy to be home.

Sunday, October 12, 2008

I haven't posted in a while: I've been in Charlotte, NC since 9/22. I'm heading home day after tomorrow, which excites me greatly.

I'v fallen behind on email and blogging, I'm starting to try and catch up on that slack now.