Skip to main content

Remove redundant variable creation expression when it is only returned/thrown (Sonar)

sonar:java/remove-redundant-variable-creation-s1488​

ImportanceReview GuidanceRequires Scanning Tool
LOWMerge Without ReviewYes (Sonar)

This change removes intermediate variables who are only created to be thrown or returned in the next statement. This makes the code more readable, which makes reviewing the code for issues easier.

Our changes look something like this:

    public LocaleResolver localeResolver() {
- SessionLocaleResolver localeResolver = new SessionLocaleResolver();
- return localeResolver;
+ return new SessionLocaleResolver();
}
    public void process() {
- Exception ex = new Exception();
- throw ex;
+ throw new Exception();
}

References​