question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: Require named arguments

See original GitHub issue

Say we have a method or constructor with many parameters of the same type:

public Foo(int a, int b, int c, int d)
{
    ...
}

And creating an instance:

new Foo(
    a: 1,
    b: 2,
    c: 3,
    d: 4);

This comes up most often for constructors of immutable types.

The warning could show when the following criteria are met:

  • More than 3 arguments.
  • Each argument on their own row.
  • At least two adjacent arguments are of the same type.
  • Not in an expression tree.

Wrote it here if anyone wants to play with it. Think it is a good fit in StyleCop though.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:4
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ArthurHNLcommented, Oct 23, 2019

I would argue that the warning should not be fired when the argument name is equal to the name of the variable that is used as the argument:


public void DoSomething(int a, int b, int c)
{
}

public void SomeMethod()
{
   int a = 1;
   int foo = 2;
   int d = 3;

   DoSomething(
      a, // No warning, argument name is the same as variable name.
      foo, // Warning, no argument name is provided AND the argument name is not equal to the argument name.
      c: d); // No warning, argument name is provided.
}

This sort of “copies” the behaviour of ReSharper, ReSharper shows the argument name in the code when it is not specified and the variable used as argument does not have the same name as the argument.

1reaction
sharwellcommented, Nov 16, 2016

💭 I wonder how helpful a “StyleCop Extras” project would be, where many of the code-style-related subjective rules that get requested could be developed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Named arguments proposal
It's not possible to have a named parameter that is required to have a value passed to it. As per 1, they're all...
Read more >
samuelgoto/proposal-named-parameters: An exploration of ...
An exploration of adding named parameters to JS. Contribute to samuelgoto/proposal-named-parameters development by creating an account on GitHub.
Read more >
A proposal for named arguments for C++
This proposal allows calling existing functions with named arguments, without the author of the function having to "opt-in" to this in any way....
Read more >
What happened to the named parameter proposal? : r/cpp
Named parameters makes the builder pattern redundant in most cases and lead to clearer code in my opinion. I am interested want happened...
Read more >
Why we need named arguments in PHP
The main argument against named arguments — the PHP 8 puns continue — is that they would make maintaining open source software a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found