65 lines
2.7 KiB
Plaintext
65 lines
2.7 KiB
Plaintext
== Why is this an issue?
|
|
|
|
When a function has multiple `return` statements and returns the same value in more than one of them, it can lead to several potential problems:
|
|
|
|
* It can make the code more difficult to understand and maintain, as the reader may be unsure why the same value is being returned multiple times. This can introduce ambiguity and increase the chances of misunderstanding the function's intent.
|
|
* 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 'false' on 3 different return statements
|
|
if (a < 0) {
|
|
return false;
|
|
}
|
|
if (a > 10) {
|
|
g(a); // side effect
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
----
|
|
|
|
To address this, you should refactor the function to use a single return statement with a variable storing the value to be returned. This way, the code becomes more concise, easier to understand, and reduces the likelihood of introducing errors when making changes in the future. By using a single return point, you can also enforce consistency and prevent unexpected return values.
|
|
|
|
[source,javascript,diff-id=1,diff-type=compliant]
|
|
----
|
|
function f(a, g) {
|
|
if (a > 10) {
|
|
g(a); // side effect
|
|
}
|
|
return false;
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
=== Documentation
|
|
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return[``++return++``]
|
|
* MDN web docs - https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Return_values[Function return values]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Refactor this function to not always return the same value.
|
|
|
|
|
|
include::../highlighting.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|