JS-641 Modify S3516 description: improve example (#4778)

This commit is contained in:
Ilia Kebets 2025-03-17 07:10:53 +01:00 committed by GitHub
parent f284ff36ad
commit c7f2158161
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,18 +6,21 @@ When a function has multiple `return` statements and returns the same value in m
* The use of multiple return statements with the same value might lead someone to assume that each return corresponds to a distinct case or outcome. However, if they all return the same value, it can be misleading and may indicate an oversight or mistake in the code.
* When the function needs to be modified or extended in the future, having multiple identical return statements can make it harder to implement changes correctly across all occurrences. This can introduce bugs and inconsistencies in the codebase.
* Code readability is crucial for maintainability and collaboration. Having repetitive return statements can lead to unnecessary code duplication, which should be avoided in favor of creating modular and clean code.
* This often happens in functions that perform a conditional side effect but always return the same value.
This rule raises an issue when a function contains several ``++return++`` statements that all return the same value.
[source,javascript,diff-id=1,diff-type=noncompliant]
----
function f(a, g) { // Noncompliant: 'f' returns 'b' on two different return statements
const b = 42;
if (a) {
g(a);
return b;
function f(a, g) { // Noncompliant: 'f' returns 'false' on 3 different return statements
if (a < 0) {
return false;
}
return b;
if (a > 10) {
g(a); // side effect
return false;
}
return false;
}
----
@ -26,11 +29,10 @@ To address this, you should refactor the function to use a single return stateme
[source,javascript,diff-id=1,diff-type=compliant]
----
function f(a, g) {
const b = 42;
if (a) {
g(a);
if (a > 10) {
g(a); // side effect
}
return b;
return false;
}
----