New News

While at the Over The Air conference just over a week ago, I got talking to a couple of people who were publicising the NHS web services (yes, they have web services). Anyway, I signed up for an account and decided to play around with the news API by building a very simple Mobile Safari application using Apple’s Dashcode tool.

If you have an iPhone or iPod Touch you can view it in Safari here:

There were a couple of things I wanted to do – such as adding a topic filter – but due to the fact that Dashcode is rather annoying, I haven’t been able to figure out how to do want I wanted to do …. so I didn’t. It’s just a proof of concept anyway, although I’m not entirely sure what concept I was trying to prove.

It wasn’t as straight forward as it looks though. Due to the fact that XML HTTP requests are restricted to the domain that the page was initially loaded from, I had to write a proxy (in Java) to proxy the request through to the NHS web service – I had to do this anyway because you have to pass your username and password to the NHS web service(s) as URL parameters (very secure!) and I didn’t want to put them directly into Javascript for obvious reasons!

Anyway, as soon as I recover from my first encounter with Dashcode I intend to play around with some of the new HTML 5 features that Safari supports such as client side storage etc, and further explore the NHS web services. Until then you have no excuse for not finding out when the next wave of pig flu is going to bring the nation to its knees!

Sieving Numbers

Here’s a problem that you some times come across in problem sets and job interviews:  

Q. Write a program to generate all the prime numbers up to N.

The simplest algorithm I can think of is the Sieve of Eratosthenes.
Here’s my attempt:

public class PrimeSieve {

     public static void main(String[] args) {		
	int N = Integer.parseInt(args[0]);
	boolean[] isPrime = new boolean[N+1];
	Arrays.fill(isPrime, true);
	int max = (int)Math.sqrt(N);
	for (int i=2; i<=max; i++) {
	   if (isPrime[i]) {
	      // Remove all of the multiples of i
	      for (int j=2; i*j<=N; j++) {
		 isPrime[i*j] = false;

	// List the primes
	for (int k=2; k<=N; k++) {
	    if (isPrime[k]) System.out.print(k+" ");

That's all. You can leave now.

Mobile Mentality

During the course of last week I have been getting up to speed on the iPhone SDK; yes, I did eventually get around to buying a new MacBook Pro! The more I learn, the more doubts I have about developing for the iPhone. Nothing to do with the technology – although Objective-C is an interesting extension to C – but everything to do with the (closed) platform, app store etc. For example, I discovered that in order to test my application on a real device I need to stump up $99 to get onto the developer program; the SDK only has an iPhone simulator. I have highlighted other concerns that I have in previous posts that become more real when you read posts such as this one.

What particularly irks me is the fact that the price of apps are perpetually being driven down to zero! Sure, there have been some hits but these are exceptions to the rule; the rule being: you aren’t going to make any money off your app! If you are a developer (or a company) trying to make a living out of this and you sell your app for 59p, you work out how many copies of it you are going to have to sell in order to make a living off of it. The numbers don’t add up!

I came across a good example of this over the weekend. There is a website called Breaking News Online and they have an iPhone app that they charge $2 for and there is a recurring $1 a month subscription to receive breaking news via push notifications. You can read about it on ReadWriteWeb here. Anyway, if you look at the comments, some people are bitching about having to fork out $1 a month! What? “Is anything worth that?”, they cry! $1 a month buys you half a latte from Starbucks (depending on the size of course) yet even this seems to be too much for some people! The bottom line is this: if you are in the business of just selling applications for the iPhone then you are soon going to be going out of business! You can blame cheap users for that.

I see two primary ways of making money from mobile applications: you can offer a mobile version that supplements whatever it is you happen to be selling, e.g. Salesforce does CRM and they have an iPhone app but it’s not their main line of business; or you can write mobile applications for other companies. There are a number of apps that I have come across recently that have inline ads but your app really needs to become popular before you can start making any serious money from it.

Myself, I am focusing on both approaches. I have done Cocoa development in the past (on the desktop) so focusing on mobile right now is just another string to add to my bow. I hope to have something in the app store during the course of the next couple of weeks months years … assuming of course that it doesn’t get blocked. We’ll see!

Finally, if you do want an iPhone app building then let me know. Just don’t ask me to try and sell it for you!

Opportunity Knocks

What follows is a brief list (without much of an explanation I might add) of what I perceive to be gaps in the market. Opportunities if you prefer. Areas where the current state of the art sucks! Not good enough. Please fix. You’ll be making my life much easier, as well as countless others. You’ll probably make a few billion on the side too!

  1. Create a search engine that works – Yahoo, Google, Bing etc. Err, no. I’m not suggesting it’s an easy problem to solve (it isn’t) but with some of the biggest companies in the search game you would think they’d be able to come up with something that didn’t involve me still having to type in hundreds of different combinations of words in the hope of finding what I’m looking for (and still not finding it!). I find myself having to do this all the time.
  2. Develop a decent desktop mail client – I currently use the Mail application on Mac OS X. It’s woeful! I can’t tag individual messages so they are easy to refer back to and I still can’t figure out how to mark all items (in the RSS feed reader) as being read. Windows users’ have Outlook. Enough said. And the response, “But I use Gmail all the time …”, is not a valid one! I want something that works on my desktop.
  3. System for delivering targeted adverts – Firstly, I very rarely click on adverts. Period. Some people do, judging by the amount of revenue Google makes! Anyway, I have lost track of the number of times I have come across adverts on sites that have nothing to do at all with what the site is about. I once saw an advert for Crucial memory on some fashion site! It has been shown that if you have adverts that are relevant to the user, click-through rates are much higher. Now Google attempts to do this AdSense by looking at keywords in the page content but it doesn’t work well in my opinion. I used to have Google Ads on this blog but it kept showing adverts for belt buckles! I don’t tend to write about belt buckles much on this blog so not really relevant.

Anyway, these are just a few ideas to get you started. Now go forth and conquer and let me know when you’re done.

Flushing The Document Early

This post is a note to myself and regards the Transfer-Encoding header field, defined in the HTTP spec. I was reminded of its use while reading Even Faster Web Sites.

First, some assumptions for the example that follows. Let’s say that the header of your page contains some annoying Flash banner advert that is downloaded from a different host and that the body of the page takes a few seconds to generate – in the example below I suspend the current thread for 10 seconds.

The page will be generated on the server and then served back to the browser. The browser will then parse the HTML and proceed to fetch the banner ad etc. In the meantime the user will be sitting there wondering what, if anything, is going on! So how can we present a more user-friendly page? Something that feels more responsive to the user. Enter the Transfer-Encoding header. By setting it to chunked we can serve the header part of the page – the first chunk – to the browser while the server works on generating the body of the page; in other words we don’t need to generate the page all in one go and then serve it up. The Transfer-Encoding header informs the browser that the content for the current page is going to come down the pipe in pieces (“chunks”) and not all at once. It also has the added benefit that as soon as the browser retrieves the first part of the page (the “header”) it can start to download the banner ad in parallel* while it waits for the remaining part of the page from the server. Overall the page should feel more responsive.

So, how to do it in a Java servlet. You would think you would just call the setHeader method on the servlet response object but you don’t – what were you thinking? Turns out it’s even easier than that! An example is given below:

void doGet(HttpServletRequest request,
           HttpServletResponse response)
   throws ServletException, IOException {

	PrintWriter out = response.getWriter();
	out.println("The start of the page.");
	// Wait 10 seconds for no reason whatsoever
	try {
	} catch (InterruptedException e) {
	     // Do nothing
	out.println("The rest of the page ...");

Basically, if you write something to the output buffer and then call flush() that will automatically set the Transfer-Encoding header for you. If you remove the call to flush() then all the output will be buffered before it is sent back to the browser. If you fire up the example code in Tomcat (or something) and then look at it in a browser, the first line will be returned immediately followed 10 seconds later by the rest of the page; if you remove the call to flush() then the page content will be returned all at the same time after about 10 seconds or so.

End of note.

(*) Most browsers open up a limited number of connections to a given host. For example, Firefox 3 opens up a maximum of 6 connections at any one time to a given host.

URL Shorteners

Yes, another post about url shorteners. Recently, apart from complaining about them, I have been thinking about how URL shortening services work; services such as and tinyurl.

Many of these services reduce a URL to a small string; typically a length of 3 to 6 characters. As a result, they can’t be simply hashing the URL. For example, if you use MD5 to hash the domain name of this site you get (in hexadecimal): 4302e8ae08795f0c67c932338f516e2f. The resulting hash value is longer than the URL itself! Not very useful for a URL shortening service.

So how do they work? I still don’t know but here’s one approach that I took:

Let’s say we want to produce a code using characters from the following alphabet [a-zA-Z0-9]; that gives a total of 62 different alphanumeric characters. For a 5 character code there are 62*62*62*62*62 (=916,132,832) possible combinations. If we associate each code with a given URL – a simple one to one mapping – then that’s a lot of URLs! The key point is that, unlike a hash function, I don’t think the URL is used as input to determine what comes out of the other end; a “random” character code is generated and then is just stored with the URL so it can be retrieved with a simple table look-up.

I came up with a probabilistic approach to generating these “hash” codes. I say probabilistic as it just generates a code at random. If there is a collision, it just tries again and generates another one. So how likely are collisions to occur? Well according to the birthday paradox we should expect to see a collision after generating 2n/2 items, or approx. every 215 items for a 5 character code using the example code, assuming, of course, that all generated codes are equally likely to occur. It’s an example, it will do!

You can look at the source code here. In the example I use a bit vector to record what codes have already been generated; the bit vector is limited to representing 231-1 different values therefore the example code is restricted to generating a maximum of 5 character codes; each character requires 6 bits. I’ll let you do the math as I have already done it 🙂

If you do run it you may have to increase the maximum heap size, e.g. -Xmx256m. I ran out of the heap space the first time I ran it using Eclipse!

It’s a first attempt so there is likely some room for improvement but it’s a start. Would be great to hear any alternative thoughts on how these things work.

Alternative App Store?

I am about to embark* on developing an application for the iPhone, of which I am going to sell thousands of copies and then retire to the Caribbean (just like all iPhone apps right?) but I have a few concerns, primarily with Apple’s app store policy about what qualifies (and disqualifies) an application from being sold – or given away – on the app store. Now I don’t know much about Apple’s policy but I have followed the various discussions about it in the “media” so I know about things such as how if your application is deemed to compete in some way with Apple then your app will be rejected etc but it doesn’t help when I keep reading articles like this. From a business perspective I don’t want to spend months developing something only to have Apple turn around and reject it!

This neatly segways into my next point: Why isn’t there an independent store selling applications for the iPhone? A place where all the misfit applications rejected by Apple can be sold on – think of it as a council estate for mobile phone apps. Maybe there is one; I have no idea. I can see why developers would want to sell their apps through the App store as it’s baked right into iTunes and it’s easy to pay for and put on your iPhone etc but surely there must be some other way to manage applications? I guess I’ll find out soon enough but if you have any useful advice in the meantime, please leave a comment.

* when I say “about to embark” that’s actually dependent on dragging myself away from my keyboard and down to the Apple store to buy myself a new Macbook Pro; mine is getting a bit long in the tooth. As they have just released the latest versions and lowered the price I guess I don’t have any excuse not to get one.

The Trouble With URL Shorteners

I have just finished reading this post about URL shortening services and it got me thinking.

I use URL shorteners on the odd occasion but I have a problem with them. Answer the following simple question: What is the destination of the following links (and no peeking by clicking on them first):


Hopefully this highlights the problem: You don’t know where you are going to when you click on the links provided by these URL shortening services! This seems to me to be an area ripe for Internet scams (especially if you use Internet Explorer); I am thinking links to porn sites, links that download the latest malware on to your PC etc; there are endless possibilities!

What I would like to see is some kludge so that when you hover over one of these shortened URLs you can see the destination of the link. Sure, not all URLs indicate what exactly awaits you at the other end of it, and, in the case of Twitter, if it is someone that you are following then you can be fairly confident that they aren’t going to send you somewhere you really don’t want to go (or maybe you do). Still, there is definitely room for improvement.

New Host

By now you should be reading this on my new host, assuming the DNS changes have propagated. I finally got around to moving this blog from Joyent – the crap and (relatively) expensive hosting company, formerly known as TextDrive – to GoDaddy. It’s even running the latest version of bloatware for blogging!

I had been meaning to migrate for a while as I had been having problems with uptime, among other things. Ultimately, I had no choice but to migrate as a few weeks ago I tried to login using the password I always use but for some reason it was complaining about an incorrect password blah blah (and no, I wasn’t using the wrong password). Anyway, I reset my password and used the one WordPress had generated for me but that too, for some still-unknown reason, wouldn’t work either. I was locked out of my own blog! What to do. Well, after some careful exporting of some database tables but not others, I now have a working blog again hosted elsewhere for half the cost! Migration was fairly painless, apart from the database munging. Was impressed with Disqus; migrating the comments just worked!

When I first started this site I decided that it was imperative that I have access to all of my data. I could have chosen some free-to-use blogging service but many of these services don’t give you access to your data; well, they certainly didn’t at the time anyway. It’s just as well I did otherwise I could well have ended up permanently locked out of my own site!

Finally, decent customer service

It’s been a long time coming but I believe I have finally found a company that knows how to do customer service properly! Yes, hard to believe I know, but it’s true!

About 4 months ago I signed up with Slicehost. I was paying a small fortune every month for the dedicated server I had with another hosting company, which I wasn’t using, so I decided to downgrade and give Slicehost a try. About 2 minutes after signing up – and I’m not exaggerating – I had access to my (virtual) server. Anyway, over the course of the last few months I hadn’t been using the server much (again!) so I decided to cancel it. My billing cycle was at the end of each month so I had just paid for the whole of May at the end of April. As I was cancelling now I figured there goes my money – other companies I have used before “delete” your server at the end of the billing cycle so you are effectively paying for server even though you are not using it. Anyway, I deleted the server and then suddenly noticed that my account had been credited (prorated) with an amount equal to the remaining amount that I would have paid had I kept the server. Amazing!! I’ll have to wait and see if my credit card gets recharged at the end of the month.

Contrast this with the experience I had a few weeks ago with the online learning site Busuu. Aside from the interesting name, their customer service leaves a lot to do desired. Let me explain. I wanted to see what the site was like so I paid for the premium service when I signed up. On the subscription page, under the basic terms, it claims that you can cancel at any time – you have to be registered to view the page so you will have to take my word for it. There is a link to the terms and conditions at the bottom of the page. My thinking was that I would try it for a month then cancel as, apparently, I could cancel at any time.

Anyway, a few days before my subscription was due to run out (at the beginning of April) I cancelled it. I then got an email saying that my subscription had been cancelled and would expire on the 5th May. “Strange”, I thought. I then got an email a few days later telling me that my credit card had been charged (again) for the next month. Given that I distinctly remember cancelling the subscription days before I was a bit surprised so I sent an email to the accounts department kindly asking them to give me my money back as I had cancelled the subscription. I was then told that you have to give a minimum of 7 days notice to cancel premium subscriptions and if I looked at the terms and conditions it clearly states so. Oh! You mean the terms and conditions that nobody reads? Now I’m not an English professor but this definitely contradicts the basic terms on the subscription page that states that “you may cancel your subscription at any time”.

Fair enough, I admit I didn’t read the terms and conditions but then who does? Hands up all of those who read the terms and conditions of all the web sites they sign up! Needless to say I didn’t get my money back and I haven’t been back to the site since nor do I intend to use it ever again … and all for the sake of 12.99 euros!

As for Slicehost I may not be using their servers at the moment but I fully intend to use them in the future when the need arises. And all because of decent of customer service. Others take note!