Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are named arguments useful?

Tags:

c#

Is there any case in C# code where positional arguments are not enough? I really don't see any benefit of named arguments, on 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); 

then:

private void Foo(bar: 1, baz: true); 
like image 832
formatc Avatar asked Dec 15 '13 23:12

formatc


People also ask

Should Use named arguments?

Always use named arguments in constructor calls Constructor arguments are often unclear. There could be a lot of arguments, multiple arguments of the same type, default arguments or everything combined. In most cases, readability improves when using named arguments in constructor calls.

What is one advantage of using named parameters?

Using named parameters really simplifies things as we don't have to worry about the order of parameters and we can specify the values for only those parameters that we want. When named parameters are used with optional parameters, the usability and the function call become easy and are much enhanced.

Why is it a good idea to use named parameters instead of the syntax?

With named parameters, it is usually possible to provide the values in any arbitrary order, since the name attached to each value identifies its purpose. This reduces the connascence between parts of the program.

How do you use named arguments in Python?

Requiring your arguments be named You can create a function that accepts any number of positional arguments as well as some keyword-only arguments by using the * operator to capture all the positional arguments and then specify optional keyword-only arguments after the * capture.


2 Answers

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); 
like image 163
Sklivvz Avatar answered Sep 27 '22 21:09

Sklivvz


I use named parameters to make call sites clearer and when I have parameters with default values. The default values case has been discussed in a number of different answers already, so let's talk about call site clarity.

An analysis with metasyntactic variables isn't going to highlight their usefulness. Consider, instead this more "real-world", if you will, example.

Let's look at a call site:

something.OpenFile(documentPath, true); 

What is this going to do? It's going to open documentPath. And do something else? What else? I can't remember, even though I wrote OpenFile only a week ago.

Here are three different examples for OpenFile that are relatively realistic.

void OpenFile(string path, bool shouldOverwrite) void OpenFile(string path, bool preserveExisting) void OpenFile(string path, bool enableWriting) 

With named parameters, we can make the call sites clear:

something.OpenFile(documentPath, shouldOverwrite: false); 

It's pretty clear that the file will not be overwritten.

something.OpenFile(documentPath, preserveExisting: false); 

It's pretty clear that the file will be overwritten if needed.

And finally, we have:

something.OpenFile(documentPath, enableWriting: false) 

It's pretty clear that the file will be opened for reading only.

Could this particular example be solved with something else like an enum? Yes. Can you always change the code? No. Does everyone else have the same abiding hatred for bool parameters that I do? No. :-)

Can you over do it with named parameters? Yes. Do good local variable names help? Tremendously.

like image 34
chwarr Avatar answered Sep 27 '22 20:09

chwarr