finally を使用して「リターン後」にコードを持つ可能性を発見しました。ここにいくつかの例を示します。
int foo() {
BufferedReader br = ...;
try {
// cca 20 lines with 4 returns in different if-else clauses etc
//example:
if(something) {
return 0;
} else {
return 1;
}
} finally {
br.close();
}
}
(私の意見では、はるかに怠惰な)代替手段として:
int foo() {
BufferedReader br = ...;
int toReturn;
// cca 20 lines with 4 ASSIGMENTS in different if-else clauses etc
//example:
if(something) {
toReturn = 0;
} else {
toReturn = 1;
}
br.close();
return toReturn;
}
それで、問題は、どちらがより速く、より読みやすいかということです。はい、この場合、私は本当に BufferedReader を閉じています。