[ACCEPTED]-Better Java method Syntax? Return early or late?-return-path
If it's a method you'll be calling thousands 7 of times, then early return is better to 6 achieve a [slightly] increased performance.
If 5 not, then I'd prefer late return, since 4 it improves readability.
Remember programmers 3 usually spend more time reading than writing 2 code, so anything you can do to improve 1 readability will be certainly welcome.
I prefer returning early and avoiding deep 10 nesting. This is particularly true right at the start 9 of the method: test anything that's simple, and 8 get out (or throw an exception) if you can 7 do so really early.
If it's right in the 6 middle of a method, it's more of a judgement 5 call.
Note that I'd refactor your example 4 straight away to use a single if
:
boolean validate(DomainObject o) {
if (o.property == x || o.property2 == y) {
return true;
} ...
return false;
}
I realise 3 this was only a toy example, but my point 2 is that it's always worth looking for more 1 ways to simplify your code :)
As with most coding styles, it's really 2 a matter of preference, but guard clauses are considered 1 by many to be a best practice.
The only time I would say you definitely 11 shouldn't return early is if you can't easily 10 see every return within a single screen 9 (whatever the standard might be for people 8 working on the same code base), you should 7 at the very least be adding comments indicating 6 that the function can return early if there 5 is an early return.
The only time I would 4 say you definitely should return early is 3 if your code looks like...
boolean valid = true;
if( condition1 ) {
valid = false;
}
if( valid ) {
...
if( condition2 ) {
valid = false;
}
}
if( valid ) {
...
if( condition3 ) {
valid = false;
}
}
... (etc)
If you find yourself 2 in either of these situations, however... you 1 should probably be refactoring the function.
To me, this is sort of one of those religious 11 war topics with no correct answer. The 10 argument against returning early essentially 9 boils down to the fact that having one and 8 only one point where a function can exit 7 reduces the number of possible paths through 6 your code, thus, in theory at least, reducing 5 the chances for bugs. My personal style 4 is to, in situations where it makes sense 3 to return early do so, and in situations 2 where it makes sense to limit to one return 1 statement I do that.
There are two factors pulling against each 16 other.
The first factor is ease of debugging. If 15 you return immediately (as shown in your 14 second code snippet), it sometimes becomes 13 difficult to debug a big function since 12 it is hard to find these return statements, specially 11 if they were put there by mistake.
The second 10 factor is ease of implementation. If you 9 are checking basic correctness of arguments 8 at the beginning of the function and there 7 is a long piece of code before the function 6 finishes, you might have to put that entire 5 code in a condition loop. If you don't, at 4 some point the argument might get used for 3 some long calculation, wasting time, because 2 it would ultimately be rejected anyways.
So, the 1 answer could be like this:
If the function is small,
save the return status in a variable and return at the end.
else
return immediately.
If exceptions aren't part of the picture, I 6 prefer returning immediately when I can.
It 5 can be easy to mismanage the flag variable 4 and I'm against flag variables in general. Not 3 returning also might make a maintainer think 2 that further work might be done (if the 1 method is long).
Personally, I like the second method better. It 3 is straightforward and clearer to me, but 2 I do know there are people who must have 1 only one return in a function.
Honestly I think it depends on the situation. Personally 10 I use both, and I decide based on which 9 one will make the code more clear and easy 8 to read.
If you have heavily nested if statements 7 (or any other control structure) and it 6 may get confusing, then I would return inside 5 the statements
Don't worry too much about 4 what is 'best practice' in this case, as 3 it is more important that the code is clear 2 and easy to understand. Use what feels right 1 for the situation.
For this case, I prefer:
boolean validate (DomainObject o) {
if (o.property == x ||
o.property2 == y ||
...) {
return true;
} else {
return false;
}
In general, I like 2 to use early return to handle error conditions, and 1 return at the end to return computed results.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.