You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Style-Guide/Documentation-and-Comments.md
+33-37Lines changed: 33 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
### Documenting and Comments
2
2
3
-
Comments that contradict the code are worse than no comments. Always make a priority of keeping the comments up-to-date when the code changes!
3
+
Comments that contradict the code are worse than no comments. Always make a priority of keeping the comments up-to-date when the code changes!
4
4
5
-
Comments should be in English, and should be complete sentences. If the comment is short, the period at the end can be omitted.
5
+
Comments should be in English, and should be complete sentences. If the comment is short, the period at the end can be omitted.
6
6
7
7
Remember that comments should serve to your reasoning and decision-making, not attempt to explain what a command does. With the exception of regular expressions, well-written PowerShell can be pretty self-explanatory.
8
8
@@ -14,28 +14,27 @@ $Margin = $Margin + 2
14
14
# Maybe write:
15
15
# The rendering box obscures a couple of pixels.
16
16
$Margin = $Margin + 2
17
-
18
17
```
19
18
20
19
#### Block comments
21
20
22
21
Don't go overboard with comments. Unless your code is particularly obscure, don't precede each line with a comment -- doing so breaks up the code and makes it harder to read. Instead, write a single block comment.
23
22
24
-
Block comments generally apply to some or all of the code which follows them, and are indented to the same level as that code. Each line should start with a # and a single space.
23
+
Block comments generally apply to some or all of the code which follows them, and are indented to the same level as that code. Each line should start with a # and a single space.
25
24
26
25
If the block is particularly long (as in the case of documentation text) it is recommended to use the `<# ... #>` block comment syntax, but you should place the comment characters on their own lines, and indent the comment:
27
-
26
+
28
27
```PowerShell
29
-
# Requiring a space makes things legible and prevents confusion.
30
-
# Writing comments one-per line makes them stand out more in the console.
28
+
# Requiring a space makes things legible and prevents confusion.
29
+
# Writing comments one-per line makes them stand out more in the console.
31
30
32
-
<#
33
-
.SYNOPSIS
34
-
Really long comment blocks are tedious to keep commented in single-line mode
35
-
.DESCRIPTION
31
+
<#
32
+
.SYNOPSIS
33
+
Really long comment blocks are tedious to keep commented in single-line mode.
34
+
.DESCRIPTION
36
35
Particularly when the comment must be frequently edited,
37
-
as with the help and documentation for a function or script
38
-
#>
36
+
as with the help and documentation for a function or script.
37
+
#>
39
38
```
40
39
41
40
#### Inline comments
@@ -47,16 +46,16 @@ They should be separated from the code statement by at least two spaces, and ide
47
46
```PowerShell
48
47
$Options = @{
49
48
Margin = 2 # The rendering box obscures a couple of pixels.
50
-
Padding = 2 # We need space between the border and the text
51
-
FontSize = 24 # Keep this above 16 so it's readable in presentations
49
+
Padding = 2 # We need space between the border and the text.
50
+
FontSize = 24 # Keep this above 16 so it's readable in presentations.
52
51
}
53
52
```
54
53
55
54
#### Documentation comments
56
55
57
-
Comment-based help should be written in simple language.
56
+
Comment-based help should be written in simple language.
58
57
59
-
You're not writing a thesis for your college Technical Writing class - you're writing something that describes how a function works. Avoid unecessarily large words, and keep your explanations short. You're not trying to impress anyone, and the only people who will ever read this are just trying to figure out how to use the function.
58
+
You're not writing a thesis for your college Technical Writing class - you're writing something that describes how a function works. Avoid unnecessarily large words, and keep your explanations short. You're not trying to impress anyone, and the only people who will ever read this are just trying to figure out how to use the function.
60
59
61
60
If you're writing in what is, for you, a foreign language, simpler words and simpler sentence structures are better, and more likely to make sense to a native reader.
62
61
@@ -78,17 +77,17 @@ Every script function command should have at least a short statement describing
78
77
79
78
##### Document Each Parameter
80
79
81
-
Each parameter should be documented. To make it easier to keep the comments synchronized with changes to the parameters, the preferred location for parameter documentation comments is _within_ the `param` block, directly above each parameter.
80
+
Each parameter should be documented. To make it easier to keep the comments synchronized with changes to the parameters, the preferred location for parameter documentation comments is _within_ the `param` block, directly above each parameter.
82
81
Examples can be found in the ISE snippets:
83
82
84
-
```powershell
83
+
```PowerShell
85
84
Param(
86
85
# Param1 help description
87
86
[Parameter(Mandatory = $true,
88
87
ValueFromPipelineByPropertyName = $true,
89
88
Position = 0)]
90
89
$Param1,
91
-
90
+
92
91
# Param2 help description
93
92
[int]
94
93
$Param2
@@ -101,16 +100,16 @@ It is also possible to write `.PARAMETER` statements with the rest of the docume
101
100
102
101
Your help should always provide an example for each major use case. A 'usage example' is just an example of what you would type in to Powershell to run the script - you can even cut and paste one from the command line while you're testing your function.
103
102
104
-
105
103
```PowerShell
106
104
function Test-Help {
107
105
<#
108
106
.SYNOPSIS
109
-
An example function to display how help should be written
107
+
An example function to display how help should be written.
108
+
110
109
.EXAMPLE
111
110
Get-Help -Name Test-Help
112
111
113
-
This shows the help for the example function
112
+
This shows the help for the example function.
114
113
#>
115
114
[CmdletBinding()]
116
115
param(
@@ -125,17 +124,14 @@ function Test-Help {
125
124
}
126
125
```
127
126
128
-
129
-
130
-
131
127
### DOC-01 Write comment-based help
132
128
133
129
You should always write comment-based help in your scripts and functions.
134
130
135
131
Comment-based help is formatted as follows:
136
132
137
133
```PowerShell
138
-
function get-example {
134
+
function Get-Example {
139
135
<#
140
136
.SYNOPSIS
141
137
A brief description of the function or script.
@@ -144,34 +140,34 @@ function get-example {
144
140
A longer description.
145
141
146
142
.PARAMETER FirstParameter
147
-
Description of each of the parameters
143
+
Description of each of the parameters.
148
144
Note:
149
-
To make it easier to keep the comments synchronized with changes to the parameters,
150
-
the preferred location for parameter documentation comments is not here,
145
+
To make it easier to keep the comments synchronized with changes to the parameters,
146
+
the preferred location for parameter documentation comments is not here,
151
147
but within the param block, directly above each parameter.
152
148
153
149
.PARAMETER SecondParameter
154
-
Description of each of the parameters
150
+
Description of each of the parameters.
155
151
156
152
.INPUTS
157
-
Description of objects that can be piped to the script
153
+
Description of objects that can be piped to the script.
158
154
159
155
.OUTPUTS
160
-
Description of objects that are output by the script
156
+
Description of objects that are output by the script.
161
157
162
158
.EXAMPLE
163
-
Example of how to run the script
159
+
Example of how to run the script.
164
160
165
161
.LINK
166
-
Links to further documentation
162
+
Links to further documentation.
167
163
168
164
.NOTES
169
-
Detail on what the script does, if this is needed
165
+
Detail on what the script does, if this is needed.
170
166
171
167
#>
172
168
```
173
169
174
-
Comment-based help is displayed when the user types `help get-example` or `get-example -?`, etc.
170
+
Comment-based help is displayed when the user types `help Get-Example` or `Get-Example -?`, etc.
175
171
176
172
Your help should be helpful. That is, if you've written a tool called `Get-LOBAppUser`, don't write help that merely says, "Gets LOB App Users." Duh.
0 commit comments