DIP 1030--Named Arguments--Community Review Round 1 Discussion

Jonathan Marler johnnymarler at gmail.com
Wed Feb 12 21:11:12 UTC 2020


On Wednesday, 12 February 2020 at 19:44:52 UTC, SashaGreat wrote:
> On Wednesday, 12 February 2020 at 07:42:32 UTC, aliak wrote:
>> Named arguments is the difference between spending 1 second 
>> looking at this:
>>
>> makeWindow(x: 67, y: 98, width: 100, height: 100)
>>
>> Or taking about 1 to 5 minutes depending on how easy 
>> documentation is to find, and how bored you are to figure out 
>> what this is doing:
>>
>> makeWindow(67, 98, 100, 100)
>
> You're using an example in favor of a point, because it's hard 
> to someone use constants for this kind of thing for a 
> production code, at least for obvious reasons it shouldn't.
>
> Now let's compare:
>
>> makeWindow(x: 67, y: 98, width: 100, height: 100)
>
> With:
>
> makeWindow(x, y, width, height);
>

You could use variables to make it look nicer at the call-side, 
but the downside is there's no guarantee that those variable 
names have any relation to the names of the parameters. For 
example, this would still compile:

makeWindow(x, width, y, height);

This problem can't happen with named arguments.  Named arguments 
also saves you if the function arguments change names/meaning, 
i.e.

ORIGINAL: void makeWindow(int x, int y, int width, int height)
MODIFIED: void makeWindow(int left, int top, int right, int 
bottom);

The version that doesn't use named arguments will still compile 
and run:

makeWindow(x, y, width, height);

But named arguments would catch the problem at compile-time:

makeWindow(x: 67, y: 98, width: 100, height: 100); // error no 
argument named 'x', 'y', 'width', 'height'



More information about the Digitalmars-d mailing list