When are named arguments useful?

c#

Solution

Named arguments are meant to increase readability. For example I've just used one as such

public void MarkAsDone(bool skipped) {}

Now by invoking the method without the name we have an ambiguity

MarkAsDone(true); //does true mean that it is successfully done?

Which can be resolved by clarifying with a name

MarkAsDone(skipped: true);

I think using the named parameter makes the client code way less ambiguous.

Apart from that they can be used to uniquely identify an optional parameter when there's more than one with the same type

MarkAsDone(int first, int second=0, int third=0) {}

///

MarkAsDone(1, third: 3);

Problem

Is there any case in C# code where positional arguments are not enough? I really don't see any benefit of named arguments, on the contrary, I can see how overusing of named arguments could make code hard to read? So my question is, why would someone use them and how can it help in writing better code as I'm sure they were not implemented without reason? This looks cleaner to me: ``` private void Foo(1, true); ``` than: ``` private void Foo(bar: 1, baz: true); ```

Original source

Related problems