
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.
104 lines
2.5 KiB
Plaintext
104 lines
2.5 KiB
Plaintext
== Why is this an issue?
|
||
|
||
include::../description.adoc[]
|
||
|
||
=== Noncompliant code example
|
||
|
||
With a maximum number of 4 parameters:
|
||
|
||
[source,python]
|
||
----
|
||
def do_something(param1, param2, param3, param4, param5):
|
||
...
|
||
----
|
||
|
||
=== Compliant solution
|
||
|
||
[source,python]
|
||
----
|
||
def do_something(param1, param2, param3, param4):
|
||
...
|
||
----
|
||
|
||
=== Exceptions
|
||
|
||
The first argument of non-static methods, i.e. ``++self++`` or ``++cls++``, is not counted as it is mandatory and it is passed automatically.
|
||
|
||
ifdef::env-github,rspecator-view[]
|
||
|
||
'''
|
||
== Implementation Specification
|
||
(visible only on this page)
|
||
|
||
include::../message.adoc[]
|
||
|
||
=== Parameters
|
||
|
||
.max
|
||
****
|
||
|
||
----
|
||
13
|
||
----
|
||
|
||
Maximum authorized number of parameters
|
||
****
|
||
|
||
|
||
'''
|
||
== Comments And Links
|
||
(visible only on this page)
|
||
|
||
=== on 15 May 2019, 12:21:20 Marcel Vingerling wrote:
|
||
In my opinion this rule should ignore mandatory named arguments that have been separated in the function definition by a {color:#FF0000}``++*++``{color} in the argument list.
|
||
|
||
|
||
|
||
=== Noncompliant code example
|
||
|
||
With a maximum number of 4 parameters:
|
||
|
||
[source,python]
|
||
----
|
||
def do_something(param1, param2, param3, param4, param5='default value'):
|
||
...
|
||
----
|
||
{color:#172b4d}This is non-compliant because the function can still be called like this:{color}
|
||
|
||
[source,python]
|
||
----
|
||
do_something(1, 2, 3, 4, 5){code}
|
||
h2. Compliant Solution
|
||
----
|
||
def do_something(param1, param2, param3, param4, *, param5='default value'):
|
||
|
||
...
|
||
|
||
[source,python]
|
||
----
|
||
In this case the {{*}} marker in the function definition dictates that the param5 parameter should always be passed as a named argument. Therefore the maximum number of positional parameters for this function is 4 and should be compliant as named parameters self document the method call and cause less brain-overload.
|
||
This should be compliant because the function can only be called like this:
|
||
----
|
||
do_something(1, 2, 3, 4, param5=5){code}
|
||
|
||
|
||
|
||
|
||
This could either be implemented in this rule or added as an additional rule, then the maximum total number of parameters can also still be validated. Maybe it makes sense to also limit the maximum number of mandatory named arguments using a rule configuration setting.
|
||
|
||
|
||
|
||
|
||
=== on 9 Mar 2020, 13:55:53 Nicolas Harraudeau wrote:
|
||
Hi [~sjaak],
|
||
|
||
|
||
Thank you for your suggestion, and sorry for the late reply. Could you post it on https://community.sonarsource.com/c/bug/fp/7[the community forum] please? This will enable other people to contribute to this discussion more easily.
|
||
|
||
|
||
Thanks
|
||
|
||
include::../comments-and-links.adoc[]
|
||
|
||
endif::env-github,rspecator-view[]
|