2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
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.
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
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());
}
}
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
function countPositiveNumbers(arr: number[], countZero = false) {
// ...
}
function toggleProperty(property: string, value: boolean) {
setProperty(property, value);
}
function togglePropertyToCalculatedValue(property: string) {
setProperty(property, calculateProperty());
}
----
2021-04-28 18:08:03 +02:00
2021-09-20 15:38:42 +02:00
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Provide a default value for 'xxx' so that the logic of the function is more evident when this parameter is missing. Consider defining another function if providing default value is not possible.
=== Highlighting
Entire parameter with type
2021-09-20 15:38:42 +02:00
endif::env-github,rspecator-view[]