Although this is prone to a strong sense of personal opinion, I believe that best practice is nay block the else
.
The reasons are as follows::
Readability of the code
Adding nested blocks, although in this small example will not cause a big impact, will only make it harder to read the code and make it more complex.
In practice, this can lead to situations with multiple levels of validation, as I’ve seen sometimes in systems out there:
if (condicao1){
throw new RuntimeException("Mensagem 1");
}else{
System.out.println("Não deu exception 1");
if (condicao1){
throw new RuntimeException("Mensagem 2");
}else{
System.out.println("Ainda não deu exception 2");
if (condicao1){
throw new RuntimeException("Mensagem 3");
}else{
System.out.println("Ainda não deu exception 3");
}
}
}
For those who think I’m exaggerating, it wasn’t once or twice that I saw methods with validations of 5, 6 or more levels in financial systems methods with somewhat complex business rules.
It’s not necessary
A condition that includes an exception release, or other instruction that will interrupt the execution of the method does not need this type of construction. Unnecessary code is a waste of time.
It is better to separate the initial validations
The initial validations of a method do not need and probably should not influence the main logic of the method.
public int meuMetodo(String parametro) {
if (parametro == null || parametro.isEmpty())
throw new IllegalArgumentException("Parâmetro não informado!");
//lógica aqui
return 123;
}
Only by "decompiling" the bytecode to find out... I believe that both forms are equivalent, so that a criterion to choose between one and the other would be subjective and/or would vary case by case.
– mgibsonbr
That question would be better on Codereview.
– karlphillip
@karlphillip probably if we ever have a codereview in pt, it will be migrated.
– Bacco
@Bacco In other words, what are you suggesting is that pt.SO presents questions that would be appropriate for Code Review, Programmers, Signal Processing, Superuser, Serverfault, Webmasters and Game Development just because there is no dedicated forum for each theme in Portuguese? This is gonna be a huge mess...
– karlphillip
@karlphillip But I didn’t suggest anything! : ). As for this discussion, it was very extensive both in the goal and in area 51, I suggest a look at both, in particular http://meta.answall.com/questions/aqui-no-o-stackoverflow-com. In summary, here the limits are wider, but some issues really off topic have already been closed up. Anyway the community is still looking for their point of balance, and you can certainly contribute to the goal ( http://meta.answall.com ) with your opinion, which is the best way forward.
– Bacco
@Bacco Thank you very Much, Sir. ;)
– karlphillip
Just a suggestion, if you’re pitching
IllegalArgumentException
and similar, it is worth using a library of preconditions like the Preconditions of the Guava or Validate from Apache Commons. These libraries remove the need for if and make code much more readable.– Anthony Accioly