rc3.org

Strong opinions, weakly held

Month: July 2006 (page 1 of 2)

Nested loops, or when to write a method

I was trying to fix a bug a few days ago and my initial fix involved nested loops. The code pulled some random values out of a list and added them to another list (creating wrong answers for a multiple choice question). I of course forgot to check for duplicate values, introducing a bug. The humbling thing about software development is that I still routinely make mistakes like this after ten years of getting paid to write software.

Anyway, I went to fix the bug, and so I just put another loop inside the loop that added the answers to the list which made sure that the value I picked wasn’t already assigned to an answer I had added. If the value was already used, I just did this:

continue;

Guess whose fix didn’t work? This statement was applied to the inner loop that checked for duplicates rather than the outer loop that added answers to the list and the duplicate answer kept getting added as before. The next logical step was to use a label, so I could label the outer loop with something helpful like outer and then change the statement above to:

continue outer;

This would work, but I don’t think it would add much in the way of clarity. Instead I added a method called hasAnswerWithValue() to my Question class and replaced the inner loop with:

if (question.hasAnswerWithValue(newAnswer)) { continue; }

I may even use the new method somewhere else at some point. In some cases, there’s no way to avoid the use of nested loops, but I’ve come to think that you should always do so whenever it’s practical.

In fact, I’m coming to think that “minimize the number of loops and conditional statements” is a good principle for programmers. That’s another post though, because that’s not what I did in this case. I just hid the loop in a method instead of nesting it.

Managing Rails migrations

I’ve mentioned Rails migrations in the past. They provide a way to script database schema changes so that you can keep them in version control and roll back versions of a schema if necessary. One thing about them though is that eventually your list of migrations gets huge. (We’re up to over 20 of them in some of our applications, and there’s no reason why we might not have over 100 some day.)

The question becomes, when do you delete all of your migrations and start over with a fresh schema? It seems to me that eventually it comes time to create a fresh database, run all of your migrations on it, and then export that database to a new file and then delete all of your migrations and call that new file migration #1.

My feeling is that this is a function of application versioning. When you reach a big milestone (the kind you wouldn’t want to roll back from incrementally), it’s time to bundle up all your migrations. Unfortunately, that seems like it would conflict with maintaining an existing application. The schema version is stored in the database for live applications, and if you reset the number you lose the ability to run subsequent migrations against an existing database.

Anyone know if there’s a plan for how to handle this sort of thing in the Rails world that I’m missing? The reason I ask is that we have applications that we’re planning on releasing as open source. They also have live instances that are currently running. For the sake of those applications, it seems logical to never roll up the migrations. Distributing an open source application that has to run 50 migrations to get the database set up seems like a bad plan, though. You could distribute your application with a schema import file, but then when you release subsequent versions, you lose the ability to distribute a migration with them that automatically updates the user’s database.

I guess I need to do some research.

Tim Bray on Ruby

Tim Bray says something in his post on Ruby that I have observed as well:

For people like me, who are proficient in Perl and Java, Ruby is remarkably, perhaps irresistibly, attractive.

If you know Java and Perl, you can start using Ruby pretty much immediately.

His impression of the Pickaxe book is the opposite of mine. He thinks it’s above average. For me, not so much.

He misses his IDE. I find that I miss it less than I thought I would, but then again, I use TextMate. Tim will be giving in to TextMate pretty soon, I’d guess.

Akismet

One of the commenters on this post suggested trying Akismet for spam filtering. Installing the Movable Type plugin and signing up for an API key took about 5 minutes, so we’ll see how that works out.

Adding a simple keyword filter seems to have gotten rid of my most persistent comment spammer, so I feel dumb for not having investigated the spam prevention measures built into Movable Type a lot sooner.

Spam-proofing my Movable Type installation

For the past week, I’ve been getting more comment and trackback spam than ever, so this weekend one of my projects was to do what I can to slow down the spammers. I started out with the Six Apart Guide to Combatting Comment Spam, and the Movable Type manual. (The guide is a bit behind the times so you have to refer to the manual as well.)

In my first round of addressing the comment spam issue, I’m trying to take measures that won’t affect users. The first step is renaming the trackback and comment scripts. I doubt it will help but it certainly won’t hurt anything.

I also discovered that I wasn’t using the SpamLookup features built into Movable Type 3.2 very well. There are ways to block keywords, ways to subscribe to IP and domain name blacklists, and you can configure how the link filter works as well. Hopefully these filters can be configured to take care of my problems. I’m starting out by adding nicknames used by some of the most persistent comment spammers. Hopefully it filters on nicknames as well as comment content.

There are a few things I’m still trying to figure out:

  • Is there a good list somewhere of other blacklists you can subscribe to?
  • Is there an easy way to ban an IP address?
  • Is there a Bayesian filter for Movable Type that works like the one in Thunderbird?

I’m also tempted to start looking at writing Movable Type plugins. BigPAPI is built into Movable Type 3.3, and there are some cool things you could do to make spam handling easier.

Stability versus transformation

From what I read, the Bush administration really does have a philosophy when it comes to foreign policy. You’d think it was simply, “always make the wrong choice” or “choose violence,” but there’s a method behind the madness (and it is madness). That philosophy is that temporary stability is not nearly as valuable as transformative change. So pushing for an immediate cease fire in Lebanon would be wrong because what’s required is transformative change in the form of removing Hezbollah from the picture. All of the death and destruction going on right now are worth it because they lead toward a better future when things are not just peaceful, but peaceful for the long term.

That was the philosophy with Iraq, too. Saddam Hussein was a brutal autocrat with a track record of human rights violations and war crimes a mile long, but Iraq was basically stable. Sure, he wasn’t a threat to us but he someday could be, and so he had to be removed from the picture. Besides, getting rid of Saddam was the first step toward bringing about transformative change throughout the Middle East. Forget trying to keep things stable in the present, better to turn the whole place over and bring about dramatic, lasting change.

At this point, I find myself in the opposite camp. I’m not in favor of coddling leaders like Saddam Hussein or armed groups of thugs like Hezbollah (or, for that matter, the IDF when it is leveling parts of the occupied territories or bombing Lebanon). What I am in favor of is doing whatever it takes to make the peace today, and if we can make the peace today, then do what we can to make peace tomorrow. An imperfect peace right now is more likely to lead to a lasting peace in the future than war now in hopes of a lasting peace later. I just don’t believe that works.

The people with guns and the willingness to use them thrive on violence. That’s when they’re in their element. When Israel is raining bombs down on Lebanon, who are the Lebanese going to turn to? Probably the people willing to fight back, not the people trying to make peace. We’ve certainly seen that in Iraq. The groups who have gained the most are those that are willing to fight, whether it be against the Americans or against whatever internal faction aggravates them the most. In a climate of violence, the people who just want to get through their day without being shot or blown up are those that suffer the most. You bring long term stability out of short term stability, not out of chaos.

Clearly Israel couldn’t stand by and let Hezbollah lead an armed incursion across their border and kill and capture its troops. Israel couldn’t let militants in the Gaza Strip hold an Israeli hostage either. But perhaps the powers that be should have asked how to accomplish their goals using the least force required rather than the most? Things could have been different.

A survey of the conflict in the Middle East

Courtesy of Billmon:

Let’s see. We’ve got: Israeli Jews fighting Lebanese Shi’a and Palestinian Sunnis; Palestinian Fatah militants who’ve stopped fighting Hamas militants, but only because they’re both fighting the Israelis; Saudi Sunni fundamentalists issuing fatwas against Hezbollah Shi’a fundamentalists; Egyptian Sunni fundamentalists backing those same Hezbollah Shi’a fundamentalists; Iraqi Sunnis killing Iraqi Shi’a and vice versa; Iraqi Shi’a (the Mahdi Army) jousting with Iraqi Shi’a (the Badr Brigade); Iraqi Kurds trying to push Sunni Arabs and both Sunni and Shi’a Turkomen out of Kirkuk; Turks threatening to invade Kurdistan; Iranians allegedly shelling Kurdistan, Syrian Kurds rebelling against Syrian Allawites who are despised by Syria’s Sunni majority but allied with the Lebanese Shi’a who are hated and feared by the House of Saud and its Sunni fundamentalist minions. Oh, and American and Israeli neocons threatening to bomb both Syria and Iran.

This sort of relates to something I’ve been wondering about. I see a lot of people speak with confidence about who’s ordered Hezbollah’s attacks on Isreael, but they all seem to be saying different things. Some people say Hezbollah is acting on its own, others claim they attacked under orders from Syria, and still others hold the Iranians responsible. Does anybody really know the answer? I think that the assertions most people make reflect their wishful thinking more than any special knowledge of Hezbollah’s foreign policy.

Unlocked cell phones

You may know that I’ve been eager to buy a new cell phone for awhile now. My phone is now pushing three years old, and I’ve been out from under my contract with Cingular since December, 2005. The thing that has kept me from buying a new phone is Cingular’s lack of interesting new models in the lineup. The phones I’m most interested are the upgraded Motorola RAZR (V3i) and Motorola PEBL. Cingular still offers the old RAZR V3, and only T-Mobile offers the PEBL. I can’t get service from T-Mobile where I live.

I have thought in the past about just buying the phone I want online and putting my SIM card in it. The phones cost more without the subsidy providers furnish for signing up for an onerous contract, but you can get any phone you like and you’re not stuck with a two year contract. The only question then is the size of the subsidy and how much you trust the people who sell you these phones online.

I’m feeling a bit emboldened today because I just read about Laura Lemay’s successful purchase of an unlocked Nokia E70 from an overseas seller on eBay. A bit of research reveals that you can get a Motorola PEBL on eBay for about $180. (Here’s an example. A new unlocked PEBL for $179.99 from a seller with about 10,000 feedbacks who has been an eBay member since 2001.) I was surprised to read then that a PEBL will cost you $150 from T-Mobile, and that’s with a contract.

The final bit of research is on how much unlocked phones cost elsewhere. A seller on Amazon.com offers them for $270. An advertiser on Google sells black PEBLs for as low as $209. Is there a catch? I’m certainly tempted to go the eBay route and see what happens.

Update: I went ahead and ordered a Motorola PEBL. We’ll see how it works out.

The essence of wisdom

According to Bob Sutton, the essence of wisdom is “strong opinions, weakly held.” I feel a bit gratified by that because I was just explaining to a coworker the other day that I will fight hard for the things I believe, but if you can convince me that I’m wrong, I’m perfectly willing to fight hard for the things you believe. I didn’t know that made me wise, but I’m glad to hear that it does. I think that if anyone really looks hard at the number of times that they’ve been wrong about something they really strongly believed, they would not have high confidence in the things they believe at the moment. Everybody seems to be wrong at least half the time.

(via Kottke)

Has Digg jumped the shark already?

I’ve been following the Digg feed for a few months to keep up with the big news in the tech world. If something important happens, it usually gets Digged, and plenty of other interesting, more esoteric links make it to the top as well. Unfortunately, the site seems to be turning to crap. It seems like more and more right wing and left wing propaganda get Digged enough to make it into the feed lately, even though such content is completely out of bounds for the site. The larger point is that once anything becomes popular enough, the incentives to game the system grow to the point where the gamers basically ruin it for everybody else.

What you wind up with is either total chaos or mass homogenization. Digg seems to be heading toward the “total chaos” phase. It seems like every Web site is sprouting “Digg this” buttons these days, even if the sites don’t cover topics that are relevant to Digg. I’m sure that if you scoured message boards you’d find people exhorting others to go to Digg in droves and add their votes to the burning topic du jour.

Bottom line, I don’t think I’ll be a Digg reader for much longer.

Update: I just read that Jason Calcanis is offering cash to Digg’s most active users to get them to post news at Netscape.com instead. I have many bad things to say about this, but no time to say them.

Older posts

© 2024 rc3.org

Theme by Anders NorenUp ↑