Archive for June, 2010

Iterators? They still make these?

The war between procedural and functional programming continues to rage, and the latest skirmish appears in this article, where the author shows two versions that add the contents of a vector.

Traditional C++

int Dice::total() const {
  int total = 0;
  for(const_iterator current = dice.begin();
      current != dice.end();
      ++current)
    total += (*current)->faceValue();
  return total;
}

Functional C++

int Dice::total() const {
  return std::accumulate(
      dice.begin(),
      dice.end(),
      0,
      bind(std::plus(), _1, bind(&Die::faceValue, _2))
  );
}

The comments show the usual dichotomy between people who prefer one style over the other (I continue to think the procedural style is vastly more readable), but interestingly, nobody seems to notice the one thing that makes both these snippets terrible examples of clean code: they both use iterators.

Iterators were made popular by the Standard Template Library (STL). When it came out (around 1994 I think), the STL featured a lot of concepts that were quite groundbreaking at the time, such as the extreme emphasis on composition over inheritance, something that is still considered a very good practice today.

Unfortunately, the STL also made iterators very popular, and although back then, iterators definitely raised the level of abstraction in the manipulation of containers and algorithms, their main drawback is that they expose a lot more about the internals of the containers than users really need to know.

When the first versions of the Java Collections came out, they enthusiastically embraced iterators as well. We can’t blame the authors, nobody really knew better back then, and this kind of code:

Old Java

List<Integer> l = ...;
int result = 0;
for (Iterator<Integer> it = l.iterator(); it.hasNext(); ) {
  result += it.next();
}

was considered a marvel of abstraction.

It took a few years, but finally, the Java community started wondering why we needed iterators at all for such operations. We have a container, we have elements, and we want to apply a certain operation on these elements. In the description of this problem, iterators are nowhere to be found. This simple realization led to one of the best new features ever introduced in Java : the enhanced for loop.

New Java

List<Integer> l = ...;
int result = 0;
for (int n : l) {
  result += n;
}

The syntax itself is unimportant (some languages use foreach, others for i in l, etc…), what really matters is that iterators are gone.

Which brings us back to the two code snippets shown at the top of this post. What’s more disturbing is not so much that the procedural version of this code uses iterators (this is, sadly, a common idiom in C++) but that the functional version exposes iterators as well. Why the author of this snippet can even remotely think this is a good idea is beyond me.

And the more I think about iterators, the more I really wonder if they have any place in a public API. At all.

For a while, I thought that being able to traverse a collection without exposing the underlying container sounded like a good idea, so instead of returning a List or a Set, you just return an iterator. I could buy that, although I found that in practice, iterators are pretty hard to manipulate, and as soon as you find yourself needing more sophisticated access to the underlying container (such as the size of the sequence, or random access to its elements), you quickly replace your iterator with a collection exposing the richer interface you need.

Let’s just declare iterators a cute idea that no longer has its place in modern software and move on, shall we?

Steps toward a gentler web

In this article, Jim Lynch is going a bit far when trying to assess the effet of the new “Reader” feature of Safari 5, which I touched on in a previous post:

Apple has essentially destroyed the web publishing model completely with the release of Safari 5

Okay, that’s pushing it a bit far.

The first problem with this assessment is that Safari 5’s market share is ridiculously small.

The second problem is that the capabilities offered by Safari 5’s Reader are not new.

First, there’s Arc 90’s Readability, which is what Safari’s feature is based on. This bookmarklet works in most browsers and it has been available for a few months now.

What Readability doesn’t allow is displaying a multi-page article in one page, but as it turns out, there is a trick I’ve been using for several years with a lot of success: look for a “Print” button.

Let me illustrate on the very article I’m quoting. As you can see, it’s pretty overloaded with annoying stuff, starting with the silly background and the ego gadgets at the bottom:

This article is spread over three pages, but hey, look! A “Print” button! Let’s click it:

Tada! The entire article on one convenient page and no more ads nor distractions. And a bonus: no more Tynt either!

You don’t know what Tynt is? It’s a service that monitors your copy/pastes and rewrites them before handing them back to you. For example, if you copy a section of the original article, the text you get when you paste is:

Why are multi-page articles so important? Many web publishers get paid based on the number of ad impressions they generate

Read more: http://jimlynch.com/index.php/2010/06/07/safari-reader-apples-weapon-of-mass-destruction/#ixzz0qnzgfmlr

As you can imagine, I never expected the “Read more” section to be part of my selection and the unique key code at the end of the link helpfully reminds you that Tynt is tracking your every pastes. Tynt is extremely annoying and unfortunately, I see it being used more and more.

The “Print” trick I described above will take care of Tynt as well (as will “Readability”) and once you get into the habit, you will no longer accept to read a multi page article without it.

Arc90’s “Readability” makes the web pleasant again


 

I’ve been a fan of Arc90’s Readability for months, to the point that I find it more valuable than AdBlock to read articles. I was pretty excited to find out that Safari 5 was now offering this feature directly, even though I don’t use Safari.
It turns out that Apple actually embedded Arc90’s code in Safari. They do mention Arc90 in their license but the courteous thing to do would have been to at least let Arc90 know directly.
If you haven’t already, install the Readability bookmarklet on your favorite browser and you will soon wonder how you could ever live without it.

PR disasters

What’s going on in the Public Relationship (PR) world? Over the past few days, PR departments from big companies keep making blunders over blunders. To wits:

AT&T

An AT&T customer displeased with the service decided to send an email to Randall Stephenson, the AT&T CEO. The response: phone calls from the AT&T legal department asking him to stop emailing their CEO or they will send him a cease and desist letter.

AT&T was quick to apologize but what’s most puzzling to me is to imagine the AT&T PR team getting together in a room and all agreeing that threatening a customer was the best way to respond to a couple of emails.

BP

BP is quite unhappy with the hilarious fake Twitter BP account @BPGlobapPR that popped up a few weeks ago and which is now boasting more than 141,000 followers. The owner of the account, who goes under the pseudonym Leroy Stick, manages to pull off the unthinkable: be consistently funny in less than 140 characters.

BP’s PR’s original reaction was to try and incorporate some of Stick’s piques in their own PR communications, which is nothing short of hilarious in itself, but they seem to have realized the errors of their ways and they are now asking Stick to clearly indicate that this is a fake account.

Right, that’s going to work.

Stick’s response: ask his 141,000 followers to help him rebrand the name of the account and come up with creative meanings for the letters “BP”. His followers were happy to oblige, resulting in quite a few sadly funny names (“birds in petrol”, “broken pipes”, etc…).

BP had an environment problem on their hands, now they have to solve a PR disaster as well.

Some of these names are guaranteed to stick (no pun intended… ok, maybe a little) and I wouldn’t be surprised if in a few months from now, BP decides to change its name completely in an attempt to distance themselves from the stigma now associated to their brand.

Microsoft

Steve Ballmer, Microsoft’s CEO, was recently quoted saying “The iPad is just another PC”.

It’s not the first time that Ballmer attempt to minimize the impact of a competing product by dissing it. Maybe it worked a few decades ago, but this kind of message no longer resonates with customers, real and potential. It probably alienates them, actually.

When you are trying to enter a market, you can’t be content with just preaching to the choir: you need to do your best to acquire new customers and, even more importantly, steal customers away from the competing product. And insulting their choice is certainly a poor way of achieving this result.

If I had been asked the same question, here is what I would have answered:

The iPad is a great device, there is no denying this, but as a first generation product, it also suffers from quite a few limitations. In the next few months, you will be seeing a lot of new Windows based tablets that will make the iPad look like a first generation iPod.

Here is why I think this kind of passive aggressive message is much more effective than Ballmer’s bashing approach:

The iPad is a great device.

There’s nothing wrong with acknowledging a competing product’s success: it will make current customers feel good about their choice and you will get their attention. One gotcha, though: always make sure that you accompany such a statement with one that will show your own product in an even better light.

but as a first generation product

A minor jab, but also a fact that nobody can deny, not even Apple advocates. The iPad is the first of its kind, both because it’s the first tablet that Apple released but also because it’s the first tablet that’s actually a commercial success.

it also suffers from quite a few limitations

Another minor jab rooted in facts. Again, you want the audience that you are trying to acquire to nod. They might love their iPad, but they are also aware that it doesn’t have a USB port, no camera, no wifi tethering and a Bluetooth profile that’s missing network connectivity.

In the next few months, you will be seeing a lot of new Windows based tablets

Done with discussing the competition, time to promote your own product now. No need to give a precise timeline but make sure to hint that these releases will happen “soon” in order to create some hesitation in customers that are on the verge of buying an iPad.

that will make the iPad look like a first generation iPod.

Maybe a negative comparison to a product that doesn’t belong to your competitors would be more appropriate here, but this one will do for now. The idea is to remind your listeners that this field is evolving extremely fast and that in this early adopter phase, waiting a few months before making a choice might be a wise move.

All of these observations sound like common sense to me, but then again, maybe there are more subtle forces at work and I’m just being naive.

Links #7

Tags: