Is there any difference between following two methods?
以下两种方法有什么区别吗?
Which one is preferable and why?
哪一个更可取,为什么?
Prg1:
PRG1:
public static boolean test() throws Exception {
try {
doSomething();
return true;
} catch (Exception e) {
throw new Exception("No!");
}
}
Prg2:
PRG 2:
public static boolean test() throws Exception {
try {
doSomething();
} catch (Exception e) {
throw new Exception("No!");
}
return true;
}
5 个解决方案
#1
23
Consider these cases where you're not returning a constant expression:
考虑这些您没有返回常量表达式的情况:
Case 1:
情况1:
public static Val test() throws Exception {
try {
return doSomething();
} catch (Exception e) {
throw new Exception("No!");
}
// Unreachable code goes here
}
Case 2:
案例2:
public static Val test() throws Exception {
Val toReturn = null;
try {
toReturn = doSomething();
} catch (Exception e) {
throw new Exception("No!");
}
return toReturn;
}
I would prefer the first one. The second is more verbose and might cause some confusion when debugging.
我更喜欢第一个。第二个更冗长,在调试时可能会引起一些混乱。
If test()
incorrectly returns null
, and you see toReturn
being initialized to null
, you might think the problem is in test()
(especially when test()
is not just a simple example like this).
如果test()错误地返回null,并且您看到toReturn被初始化为null,您可能会认为问题出在test()中(特别是当test()不仅仅是这样的简单示例时)。
Even though it can only return null
if doSomething
returns null
. But that might be hard to see at a glance.
即使doSomething返回null,它也只能返回null。但这可能很难一目了然。
You could then argue that, for consistency's sake, it's better to always use the first form.
然后你可以争辩说,为了保持一致性,最好总是使用第一种形式。
#2
4
Nope there is no difference between both the methods. It will return true value in both the cases effectively by resuming the flow of the program as soon an and exception is handled. Catch will be accessed only when an exception occurs.
不,两种方法之间没有区别。通过尽快恢复程序流并处理异常,它将在两种情况下有效地返回真值。只有在发生异常时才会访问Catch。
#3
3
There is no functional difference. Both programs behave the same way. It is only a matter of coding style and personal taste.
没有功能差异。两个程序的行为方式相同。这只是编码风格和个人品味的问题。
As a good practice, it is better to have a single return statement per method - at the end of the method, instead of in-between. In the case of lengthy blocks of code, this style makes the code more readable and more maintainable in the future when the (original author or someone else) has to make changes to the code.
作为一种好的做法,每个方法最好有一个return语句 - 在方法的最后,而不是在中间。在冗长的代码块的情况下,当(原作者或其他人)必须对代码进行更改时,这种样式使代码在将来更具可读性和可维护性。
Hence, the first one is considered better from a good practice standpoint.
因此,从良好的实践角度来看,第一个被认为是更好的。
#4
2
I'm assuming this is a general question. Otherwise I might comment on other aspects of your method(s).
我假设这是一个普遍的问题。否则,我可能会评论您的方法的其他方面。
I think in the case or small methods like these it doesn't really matter. The method is short enough to understand immediately what's going on, what's related to what etc.
我认为在这种情况下或类似的小方法并不重要。该方法足够短,可以立即了解发生了什么,与什么有关等等。
However, in the case of longer methods the flow is much easier to follow in the first example. In my opinion. It keeps together related code and related scenarios. When you're reading the method, the normal execution flow is not broken by the catch
block, making it more obvious and "fluent".
但是,在较长方法的情况下,在第一个例子中流程更容易遵循。我的想法是。它将相关代码和相关场景保持在一起。当您阅读该方法时,正常的执行流程不会被catch块破坏,使其更加明显和“流畅”。
public static boolean test() throws Exception {
try {
doSomething();
return true;
} catch (Exception e) {
throw new Exception("No!");
}
}
But I won't generalize this for all methods; it's all about the context.
但我不会对所有方法进行概括;这完全取决于背景。
#5
-5
There is no difference, but the first Prg1 is faster than the Prg2.
没有区别,但第一个Prg1比Prg2快。
#1
23
Consider these cases where you're not returning a constant expression:
考虑这些您没有返回常量表达式的情况:
Case 1:
情况1:
public static Val test() throws Exception {
try {
return doSomething();
} catch (Exception e) {
throw new Exception("No!");
}
// Unreachable code goes here
}
Case 2:
案例2:
public static Val test() throws Exception {
Val toReturn = null;
try {
toReturn = doSomething();
} catch (Exception e) {
throw new Exception("No!");
}
return toReturn;
}
I would prefer the first one. The second is more verbose and might cause some confusion when debugging.
我更喜欢第一个。第二个更冗长,在调试时可能会引起一些混乱。
If test()
incorrectly returns null
, and you see toReturn
being initialized to null
, you might think the problem is in test()
(especially when test()
is not just a simple example like this).
如果test()错误地返回null,并且您看到toReturn被初始化为null,您可能会认为问题出在test()中(特别是当test()不仅仅是这样的简单示例时)。
Even though it can only return null
if doSomething
returns null
. But that might be hard to see at a glance.
即使doSomething返回null,它也只能返回null。但这可能很难一目了然。
You could then argue that, for consistency's sake, it's better to always use the first form.
然后你可以争辩说,为了保持一致性,最好总是使用第一种形式。
#2
4
Nope there is no difference between both the methods. It will return true value in both the cases effectively by resuming the flow of the program as soon an and exception is handled. Catch will be accessed only when an exception occurs.
不,两种方法之间没有区别。通过尽快恢复程序流并处理异常,它将在两种情况下有效地返回真值。只有在发生异常时才会访问Catch。
#3
3
There is no functional difference. Both programs behave the same way. It is only a matter of coding style and personal taste.
没有功能差异。两个程序的行为方式相同。这只是编码风格和个人品味的问题。
As a good practice, it is better to have a single return statement per method - at the end of the method, instead of in-between. In the case of lengthy blocks of code, this style makes the code more readable and more maintainable in the future when the (original author or someone else) has to make changes to the code.
作为一种好的做法,每个方法最好有一个return语句 - 在方法的最后,而不是在中间。在冗长的代码块的情况下,当(原作者或其他人)必须对代码进行更改时,这种样式使代码在将来更具可读性和可维护性。
Hence, the first one is considered better from a good practice standpoint.
因此,从良好的实践角度来看,第一个被认为是更好的。
#4
2
I'm assuming this is a general question. Otherwise I might comment on other aspects of your method(s).
我假设这是一个普遍的问题。否则,我可能会评论您的方法的其他方面。
I think in the case or small methods like these it doesn't really matter. The method is short enough to understand immediately what's going on, what's related to what etc.
我认为在这种情况下或类似的小方法并不重要。该方法足够短,可以立即了解发生了什么,与什么有关等等。
However, in the case of longer methods the flow is much easier to follow in the first example. In my opinion. It keeps together related code and related scenarios. When you're reading the method, the normal execution flow is not broken by the catch
block, making it more obvious and "fluent".
但是,在较长方法的情况下,在第一个例子中流程更容易遵循。我的想法是。它将相关代码和相关场景保持在一起。当您阅读该方法时,正常的执行流程不会被catch块破坏,使其更加明显和“流畅”。
public static boolean test() throws Exception {
try {
doSomething();
return true;
} catch (Exception e) {
throw new Exception("No!");
}
}
But I won't generalize this for all methods; it's all about the context.
但我不会对所有方法进行概括;这完全取决于背景。
#5
-5
There is no difference, but the first Prg1 is faster than the Prg2.
没有区别,但第一个Prg1比Prg2快。