Comments (2)
As a general rule: use the native capitalization for type names. The easiest implementation of that is to use the tab-completion value. This assumes you're using the built-in tab-completion...
Normally, that results in PascalCase, but for accelerators it's lowercase. Here's a few examples you're probably familiar with:
Key Value
--- -----
array System.Array
bool System.Boolean
double System.Double
float System.Single
single System.Single
guid System.Guid
hashtable System.Collections.Hashtable
int System.Int32
int32 System.Int32
int16 System.Int16
long System.Int64
int64 System.Int64
string System.String
The reason for this is rooted in the way C# has "native" types which must be written in lowercase. However, in PowerShell they're not native types, they're just accelerators (aliases). Most of the accelerators that were added originally in Windows PowerShell were all lowercase.
There are some built-in accelerators which were added later that are not full lowercase (personally, I prefer to type these in all lowercase too, to help remember what they are). If you run PowerShell -NoProfile
you can get a list of them like this:
<# PS C:\ #> [psobject].assembly.
gettype("System.Management.Automation.TypeAccelerators")::
get.GetEnumerator().
Where{ $_.Key -cmatch '^[A-Z]' -and $_.Value.Name -notmatch 'Attribute$' } |
Sort Key
Key Value
--- -----
CimSession Microsoft.Management.Infrastructure.CimSession
IPEndpoint System.Net.IPEndPoint
NullString System.Management.Automation.Language.NullString
ObjectSecurity System.Security.AccessControl.ObjectSecurity
PhysicalAddress System.Net.NetworkInformation.PhysicalAddress
WildcardPattern System.Management.Automation.WildcardPattern
X500DistinguishedName System.Security.Cryptography.X509Certificates.X500DistinguishedName
X509Certificate System.Security.Cryptography.X509Certificates.X509Certificate
ExperimentAction System.Management.Automation.ExperimentAction
ExperimentalFeature System.Management.Automation.ExperimentalFeature
The last two of those were added in PowerShell 7.1 ...
In addition to those, all of the attributes are written with PascalCase.
Since accelerators can be changed, and since you can always leave off the System
namespace (intentionally or inadvertently), I like to be slightly more consistent than "whatever tab completion outputs". If you're like me, here are the detailed rules:
- Use
using namespace
if you want to skip entering the full name. - Prefer the full name and native capitalization of the type (which is always PascalCase,)
- Don't leave off System
- You can check the
.FullName
property of the type.
- Always use PascalCase for attributes
- Use full lowercase when using an accelerator
The attributes we use frequently in PowerShell are all short names (think of CmdletBinding
, Parameter
, Alias
, and the Validate*
attributes). Those are always entered uniformly PascalCase, matching the original case of the type, so I just stick to that for attributes.
If we're consistent, we will be reminded by the [lowercasetype]
that we're dealing with an accelerator, and everything else has the proper full name, which will match what's in docs.microsoft.com, etc., except attributes, which are always accelerators.
Obviously the only thing that differs from the initial answer are those 8-10 classes that aren't attributes but were accelerated with PascalCase anyway. These types are pretty rarely used, so most of the time you won't be able tell the difference. Having said that, the easiest way to achieve consistency is to just recreate the non-uniform accelerators the way that you want them, and always use the built-in tab completion, here's the snippet you need for that 😁
$xlr8 = [psobject].assembly.gettype("System.Management.Automation.TypeAccelerators")
@($xlr8::get.GetEnumerator()).Where{ $_.Key -cmatch '^[A-Z]' -and $_.Value.Name -notmatch 'Attribute$' }.ForEach{
$null = $xlr8::remove($_.Key)
$null = $xlr8::add($_.Key.ToLower(), $_.Value)
}
Of course, since PowerShell is not C#, and these aren't actually native types, you could hypothetically choose to achieve consistency the other way: rewrite all the accelerators to use the native PascalCase names. That would not match the normal community conventions, but I think that if you wanted to make that the rule within a project or an organization, it wouldn't be too hard to enforce.
from powershellpracticeandstyle.
Wow, thank you so much for this awesome detailed explanation 🎉
So If I get this right, the common way would be:
- Use lowercase for basic types provided via accelerators ( like.
string
,int
,bool
) - Use PascalCase as standard for all other Types/ Objects. (
[PSSCustomObject]
,[MailAddress]
) - Use PascalCase for common used Attributes like
[CmdletBinding()], [ValidateNotNullOrEmpty()]
from powershellpracticeandstyle.
Related Issues (20)
- Question: Best Practice fot HOT 3
- Indentation advice is bad/wrong HOT 9
- Constant variables placement with in a script. HOT 1
- Is it better to use $PSItem vs $error[0] or $_ HOT 2
- Add Set-Variable vs. $foo = "bar" best practice HOT 2
- Correct formatting HOT 5
- Unquoted strings HOT 3
- Indentation on Comment-Based help HOT 5
- Consider revising PERF-02 HOT 1
- Naming-Conventions refers to a broken link
- Guidance on External Module Dependencies HOT 3
- Suggestion - explain why the second command in PERF-01 is faster
- Broken link and sentence fragment on naming conventions page
- Module structure HOT 4
- Avoid Using Semicolons (`;`). HOT 3
- Parameter Comments and Get-Help HOT 2
- Begin/Process/End or Process only. No Process/End, Begin/End, Begin/Process. HOT 5
- Indentation for lines after a pipe character HOT 3
- How to deal with a module that contains multiple interdependent classes? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from powershellpracticeandstyle.