shinyblog

Saturday, November 24, 2007

nutritious syntactic sugar

Is syntactic sugar just tasty, or does it actually improve the language? Depends on your definition of "improve," I suppose. I just found a ruby idiom in Agile Web Development with Rails that can make a very common, wordy coding task short and clear. We often have to say "give me a thing, and if it doesn't exist yet, make one for me."
In Java:
public cart findCart() {
if (cart == null) cart = new Cart();
return cart;
}
In Ruby, this can be expressed as...
def find_cart 
session[:cart] ||= Cart.new
end

The or-equals operator belongs in a dynamic language, where expressions that evaluate to booleans can also be very nice rvalues. These three lines of code show off a few things about Ruby that might be mistaken for syntactic sugar, but actually make the language better:

  • Avoid unnecessary punctuation.

  • Clean syntax for hashes make them almost as readable as member data accessors

  • Most statements are also expressions.

  • Implicit returns.

It's delicious... and nutritious!

Friday, November 23, 2007

Over-Preparedness Vindicated!

Last winter I posted the contents of my personal geek security pack: some money, some painkiller, some duct tape, a BART ticket, a snack, that sort of thing. Since then I've been carrying it around in my backpack.

In an incident involving an ice cream sandwich and an intra-pocket butter malfunction, I lost my wallet a few days ago. In Berkeley -- that's a large bay away from San Francisco. No problem! Well, okay, yeah, it was a problem, but the problem-ness was much ameliorated by having a BART ticket and twenty dollars cash in my personal geek security pack. I used the money to buy a bus ticket back to look for my wallet at the site of the ice-cream-sandwich incident, then retreated to San Francisco with the loaded BART ticket.

The wallet hasn't turned up, but I've got spare ID tucked away in a safe place (not at home!) and a bunch more cash on my refrigerator door. Over-preparedness: vindicated!

Now-- everybody go back up your data! And store the backups off-site! And put twenty bucks in a secret spot in your backpack! And for the love of Pete, don't put butter in your pockets!

Thursday, November 22, 2007

Slow news is good news?

I rarely read news online. You might think that leaves me out of crucial blogosphere zeitgeist or military-industrial-political news, but nope: my co-workers filter the web for me, forwarding articles about OpenLaszlo, net neutrality, software-as-a-service, and media business models; and I read messenger-bag-loads of books and magazines. Each month, I read Harper's cover-to-cover, with a liberal (heh) dose of the Atlantic, Utne Reader, MIT Technology Review, San Francisco, and occasional forays into The Economist, and the Sunday New York Times. Then a year or two after things happen, I read non-fiction books: The Looming Tower, the Great Deluge, the Assault on Reason, the Shock Doctrine, that sort of thing.
Reasoned slow analysis with editors and proofreaders and fact-checkers, passages I can go back to years later (without the internet way-back machine), passages that authors will have to stand by for decades, footnotes -- yeah, I'll pay for that. What would the invasion of Iraq look like two years later? A fiasco. What about the Thanskgiving 2007 travel breakdown? Check back in two years and I'll have read some reasoned analysis, complete with footnotes.