Passing function parameters by name

Janice Caron caron800 at googlemail.com
Wed Dec 5 23:45:51 PST 2007


On Dec 5, 2007 1:48 PM, renoX <renosky at free.fr> wrote:
> I could ask the opposite question: why should we restrict the API to the function and object name instead of using the full data that we have?

I often write constructor functions which look something like this

    this(int x_, int y_, int z_)
    {
        x = x_;
        y = y_;
        z = z_;
    }

If parameter names were part of the API, then anyone calling my code
would have to refer to those constructor parameters as x_, y_ and z_,
complete with trailing underscores

Of course, I could go back and change all my code to

    this(int x, int y, int z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

but that would be a lot of code to change, and it might break calling code!










> To answer your question: passing parameter by name increase the readability of the source, reduce the number of mistake when writing the code (and with a good IDE the number of character to type would be the same).
>
>
> > Named arguments are potentially a disastrous feature, if completely
> > unrestricted. It was when COM needed to support VB's named arguments that
> > Windows programming really nose-dived.
>
> Could you explain this point? (I know nothing about COM).
>
> > (OTOH: A string mixin can, given the name of a function, tell you what the names
> > of all of it's default arguments are (as well as what their default values are).
> > I can in fact write a string mixin implementation of this feature; it's
> > perfectly feasible. But is the concept actually a good idea?)
>
> Note that passing parameter by name is only useful if programmers doesn't have to jump through hoops to do it, otherwise nobody will use it.
>
> renoX
>



More information about the Digitalmars-d mailing list