dotnet/roslyn

Add an analyzer to recommend using named parameters when passing literals as arguments

Aperta

#43.988 aperta il 2 mar 2020

 (30 commenti) (13 reazioni) (0 assegnatari)C# (4257 fork)batch import
Area-IDEhelp wanted

Metriche repository

Star
 (20.414 stelle)
Metriche merge PR
 (Merge medio 6g 17h) (256 PR mergiate in 30 g)

Descrizione

We employ a guideline to always use named parameters when the meaning of an argument is not obvious from the argument itself.

For example, the intent is not very clear here, from just looking at the code:

var employees = await FetchEmployeesAsync(true);

If you're in an IDE, you can of course hover over the method call for a few seconds to get a tooltip, or go to the definition of the method, but it adds friction and delay when reading code. And oftentimes, code is read outside of an IDE (for example, when reviewing a PR or perusing code on GitHub).

But it's much clearer if you do:

var employees = await FetchEmployeesAsync(includeContractors: true);

Or:

var includeContractors = true;
var employees = await FetchEmployeesAsync(includeContractors);

For most cases, this could be formalized as "always use named parameters when passing literal values as arguments in a method call".

It would be nice to have an analyzer for this. Let me know if you think this is a good idea or not. I wouldn't mind taking a stab at implementing it myself, if it doesn't already exist and nobody else is already working on it.

Guida contributor