This blog has moved to Medium

Subscribe via email


Archive for December 2011

Sometimes a little sleep is ok

Thread.sleep() has always been considered a “bad thing” in programming, something you do when you don’t have a good alternative. You could sleep() when you’re polling for a long operation, but the better solution would always be to get a notification when the operation completes, to avoid blocking a thread and generally wasting time – if the operation finished in 600 milliseconds, and you poll only every second, then you completely wasted 400 milliseconds, right?

Well, it turns out there is at least one use case where you really should waste those extra 400 milliseconds – User Interface.

I recently implemented a web UI component that checks if another website is reachable, and only then proceeds. If the website is not reachable, the user has to stay on that page and try again (perhaps he mistyped the URL). I implemented a server method that checks if the URL is alive, and called it from the client side via AJAX. The code looks something like this:

showLoadingIcon(); // displays a "loading" GIF
$.post(url, function(result) {
  hideLoadingIcon();
  if (result.good) {
    // proceed
  } else {
    // Display error message
  }
});

Well, it turns out that while this code is perfectly correct and efficient, it feels erratic from a usability perspective. If the page took a long time to access, it would behave fine, but if the page was very quick, and the method returned within say 200 milliseconds, the “loading” icon would flicker, creating an annoying experience for the user.

The solution is adding a manual, “artificial” sleep(). If the loading was long, no sleep was necessary, but if it was too short, I added a call to setTimeout, to ensure the loading image is always spinning for at least a full second.

While this is not really an example of Thread.sleep() (that doesn’t really have a javascript equivalent), this does show that sometimes delaying an interaction is required to reduce the user’s pain or confusion.

On SOPA

In case you have been living in a hole and haven’t heard about SOPA, I’ll just explain it briefly – it’s a proposed US bill that if passed, would literally destroy the internet as we know it. This bill includes massive punishment for anyone who hosts or streams copyrighted material, or even merely links to such sites (Go TV Links!).

Even sites that host or index user generated content such as Facebook, Wikipedia, WordPress or Google are not except. What’s especially outrages is that no court order is required for liability – if anyone doesn’t like a certain blog post that contains links to copyrighted material, and they file a complaint, if the owner does not remove the offending content, then he is liable, as well as a vast array of internet infrastructure that “assist him in his activity” – Internet Service Providers are expected to block his domain name, payment networks such as Paypal are expected to refuse to work with him. It’s a literal thought police, and it could happen.

Luckily, a lot of companies have stepped up and announced their objections to SOPA. If passed, SOPA could destroy those companies, but I believe that a major part of their objections is also on moral grounds. SOPA is pure evil, a revert to darker times.

Today, the U.S government still controls the internet. The major internet user-facing business are based on U.S soil, as well as payment processors and ISPs. This will not be the situation forever. Bitcoin is proving to be an alternative to the U.S dollar and Paypal, that no body can shut down or control. Namecoin can be a fully distributed alternative to DNS and SSL Certificates. These technologies are not yet mature, but the very idea of SOPA being proposed in the so called “land of the free” clearly shows the necessity of disentangling the internet from U.S control.

The internet is our home. It’s the force that binds us together, and we will not let it be corrupted and controlled.