continuations

Do we really want to use state machines to model application flow?

October 9, 2003 18:57:37.449

Daniel von Fange replies to my post on Ptth! with this snippet of PHP code:

$amount = $_REQUEST['amount'];
$rate = $_REQUEST['rate'];

if( $amount != "" && $rate !=""){
    if(! $amount > 0){
        $t->assign("message", "Amount must be greater than zero");
        $t->display("interestForm.html");
    }else if(! $rate > 0){
        $t->assign("message", "Rate must be greater than zero");
        $t->display("interestForm.html");
    }else{
        $result = $amount * rate;
        $t->assign("result", $result);
        $t->display("interestResult.html");
    }
}else{
    $t->display("interestForm.html")
}
At least with an example this simple, he says, continuations don't have much of an advantage.

Not surprisingly, I disagree. Looking at that piece of code, can anybody tell me:

  • What's the first page the user sees? How obvious is this?
  • What's the order of pages the user sees after that? If this isn't fixed, what determines the order?
  • How would I change the order if I wanted to? If say, I decided I wanted to ask for amount first, and then rate, how many different pieces of code (and templates!) would I have to change?
  • Say I had to ask for 10 different variables instead of 2. How would I abstract the validation code so that I could reuse it easily?
These questions all have trivial answers when you're using a continuation-based system. They're a lot more difficult (especially the last two) when you're designing your program as a state machine - which is effectively what Daniel's code, and much code like it, does. With all due respect: this is spaghetti code, and in any context but web development you wouldn't stand for it either.

Comments

Oh.

[Avi] October 9, 2003 19:12:52.527

Actually, having looked at the code more closely, I see that he's cheating a bit - there's one form with both rate and amount on it, which is different from the Ptth exercise and makes some of my questions irrelevant. However, the fact that I couldn't *tell* that was what was going on proves my point in a different way...

Continuations for web services

[David Buck] October 9, 2003 22:08:42.308

I find the idea of continuations for web services very intriguing, but I'm having a hard time grokking how certain situations can be handled. For example, what happens if the user clicks the back arrow and returns to a previous page? What happens if they bookmark a page in the middle of the flow and try returing to it later?

Does this approach require the HTML to be written in code or can you handle JSP style pages that separate the HTML from the code? It also strikes me that if this approach works well for HTML, it should also work well for stand-alone GUI's. I'll have to think about it some more and play with Seaside.

On the other hand ...

[Shane King] October 9, 2003 22:23:57.707

The WinForms framework in .NET does the same thing, but in a more OO way, using event callbacks rather than continuations to hide the fact you're doing a roundtrip to the client. They're both valid approaches to the very real problem that by default, you have to manage your own program state when doing web development.

Continuations and Callbacks

[Avi] October 10, 2003 4:25:40.248

Shane, continuations and callbacks are orthogonal. Seaside uses callbacks for forms, links, etc (actually, I've been meaning to write a long blog on the exact mechanics of that, since I think the details are generally useful). It's probably similar to WinForms in this. But it uses continuations for application flow, and AFAIK there's nothing equivalent in .NET.

Actually, I would say that there are three orthogonal issues here:

  • managing input (events, callbacks, whatever)
  • managing state (back button, sessions, etc)
  • managing flow (continuations vs. state machines)
I've been focusing on continuations because I think the biggest and most interesting gains come from those, but in terms of actual code and design work Seaside is much more about the other two.

Back Buttons

[Avi] October 10, 2003 4:31:08.957

David - as a quick answer to your question about the back button and bookmarking - they Just Work. Honest. As does opening multiple browser windows on the same session. A longer answer will hopefully be forthcoming.

As for templates vs. code gen, yeah, there's nothing stopping templates from being used with Seaside or tools like it - there's at least one system ("Nori") that provides templates for Seaside. I personally don't like them (that's a whole other long blog posting...).

Trackback

[BTW] October 10, 2003 15:48:00.808

Trackback link seems broken...

Maybe

[Daniel Von Fange] October 10, 2003 15:51:07.298

Maybe I should get the stupid prize for somehow using the name field as second subject field in the second post. :)

Err

[Daniel Von Fange] October 10, 2003 15:52:32.550

Replace "second post" with "previous post". :)