Some naming rules reference in PowerShell programming _powershell

Source: Internet
Author: User
Tags function definition

This article describes best practices for PowerShell custom functions when they are in use (top practices). This includes naming the function name, optional parameters, required parameters, and so on.

The function definition in PowerShell can be defined very simply by using a form of "function < functions name >{". But Microsoft recommends that you follow the following practice rules when creating functions.

1, function name.

Use the Uniform command Rule of the cmdlet, which is the form of "verb-noun". such as Write-host, indicates output to the host. If you are getting a list, do not use list as a verb and suggest to use get, such as Get-childitem. The noun part uses a meaningful English word, remember to use the singular, do not use the plural form of the noun. If you want to list a function of the NIC, do not use listnetworkcards, instead use Get-networkcard. This is the best practice for naming a function.

2, the company prefix.

Assuming you write a get-networkcard function, Hongo also writes the same one. Is it hard to tell who wrote it? Therefore, Microsoft recommends that the noun part of the function name plus the company prefix, but not the company prefix function, we can generally understand the release of Microsoft. The company prefix is written with two to three letters appropriate, such as the Hongo function name can be life for Get-hgnetworkcard. Here Hongo the prefix with the HG, what about yours?

3, the name of the parameters to be standardized

Arguments need to use meaningful words, do not use-pc as parameter names, you can replace them with-computername. If you sometimes need a file path as a parameter, do not use-file, because the reference is too vague. You can use-path to make it clearer. Although there is no official name for a standard parameter named list, we should refer to those parameter names that are used by the built-in (built-in) cmdlets. It's easier to understand when other people are using it, right?

4. Optional Parameters

It is a good practice to define a default value for optional parameters. If the user regardless of this parameter, then this parameter can also be in the assigned state.

5. Required Parameters

Although it is not required to specify a data type when defining function parameters in PowerShell, the best practice requires that we do so. If we specify a type, the user's input will be cast to the data type we defined, otherwise it will be treated as a string.

6, write some comments to help explain

A section of the function to others, no annotation, it is difficult to understand others. Even their own, without annotating the code, time is too long, not necessarily guaranteed to understand, right?

About the best practice of using PowerShell functions (top practices), this article on the introduction of so many, I hope to help you, thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.