構造化プログラミングではサブルーチンの出口は
1つにすべきである、とかつて学んだ気がしたのですが、そうではないのですね。
Historically, several different structuring techniques or methodologies have been developed for writing structured programs. The most common are:
(中略)
2. A view derived from Dijkstra's which also advocates splitting programs into sub-sections with a single point of entry, but is strongly opposed to the concept of a single point of exit.
(wikipedia:en:Structured_programming)
コードを読む立場では、サブルーチンが複数の出口をもつ利点と欠点は、
- 利点:不必要な処理を追わなくてよくなる(elseの除去)
- 欠点:(値を返す場合に)値を返すコードがあちこちに分散する
ということでしょうか。
いままで私はサブルーチンの出口をなるべく1つにしようと努力していました。これは改めるべきですね。