Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

83 lines
2.3 KiB
Plaintext

== Why is this an issue?
The magic of JavaScript is that you can pass arguments to functions that don't declare parameters, and on the other side, you can use those passed-in arguments inside the no-args ``++function++``.
But just because you can, that does't mean you should. The expectation and use of arguments inside functions that don't explicitly declare them is confusing to callers. No one should ever have to read and fully understand a function to be able to use it competently.
If you don't want to name arguments explicitly, use the ``++...++`` syntax to specify that an a variable number of arguments is expected. Then inside the function, you'll be dealing with a first-class array, rather than an array-like structure.
=== Noncompliant code example
[source,javascript]
----
function concatenate() {
let args = Array.prototype.slice.call(arguments); // Noncompliant
return args.join(', ');
}
function doSomething(isTrue) {
var args = Array.prototype.slice.call(arguments, 1); // Noncompliant
if (!isTrue) {
for (var arg of args) {
...
}
}
}
----
=== Compliant solution
[source,javascript]
----
function concatenate(...args) {
return args.join(', ');
}
function doSomething(isTrue, ...values) {
if (!isTrue) {
for (var value of values) {
...
}
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use the rest syntax to declare this function's arguments.
=== Highlighting
* Primary: First use of ``++arguments++``
* Secondary: All other uses of ``++arguments++``
** message: 'Replace this reference to "arguments".'
'''
== Comments And Links
(visible only on this page)
=== on 19 Feb 2016, 11:12:14 Elena Vilchik wrote:
\[~ann.campbell.2] I don't think that tag ``++api-design++`` is good here. What ever way you use arguments in function declaration, the way to call this function is the same.
=== on 19 Feb 2016, 14:28:02 Ann Campbell wrote:
\[~elena.vilchik] it should be clear from the function declaration what you need to pass into it. Declaring a no-args function and expecting args in it is crap design IMO.
=== on 24 Apr 2017, 11:54:57 Elena Vilchik wrote:
\[~ann.campbell.2] Don't you mind making severity ``++minor++``?
endif::env-github,rspecator-view[]