If you've been Web programming for a while, you may be used to these problems. However, there are
alternatives. While it's beyond the scope of this article to consider all the possible solutions (see the related resources), this article covers one possibilitysomething I call "active links."
An active link is a kind of function call that has been encoded as a Web link on a Web page. When the user clicks on it, the function is called. It's as simple as that.
Here is how I'd like an active link to lookagain, in Hypothetical Web Language:
$link = activeLink( processOrder( $account, $method ) );
<a href="$link">Click to process order!</a>
In the above example, you're not calling processOrder()
. Instead, you're freezing a call to processOrder()
and stuffing it in a Web link. When the user clicks on that link, the order is processed.
Most commercial languages use convenient syntax like this. Programmers have to juggle things around a little bit to get it to work. But the syntax above is the idealafter all, succinctness is power.
|Figure 1. The User Interface of the Mult Program: Clicking on a link will give you the product of the two numbers.|
The Example Application
In order to focus on programming-language issues, the example application is very simpleit's a multiplication table, called Mult. Figure 1 shows the user interface.
If you click on any of the links, you get the answer:
|Figure 2. Click the Links: The result of clicking on a multiplication link in the Mult program.|
In each language, the implementation consists of three parts:
- The multiplication routine, called mult(), which takes two integers and returns the answer.
- The main page, which renders the multiplication table filled with active links.
- The infrastructure, which "freezes" a call to mult(), and then "thaws" it when the user clicks on a link.
The idea here is to make the first two parts very convenient, because they comprise the bulk of all Web programming. The third part involves a bit of trickery, but that's okayyou only have to do it once. In a sense, you're trying to take the messiness described above in the shopping-cart example and remove it from the day-to-day code you have to write. To do this, it must be hidden in the infrastructure.
The following sections demonstrate how this is implemented in the four languages.