I have a Java method with a void type of return that checks a condition. It do something in case of true and other thing in case of false, typical if / else structure, but is possible use only the if block with a empty return in the end and omit the else block placing its contents with one less level of indentation. I don't know the implications of use one or other form, is one of they a bad practice? or use one or the other is a personal election?
// The first way
private void test(String str) {
if (str.equals("xxx")) {
// Do something
} else {
// Do other thing
}
}
// The second way
private void test(String str) {
if (str.equals("xxx")) {
// Do something
return;
}
// Do other thing
}
2 Answers 2
It depends on the semantics of your code, if the else branch is the point of the method, i.e. it's named after what happens in the else branch, the early return is probably correct, especially if the first part is some sort of check.
If on the other hand the two branches are both related to the methods functionality, the if/else makes more sense to use.
ex.
void frob(...)
{
if(isBlob())
return; //can't frob a blob
doFrobbing(...)
}
vs.
void condOp(...)
{
if(condition)
conditionalthing();
else
otherthing();
}
making the code match the semantics of your method makes it easier to read.
-
I think, your example don't respond exactly to my question, the return in the first code example is a simple "safeguard clause", the if block only check if a condition is valid before to continue the execution of code, it don't execute nothing in it.Orici– Orici2018年07月17日 07:16:54 +00:00Commented Jul 17, 2018 at 7:16
-
@Orici: Imagine if the "safeguard clause" (as you call it) would also log a message that the safeguard clause was activated. While it is technically not just a return statement, it really still is a "safeguard clause". The question is where you draw the line of what is a safeguard clause and what is not. Finding a universal rule that is pedantically correct, in my opinion, is a futile effort. In most cases, the surrounding context of the method already makes it abundantly clear whether it's a "safeguard clause" or not.Flater– Flater2018年07月17日 12:37:16 +00:00Commented Jul 17, 2018 at 12:37
-
@Orici I meant if your code resemebles the safeguard clause use that pattern, if it doesn't use what ever is cleaneresoterik– esoterik2018年07月17日 17:53:48 +00:00Commented Jul 17, 2018 at 17:53
-
@esoterik I really like making it about the name.candied_orange– candied_orange2018年07月20日 03:17:43 +00:00Commented Jul 20, 2018 at 3:17
-
Instead of
if (str.equals("xxx"))
it is safer to useif ("xxx".equals(str))
. This might spare you an NPE when thestr
is nullAlexander Farber– Alexander Farber2020年01月21日 09:18:45 +00:00Commented Jan 21, 2020 at 9:18
If "something" and "other thing" are simple, straightforward, and understandable, it doesn't matter because both work.
// The first way is completely readable
private void test(String str) {
if (str.equals("xxx")) {
System.out.println("value is xxx");
} else {
System.out.println(str);
}
return;
}
// The second way is also readable
private void test(String str) {
if (str.equals("xxx")) {
System.out.println("value is xxx");
return;
}
System.out.println(str);
return;
}
However, if "something" or "other thing" are complex and hard to understand, it doesn't matter because both fail, and the solution is to refactor it into something simple enough that the code is back to the first case.
// The first way is completely readable
private void test(String str) {
if (str.equals("xxx")) {
doSomething(); // obfuscates the complex code you had before
} else {
doOtherThing(); // obfuscates the complex code you had before
}
return;
}
// The second way is also readable
private void test(String str) {
if (str.equals("xxx")) {
doSomething(); // obfuscates the complex code you had before
return;
}
doOtherThing(); / /obfuscates the complex code you had before
return;
}
/*
* Obviously, real code would have better variable and function names,
* allowing the reader to understand what is happening.
* If I care about how we doSomething(), I can investigate that function,
* but I know that test(String) in some cases does something
* and in other cases does the other thing, which may be enough
*/
-
6The last
return
is completely unuseful in a void method.Orici– Orici2018年07月17日 07:13:19 +00:00Commented Jul 17, 2018 at 7:13 -
3Obfuscate generally has a negative connotation when it comes to code readability, I think "abstracts" or "pulls out" is the word you're wanting.Ecksters– Ecksters2020年12月18日 21:23:27 +00:00Commented Dec 18, 2020 at 21:23
if return
andif else
. Both of your example methods have a CC of 2.