Declaring a variable only to immediately return or throw it is a bad practice.
Some developers argue that the practice improves code readability, because it enables them to explicitly name what is being returned. However, this variable is an internal implementation detail that is not exposed to the callers of the method. The method name should be sufficient for callers to know exactly what will be returned.
- SonarQube link: https://sonar.openmrs.org/project/issues?id=org.openmrs%3Aopenmrs&issues=AWT7U3IOoPwAqlrbRYit&open=AWT7U3IOoPwAqlrbRYit
- GitHub link: https://github.com/openmrs/openmrs-core/blob/2.3.1/api/src/main/java/org/openmrs/api/db/hibernate/HibernatePatientDAO.java#L183-L185
- SonarQube link: https://sonar.openmrs.org/project/issues?id=org.openmrs%3Aopenmrs&issues=AWM-2UqHSNPHhr6Gt5N4&open=AWM-2UqHSNPHhr6Gt5N4
- GitHub link: https://github.com/openmrs/openmrs-core/blob/2.3.1/api/src/main/java/org/openmrs/obs/handler/AbstractHandler.java#L72-L74
Create a local variable just to be returned immediately.
Return an interesting value immediately, without creating unnecessary variables.