Edit 2を参照すると、OOよりも古いプログラミングの正統性に違反しているため、常に少し不自然に見えます:「構造化プログラミング」(http://en.wikipedia.org/wiki/Structured_programmingを参照)。それはgotoのスマックでもあり、すべての優れたプログラマーは、gotoをコードに入れる場合は告白に行く必要があることを知っています。
コンパイラーが関数の制御フローを分析するのが難しくなるかもしれないという懸念があるかもしれませんが、それは通常、効率上の理由から使用される一種のツールです。たとえば、のApache実装java.lang.String
は、少なくとも最適化を目的としたこの関数でそれを使用します。
/*
* An implementation of a String.indexOf that is supposed to perform
* substantially better than the default algorithm if the "needle" (the
* subString being searched for) is a constant string.
*
* For example, a JIT, upon encountering a call to String.indexOf(String),
* where the needle is a constant string, may compute the values cache, md2
* and lastChar, and change the call to the following method.
*/
@SuppressWarnings("unused")
private static int indexOf(String haystackString, String needleString,
int cache, int md2, char lastChar) {
char[] haystack = haystackString.value;
int haystackOffset = haystackString.offset;
int haystackLength = haystackString.count;
char[] needle = needleString.value;
int needleOffset = needleString.offset;
int needleLength = needleString.count;
int needleLengthMinus1 = needleLength - 1;
int haystackEnd = haystackOffset + haystackLength;
outer_loop: for (int i = haystackOffset + needleLengthMinus1; i < haystackEnd;) {
if (lastChar == haystack[i]) {
for (int j = 0; j < needleLengthMinus1; ++j) {
if (needle[j + needleOffset] != haystack[i + j
- needleLengthMinus1]) {
int skip = 1;
if ((cache & (1 << haystack[i])) == 0) {
skip += j;
}
i += Math.max(md2, skip);
continue outer_loop;
}
}
return i - needleLengthMinus1 - haystackOffset;
}
if ((cache & (1 << haystack[i])) == 0) {
i += needleLengthMinus1;
}
i++;
}
return -1;
}