Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the rules for named arguments and why?

Consider a method like this

void RegisterUser(string firstname, string lastname, int age);

I like explicitly naming the arguments of methods like this when I call them because it's easy for someone to mix up the firstname and lastname arguments. However, it's not really necessary for age. For instance, I would think this should be OK from a clarity standpoint.

RegisterUser(firstname: "John", lastname: "Smith", 25);

But the following error would be thrown:

Named argument specifications must appear after all fixed arguments have been specified

Another interesting thing is that if the signature were

void RegisterUser(int age, string firstname, string lastname);

then calling it as follows does NOT throw an error

RegisterUser(25, firstname: "John", lastname: "Smith");

Why is C# designed like this? Is there a complication for the compiler if the first scenario were allowed?

like image 678
Frank Bryce Avatar asked Dec 30 '15 19:12

Frank Bryce


People also ask

What is the purpose of argument what is a named argument?

Named arguments enable you to specify an argument for a parameter by matching the argument with its name rather than with its position in the parameter list. Optional arguments enable you to omit arguments for some parameters. Both techniques can be used with methods, indexers, constructors, and delegates.

What is named argument in Python?

Keyword arguments (or named arguments) are values that, when passed into a function, are identifiable by specific parameter names. A keyword argument is preceded by a parameter and the assignment operator, = . Keyword arguments can be likened to dictionaries in that they map a value to a keyword.

What is a named argument in R?

Arguments are always named when you define a function. When you call a function, you do not have to specify the name of the argument. Arguments are optional; you do not have to specify a value for them.

What is named argument in PHP?

Named arguments allow passing arguments to a function based on the parameter name, rather than the parameter position. This makes the meaning of the argument self-documenting, makes the arguments order-independent and allows skipping default values arbitrarily.


1 Answers

the compiler might be able to figure it out but for us mere humans it would be nearly impossible to know if 25 refers to the 1st or 3rd parameter. Especially since it opens up the posibility of mixing arguments. why not

MyFunction(firstname: "josh", 25, "smith", someotherargument: 42)

How would you interpret this, 25 for age and smith for lastname? make a rule for it and a compiler can implement it. But what would make sense to humans. Code obfuscation shouldn't be that easy

A language should make it hard to make errors, not easier

NOTE: strange things start happening with the ordering if earlier arguments are named later. (like the firstname & smith in my example) because then becomes a puzzle for your unnamed arguments to be mapped to the right arguments. it could be done, but code shouldn't produce puzzles

like image 138
Batavia Avatar answered Sep 23 '22 00:09

Batavia