When are named arguments useful?

Related searches

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);


private void Foo(bar: 1, baz: true);

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.

A part 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);

Named parameter, As both sellerName and productName are string types, instead of sending arguments by position, it makes sense to use named arguments to disambiguate the two and reduce confusion for anyone reading the code. Named arguments are especially useful when you are calling a procedure that has optional arguments. If you use named arguments, you don't have to include commas to denote missing positional arguments. Using named arguments makes it easier to keep track of which arguments you passed and which you omitted. Optional arguments are preceded by the Optional keyword in the procedure definition. You can also specify a default value for the optional argument in the procedure definition.

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.

Named and Optional Arguments, IMO, named arguments are useful when you need to deal with optional arguments. With mandatory arguments, you must assign a value to each� Named arguments make life much easier. They allow you to play with function arguments almost like you play with letters in Scrabble. You can pass any argument in whatever order you want just by

We found a very interesting use for named arguments when we needed to use a method like this:

private void ShowPopup(FrameworkElement content, 
                         string title = "My Application", 
                         bool isDialog = true,
                         double? width = null, 
                         double? height = null, 
                         double? offsetX = null, 
                         double? offsetY = null, 
                         bool isTransparent = false,
                         ... etc) 

where almost all parameters are optional. There are situations where you will want to leave all these parameters to their default, except one or a couple of them, such as:

PopupHelper.ShowPopup(_view, isTransparent: true);

or things like that.

Keyword (Named) Arguments in Python: How to Use Them, I find the C#'s optional named parameters very useful. In essence, it allows you to pick and choose when to use named parameters and when� Because we used named arguments, we were able to leave out two arguments and rearrange the remaining 2 arguments in a sensible order (the file object is more important than the “wt” access mode). When we use keyword arguments: We can often leave out arguments that have default values

They are useful - indeed implicitly required - when calling methods with optional parameters - because when you call a method of with optional parameters you must specify the ones you want to pass, otherwise you have to provide the whole list up to the last one you want to use.

Given a method like this:

public void Do(Thing thing, bool doItTwice = false, bool logResults = false,
               string something = null, bool makeTeaAfterwards = false)

You then must use named parameters to avoid having to specify the whole list:

Do(thing, makeTeaAfterwards: true);

Rather than:

Do(thing, false, false, null, true);

The latter also has the disadvantage that you must replicate the defaults, which introduces the possibility of error.

Named Arguments in C++, PHP 8: named arguments It was a close call, but named arguments — also called named parameters — are supported in PHP 8! In this post I'll discuss their ins and outs, but let me show you first what they look like with a few examples in the wild: setcookie ( name: 'test', expires: time + 60 * 60 * 2, );

I'm not sure, but I think you have misunderstood named parameters.

Please see: http://www.dotnetperls.com/named-parameters

Basically, they are useful when you've a lot of parameters to send to a method. With named parameters you can be sure of which parameters you are sending to the method

Method1(test1: 1, ..., test20: 10);

You should use it careful, as it has a substantial performance drawback.

An Argument for Named Parameters, 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. A few languages use named parameters but still require the parameters to be provided in a specific order.

Named Parameters allow developers to pass a method arguments with parameter names. Prior to these this feature, the method parameters were passed using a sequence only. Now, using named parameters in C#, we can put any parameter in any sequence as long as the name is there.

Passing Arguments by Name. Alternatively, you can call Display with the arguments passed by name, also delimited by commas, as shown in the following example: StudentInfo.Display(age:=19, birth:=#9/21/1998#, name:="Mary") Passing arguments by name in this way is especially useful when you call a procedure that has more than one optional argument.

  • MSDN has a good example.
  • there useful because your naming your values, rather than doSomething(true) which has no meaning for what true is, whats true??? with named params, this is clearly stated
  • Foo,bar and baz are poor names if you are talking about the benefits of named Arguments ;)
  • .. although in this example, having two methods MarkAsDone and MarkAsSkipped removes the possibility of ambiguity.
  • @stuartd it introduces a different ambiguity, however it is smaller. Of course, this presumes you can change the called method, which is often not the case.
  • @Sklivvz Nice example, although stuartd has a good point, considering performance hit, if any
  • Minor edit: "A part from that" should be "Apart from that". But SO 6-char-limit doesn't let me do the change. :-( [Let's delete this comment afterwards.]
  • Are named arguments maintenance friendly though? What happens if the parameter name is changed on the method without updating the caller? Does this add more overhead to method signatures, especially in APIs?
  • +1 for real world example, I can see benefit. Looks addictive :) But IMO, I would put enum here, feels safer somehow.
  • Isn't this what XML documentation is for?
  • The XML documentation is not typically rendered when looking at a place where the method is being called. And, if you're using a tool other than an IDE (like looking at a diff when reviewing a colleague's change), you probably can't quickly look at the XML documentation.