org.sonar.l10n.javascript.rules.javascript.S1488.html Maven / Gradle / Ivy
Why is this an issue?
Declaring a variable only to immediately return or throw it is considered a bad practice because it adds unnecessary complexity to the code. This
practice can make the code harder to read and understand, as it introduces an extra step that doesn’t add any value. Instead of declaring a variable
and then immediately returning or throwing it, it is generally better to return or throw the value directly. This makes the code cleaner, simpler, and
easier to understand.
How to fix it
Declaring a variable only to immediately return or throw it is considered a bad practice because it adds unnecessary complexity to the code. To fix
the issue, return or throw the value directly.
Code examples
Noncompliant code example
function computeDurationInMilliseconds(hours, minutes, seconds) {
const duration = (((hours * 60) + minutes) * 60 + seconds) * 1000;
return duration;
}
Compliant solution
function computeDurationInMilliseconds(hours, minutes, seconds) {
return (((hours * 60) + minutes) * 60 + seconds) * 1000;
}
Noncompliant code example
function doSomething() {
const myError = new Error();
throw myError;
}
Compliant solution
function doSomething() {
throw new Error();
}