Sunday, February 15, 2009

And Now for Something Completely Different

The migration turned out to be a whole bunch easier than expected. Three cheers for WordPress' built-in Blogger migration support. It's not 100% perfect, and I'm not set on the theme either, but it's good enough to kiss Blogger goodbye.

Update your bookmarks:

Update your feed readers:

Thanks for all the good times, hope to see you 'round me new place.

Time for a Change

I started this blog with a tip of the hat to my earlier efforts at an online journal and a hope that Blogger would be a more permanent home than past attempts. By any objective measure, Blogger has been a complete success, clocking a total of 289 posts since April of 2005. Less prolific than, say, Wonkette's twenty posts a day, but not too shabby for a dude whose never kept a blog longer than a year.

Recently, I've grown frustrated with Blogger. It's a fine platform, and having someone else deal with the hosting is certainly a plus... but in the end, it's a service over which I have no control. It does exactly what Google wants it to do, and nothing more. For a long time I didn't want more... but times, they are a changing. The first hint of longing came when friends launched two new blogs with WordPress, Minor Failures and GeekBeer. Both blogs have gravatar support, an idea with which I am absolutely smitten. Then, most recently, I posted some code examples and found the Blogger support for showing that code was most disappointing. Combined with the byzantine themeing system, the inability to change the blog's domain name, and the general need to refresh the look & feel of the site; one gets a very compelling case to switch blogging platforms.

I administer dozens of WordPress blogs for work and thus have a deal of familiarity not only with general hosting and administration concerns, but also the internal code structure. PHP isn't my first choice of languages, but it's always fun to contribute when you can. So, gentle readers, this marks my last second-to-last post with Blogger. I'm going to take a few days to write a conversion script that will port as much content as possible from the old blog to the new one and find a reasonable theme from which to start customization. Soon as the new blog isn't embarrassing to look at, I will switch the Apache configuration so that points to the WordPress deployment instead of redirecting you to Blogger. Once that is done I will make a final post letting readers know where the new RSS feed is at and then leave good old reliable behind.

Wednesday, February 11, 2009

The Trouble with Enumerables

Quite a lot of political postings for what's suppose to be a technical journal recently... time for a return to our traditional values!

Today's topic is Enumerables. Originally I thought this post was going to be about iterators, but on reflection, iterators aren't really the trouble here... but I'm getting ahead of myself. Ruby, dynamic scripting language of MVC fame, has this Enumerable concept. What it does is takes a set of objects and performs actions on that set of objects, sometimes returning a modified set, sometimes returning a single element from the set. The available actions are known as iterators, and some of the more common ones are each, select, and collect.

The underlying mechanic for all of these iterators is the each iterator, that does nothing more than return each item in the set, one at a time. You can then wrap the each iterator with additional functionality to generate all the additional iterators... so, if you wanted to go through each item in the set until you find an item that meets an established criteria, you would use detect. Or, if you wanted all of the items that meet the criteria, you would use select. The big upside is that you get really clean looking code since you are using all this built in magic instead of writing your own.

Let's look at a bit of code to see what I'm talking about. We start with an array of hashes describing a person, with their name and address:
  person_array = [
:name => 'alice',
:address => '123 Fake Street'
:name => 'bob',
:address => '1600 Pennsylvania Avenue'
:name => 'charlie'
:address => 'Infinite Loop'
Now, we want to know alice's address, so we can use the detect iterator to find a hash with a :name value that equals "alice"
  person = person_array.detect { |person| person[:name] == 'alice' }
puts person[:address]
Which, one must admit, is pretty slick. In that one little line of code we go all the looping, variable checking, and returning behavior. You can even take it a step further and wrap the whole thing in a method:
  def address_for(name,person_array)
person = person_array.detect { |person| person[:name] == name }
return person[:address]
Now, you're probably asking, if I think all of this is so slick, why title this post The Trouble with Enumerables? The reason these little guys are trouble is because they can hide inefficient implementations behind clean looking code because it teaches developers to treat all sets of data as equivalent.

But, the truth is, sets of data are not equal in the eye's of their maker. In fact, in the dynamic language world there are really two different kinds of sets, each with their own particular strengths and weaknesses. On one hand, you have the arrays, which is ordered data, meaning that each item in the set is stored in a specific order, 0..n, and you can loop through that safe in the knowledge that you're going to get the data in the same order every time. On the other hand you have hashes, which is keyed data, meaning that each item is stored based on a hashed key value, such that you can find it again quickly by just repeating the hash algorithm.

Let's return to the code example from above. I used an array there, but if I know I want to use that data to find addresses based on a name, it would be much faster if I used a hash that looked like this:
  person_hash = {
:alice => '123 Fake Street',
:bob => '1600 Pennsylvania Avenue',
:charlie => 'Infinite Loop'
Then to access alice's address all we would need to do is:
  puts person_hash[:alice]
We can even wrap it in the same method as above:
  def address_for(name,person_hash)
return person_hash[name]
Now, the output is the same in both cases, "123 Fake Street", but the two implementations differ in important ways. The first one (with the array) is an O(n) speed function, meaning that to find the desired result under the worse case scenario, it will take n runs of the function to do so (n being the number of items in the array). That's because we have to look at each item in the array to see which one has a :name value of "alice". The second implementation (with the hash) is just O(1), or whatever constant number you want to put in between the parentheses. No matter how many items are in the hash, it will take the same amount of time to find alice's address.

Which is why Enumerables are trouble. Because they are so darn easy to use they hide the fact that you may have traded a possible constant time function for a linear time function with no actual gain in functionality.

So, my fellow ruby developers (and any other language that has enumerable like functionality), the next time you reach for your favorite iterator, ask yourself, am I using this because it's easy and looks clean, or am I using it because it's the best tool for the job? There are plenty of good reasons to using an enumerable, but if your sole reason is because it's "easy and clean," then you are only asking for trouble down the road when you put your code into production and suddenly that array of four items you tested during development has become 4,000 items and that one function is slowing everything down, to say nothing of all the other functions where you made the same short-sighted decision.

Monday, February 09, 2009

Thoughts on Stimulus

Sen. Arlen Specter (R-PA) has a piece in the Washington Post this morning entitled, Why I Support the Stimulus. Beside the rather boring title, I think -- in general -- I agree. There has been much machination about how the Obama administration failed to play this properly, but I think that's wrong... both in function and form.

Functionally, this is a stimulus bill (though, I prefer the term recovery bill, not sure why that language isn't used more frequently) whose primary purpose is to get the economy moving through a large scale injection of government spending. Period, full stop. It is not a green energy bill, or a universal health care bill, or an education reform bill. Those partisans who saw the stimulus as an opportunity to attach their personal pet project, regardless of how meretricious the idea might be, are guilty of the same sin as when the Republicans used September 11th to push through only tangentially related policy objectives through a hurried congressional approval process.

Specter, a moderate Republican, has joined with other Senate moderates to trim many of these programs. The folks over at Think Progress' Wonk Room would have you believe the sky is falling and that these Senators oppose the programs they are either eliminating or reducing. But there is little evidence of that, and Specter admits that many of the programs being cut are "worthy in themselves." But his point is that we have an appropriations process for this sort of thing, and with that process comes deliberation, transparency, and accountability. Just as anti-war activists were angered by the Bush Administration's refusal to fund the Iraq War through the normal appropriations in an effort to hide the real cost, so too should we be angry when any other administration tries to go through the back door.

Which brings us to form. This isn't a game! Do you hear me Nate Silver. Obama did not run -- and he did not win -- on the argument that he was going to get his way every time. He was elected on the premise that government is broken because we treat it like a game. There is this great story, which I can't seem to find online now, that I first heard reported on the Daily Show. Leading up to the 2006 midterm elections, where the polls suggested the Democrats where going to seize power in the House (the Senate was still too close to call), a White House official was asked how Bush was going to work together with the new Democratic Committee Chairs. The official responded with a glib response about how, "we are playing this game to win it," implying the Administration wasn't going to entertain the idea that the Republicans would lose their majority. Then, in a moment of absolute political honesty, a reporter gave a follow up... "It isn't a game. The American people want to know how you are going to govern."

And the dude was absolutely right. I've been in politics, I know it's easy to treat the whole thing like a game, with pieces you move around the board and objectives achieved. But this is real life, it has real consequences, and developing strategies based on the philosophy that this is a game, and not governing, is exactly what Obama ran against. He is governing, best he knows how, and helping forge a stimulus bill he believes will get America moving again. The rest of the Congress, they are governing too, in their own way and with their own priorities. But we shouldn't treat this as a game, and we shouldn't say anyone played anything right or wrong. It's not about winning and losing, it's about the our lives.

Tuesday, February 03, 2009

A Bad Day for American Health Care Reform?

Today Sen. Tom Daschle, former Senate Majority Leader from South Dakota, asked President Obama to withdraw his nomination as Secretary of Health and Human Services. I will admit that when Daschle was first nominated, I was very excited. I thought, here was a man who knows the Senate better than most, is a passionate advocate for change in health care, and may be the one guy who can shepherd universal health care through the Congress. I honestly don't know how he would perform as an administrator, but you can solve that with a good Deputy Secretary. But this guy, this guy was going to make the politics happen.

However, I cannot agree with Sen. Kerry's comment following today's announcement.
I wish Tom Daschle had not decided to withdraw his nomination... While Tom’s decision is a reminder of his loyalty to President Obama and his determination not to be a distraction, this was no ordinary appointment and today is not a good day for the cause of health care reform.
With all due respect to Sen. Kerry, today is not the bad day for the cause of health care reform. The bad day was when Sen. Daschle, a man who served on the Finance Committee of the United State Senate (they write the tax code), failed in his basic obligation to pay his taxes. This isn't the sort of difficult to understand tax situation, like with Sec. Geithner, this is an obvious case of either gross negligence or willful evasion.

If you had asked me before last year's tax season, I might have had a different feeling. Last year I fretted extensively about my taxes because so much of my income was as an independent contractor. As I had failed to make quarterly installments, I had a significant tax burden to pay... and I had to save and scrimp for months to come up with the money by April 15. I didn't have to do that -- a lot of what I earned never got reported as 1099 income -- but I reported it anyway because the law is clear. The law was equally clear for Sen. Daschle, as it is for rest American upper class who seems to be engaging in massive and widespread tax fraud. I wonder, how many people making over a million dollars annually in this country would pass through this kind of scrutiny? It sort of make sense, if you think about it... try and skim off as much off your taxes as you can and bet that if you do get caught, you'll get the mess cleared up through lawyers. It's a win-win... unless you are the average tax payer who can't afford tax lawyers and whose only real option is to pay to the best of their ability and pray they aren't audited.

Yes, I too wish Sen. Daschle could have served as the new Secretary of Health and Human Services, but I wish even more that he had shown a basic level of respect to his fellow citizens and the law and shouldered his share of the burden, instead of trying to get away with tax evasion until suddenly it became a political issue.

It's just like John Stewart said on the Daily Show.... "pay your f*cking taxes."