48 lines
1.2 KiB
Plaintext
48 lines
1.2 KiB
Plaintext
Having default value for optional boolean parameters makes the logic of function when missing that parameter more evident. When providing a default value is not possible, it is better to split the function into two with a clear responsibility separation.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
function countPositiveNumbers(arr: number[], countZero?: boolean) { // Noncompliant, default value for 'countZero' should be defined
|
|
// ...
|
|
}
|
|
|
|
function toggleProperty(property: string, value?: boolean) { // Noncompliant, a new function should be defined
|
|
if (value !== undefined) {
|
|
setProperty(property, value);
|
|
} else {
|
|
setProperty(property, calculateProperty());
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
function countPositiveNumbers(arr: number[], countZero = false) {
|
|
// ...
|
|
}
|
|
|
|
function toggleProperty(property: string, value: boolean) {
|
|
setProperty(property, value);
|
|
}
|
|
|
|
function togglePropertyToCalculatedValue(property: string) {
|
|
setProperty(property, calculateProperty());
|
|
}
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
include::highlighting.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|