every person has its own taste in music, fashion or cars. developers can also have different taste in programming languages, operating systems or code styles. like people argue that their taste in music is the best, so do programmers try to convince others in their way of thinking. in the next few post i want to point out some of the most frequently discussed style issues with my coworkers and mates.
style 1:
public String doSomethingStyle1(final String... parameters) {
final String result;
try {
result = doSomethingInternal(parameters);
} catch (final Exception exception) {
throw new IllegalArgumentException("oops", exception);
}
return result;
}
style 2:
public String doSomethingStyle2(final String... parameters) {
try {
return doSomethingInternal(parameters);
} catch (final Exception exception) {
throw new IllegalArgumentException("oops", exception);
}
}
so which style are you, 1, 2 or even something else, tell me about it in the comments. 😄
In the sample you posted above I would choose #2 as it's more concise and the extracted variable doesn't provide any benefit (excluding easier debugging or logging of the returned value). If I should answer the question in general I would tend to select #1 as it's more versatile and can have quite a few benefits in more complex scenarios.