dotnet/roslyn

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

オープン

#43,988 opened on 2020/03/02

 (30 件のコメント) (13 件のリアクション) (0 人の担当者)C# (4,257 件のフォーク)batch import
Area-IDEhelp wanted

Repository metrics

Stars
 (20,414 個のスター)
PR merge metrics
 (平均マージ 6d 17h) (30d で 256 merged PRs)

説明

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.

コントリビューターガイド