powershellfunctionsemanticsnamingplural

Should function parameters be named as a plural or singular object?


This is probably a dumb question so forgive me. I've used many cmdlets that have a parameter that is named as a singular object, yet it accepts multiple objects.

This makes sense to me for the parameter to be named singularly. For example:

Process-VM -VMName

However, I'm writing my own function that can do the same; take a singular object or a collection, but I'm having trouble with semantics of naming the parameter singularly.

For example, my function accepts one or more virtual machines. Normally with a collection like this I'd use a foreach loop to enumerate through them, and it would look like

foreach ($vm in $VMNames)

But if I write the parameter singularly it would look something like:

foreach ($vm in $VMName)

For the foreach to make sense to me, my parameter would have to be named plurally but then the function call looks silly to me when I'm passing a single object. Ofc this also deviates from those other well-known cmdlets.

Process-VM -VMNames "myvm"

I feel silly having this problem, but I'm curious how this is handled by someone like Microsoft and their cmdlets.

TIA


Solution

  • If you're asking for official guidelines then you can checkout Strongly Encouraged Development Guidelines. More specifically for the question being asked, Use Singular Parameter Names:

    Avoid using plural names for parameters whose value is a single element. This includes parameters that take arrays or lists because the user might supply an array or list with only one element.

    Plural parameter names should be used only in those cases where the value of the parameter is always a multiple-element value. In these cases, the cmdlet should verify that multiple elements are supplied, and the cmdlet should display a warning to the user if multiple elements are not supplied.