Avi Bryant is big on using continuations for implementing web applications. (He's working on a framework to do this with Squeak.)
Avi postulated the other day that a well designed app using continuations is much easier than imperative programming. Today, an enterprising PHP programmmer tried to disprove Avi's postulate by using a simple state machine:
$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") }To which Avi responded:
With all due respect: this is spaghetti code, and in any context but web development you wouldn't stand for it either.Yep. How many overly complex if/elsif/else cascades are in production code today? And what's a better (er, less complex) way to write this kind of simple code?
sub Form::handle_request {
my($self, $request) = @_;
$self->validate_request;
return $self->generate_response;
}
sub Form::validate_request {
my($self, $request) = @_;
$constraint->validate_request($request);
}
sub Form::generate_response {
throw Exception::SubclassResponsibility;
}
type handler. (Admittedly, relying on everything to throw an exception that has a display method is possibly a little courageous...)$response = eval {...} || $@; $response->display