Со ссылкой на ваш 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;
}