views:

42

answers:

1

I was splitting RenameFolder to two pieces and i notice visual studios 2010 supports named parameters! (example below).

I know this has existed for a number of years in other languages. I remember a professor saying why he likes named parameters and that he uses them in all of his code. But i am sure its not in all of his code. I was wondering.

When should i consider to write the function using a named parameter vs normal style (func(a,b,c)). Some cases area (without a when and when not suggestion)

  • Calling public methods in the same class
  • Calling private methods in the same class
  • Calling methods in external 3rd party libraries
  • Calling methods in another class in the same namespace
  • Calling methods in another class in a different namespace or module
  • Calling methods from internal classes not meant to be a library

    public bool RenameFolderIn(PK folderId, string newfoldername)
    {
        return RenameFolder(newfoldername: newfoldername, infolder: true, folderId: folderId);
    }
     public bool RenameFolderOut(PK folderId, string newfoldername)
    {
        return RenameFolder(newfoldername: newfoldername, infolder: false, folderId: folderId);
    }
    public bool RenameFolder(PK folderId, string newfoldername, bool infolder)
    {
    
+2  A: 

Typically, I use named parameters when there are a large number of default values and I only need to specify a few non-default, or when the function name doesn't suggest the order of parameters. In the example RenameFolder* functions, I would expect the folder to come before the new name (RenameFolder can be short for the phrase "rename folder folder to name"; phrasing it so the name comes first, if possible, isn't the obvious approach), and so wouldn't bother with named parameters.

Example: suppose Gamma is a constructor for the Gamma distribution, which hase two parameters: shape and scale. There's a statistical convention for passing shape before scale, but the convention isn't obvious from the name, so we use named parameters.

waitTime = Gamma(shape: 2, scale: 2)
outis