継続サーバのもたらす利点

HREF Considered Harmful "Let me tell you about..." by Avi Bryant

を翻訳。

Bill Clementson posts that although a lot of people are interested in the use of continuations for web applications programming, nobody has yet come up with an elevator pitch explanation of their benefits. My pitch usually goes something like this: "Continuations bring precisely the same benefits to traditional web development that subroutines bring to GOTOs". Julian Fitzell and I had to expand on this for a tutorial we gave at Smalltalk Solutions. Here's the first part of the abstract:

Dijkstra may have taught us 45 years ago that GOTO was a bad idea, but web development has barely caught up. In the CGI model of web applications, each link that is followed triggers an entirely new execution of the program. Building complex control flow out of these abrupt transitions can lead to a tangled and brittle mess of interdependent pages. Modern frameworks such as WebObjects and Struts may remove many of the difficulties of CGI scripting, but they do not escape the web's inherent GOTO: moving from one page to the next, whether through anchors, actions, forms, or forwards, is still a simple one-way jump.

Seaside is a framework for developing web-based applications that insulates the developer from the HTTP request/response loop, presenting the illusion of a continuous interactive session with the user. Each page or form acts much like a subroutine, which returns a value to its caller based on user input. Complex, conditional or looping workflows can be described in a single piece of straightforward Smalltalk code as a sequence of calls to individual pages. The benefits this brings to the reusability and maintainability of web applications closely mimick the advances made by structured programming long ago.

Bill Clementson の投稿で、"たくさんの人がWebアプリケーションのプログラミングにおける継続の使いかたについて、関心を持っているにもかかわらず、いまだに、その利点を手に取るようにわかりやすく説明したものにはお目にかからない" といわれている。
それには、私は、こんなふうな説明をすることにしている。"継続が従来のWeb開発にもたらす利点というのは、ちょうどサブルーチンがGOTOにもたらした利点のようなものだ"と。
Julian Fitzell と私は、このことを主旨として、Smalltalk Solutionsでのチュートリアルに使った。以下はそのアブストラクトのはじめの部分である。

Dijkstra は45年前にGOTO文は有害だと提唱したというが、Web開発もようやくそれに習うことができるようになった。WebアプリケーションのCGIモデルでは、リンクをたどるたびに、プログラムが起動されている。
こんな細切れの遷移では、制御の流れが複雑になるにつれて、だんだんこんがらがって、たくさんの独立したページ群を脆く寄せかためたようなものになってしまう。現代的なフレームワーク(WebObjectsStruts)は、このようなCGIスクリプティングの難点を取り払ってくれるようだが、Webの本質であるGOTOからは逃れられていない。あるページから次へいくときは、何を使うにしろ(アンカ、アクション、フォーム、フォワードなど)結局は単なる一方向のジャンプである。

Seasideは、Webベースのアプリケーションを開発するためのフレームワークであり、開発者に、HTTPのリクエスト/レスポンスのループを意識させずに、まるで、ひとつながりにユーザとインタラクティブにセッションしているような錯覚を見せてくれる。それぞれのページやフォームはまるでサブルーチンのように振る舞い、呼出し元にユーザの入力を値として返してくれる。複雑で条件分岐やループのあるワークフローも、継ぎ目なく連続した Smalltalk のコードとして、まるで個々のページを順番に呼出すように、記述できる。これがWebアプリケーションの再利用性、保守性にもたらす利点は、大昔に構造化プログラミングがもたらしたものと、うりふたつである。