Pretty please: Named arguments

Tomek Sowiński just at ask.me
Mon Feb 28 11:35:52 PST 2011


Steven Schveighoffer napisał:

> Although I am not strongly for named arguments, I think they would be a  
> definite improvement.
> 
> Bearophile brought up one of the strongest cases for them:
> 
> foo(int width, int height) {}
> 
> Seems simple enough, I don't see how you have "too many arguments", but  
> the call looks like this:
> 
> foo(123, 456);
> 
> So, looking at this call, can you tell which is width and which is  
> height?  I've seen some libs that use width and height do height first  
> also.  I usually have to go look up the API every time I'm reading/writing  
> one of these.
> 
> But this is perfectly clear and resists API changes/differences:
> 
> foo(width: 123, height: 456);
> 
> The cool part about this is, named arguments are not required -- you can  
> always just not use them.  But when you do use them, the code becomes much  
> clearer.

The classic beneficiary of named parameters is the widely understood user interface. Presentation code (layout, formatting, graphics) is naturally awash in options and switches.

Take construction of std.format.FormatSpec as a next-door example.

-- 
Tomek



More information about the Digitalmars-d mailing list