Skip to content

Commit e7f675c

Browse files
authored
Update wording in code-layout and formatting
1 parent bb37adc commit e7f675c

File tree

1 file changed

+45
-35
lines changed

1 file changed

+45
-35
lines changed

Style-Guide/Code-Layout-and-Formatting.md

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Rules about indentation, line length, and capitalization are about consistency a
88

99
We don't expect everyone to follow these guidelines, and rules for individual projects always trump these. Whether for legacy reasons, or to match guidelines for multiple languages in a single project, different projects may have different style guidelines. Since the goal is consistency, you should always abide by any style rules that are in place on the project you are contributing to.
1010

11-
If you do have a legacy project that is in source control and you decide to reformat code to adopt these rules, try to make all of your whitespace changes in a single a commit that does _nothing_ but edit the whitespace. You should never reformat the whitespace on a file as _part_ of a content change because it makes the changes hard to spot.
11+
If you do have a legacy project that is in source control and you decide to reformat code to adopt these rules, try to make all of your whitespace changes in a single commit that does _nothing_ but edit the whitespace. You should never reformat the whitespace on a file as _part_ of a content change because it makes the changes hard to spot.
1212

1313
#### Capitalization Conventions
1414

@@ -67,34 +67,38 @@ A special case is made for two-letter acronyms in which both letters are capital
6767
6868
If you wish, you may use camelCase for variables within your functions (or modules) to distinguish _private_ variables from parameters, but this is a matter of taste. Shared variables should be distinguished by using their scope name, such as `$Script:PSBoundParameters` or `$Global:DebugPreference`. If you are using camelCase for a variable that starts with a two-letter acronym (where both letters are capitalized), both letters should be set to lowercase (such as `adComputer`).
6969

70-
#### Open braces on the same line
70+
#### One True Brace Style
7171

72-
This can be considered a matter of consistency; several common cmdlets in PowerShell take script blocks as _parameters_ (e.g., `ForEach-Object`), and in these cases it is functionally impossible to place the opening brace on a new line _without_ use of a line-continuator (i.e., ``` ` ```, a backtick), which should generally be avoided.
72+
This guide recommends the so-called ["One True Brace Style" variant to K&R](https://github.com/PoshCode/PowerShellPracticeAndStyle/issues/81#issuecomment-285835313), which requires that every braceable _statement_ should have the opening brace on the _end of a line_, and the closing brace at the _beginning of a line_.
7373

74-
```powershell
75-
$Data | ForEach-Object {
76-
$_.Item -as [int]
77-
}
78-
```
79-
80-
v.s.
74+
There is one notable exception when passing small scriptblocks to parameters (where K&R would allow leaving off the braces entirely), we allow putting the entire statement on a single line.
8175

8276
```powershell
83-
foreach ($Entry in $Data)
84-
{
85-
$Entry.Item -as [int]
77+
enum Color {
78+
Black,
79+
White
8680
}
87-
```
8881
89-
As such, both native keywords and function parameters should include opening braces on the _same_ line.
90-
91-
Code folding is also nicer in many editors.
92-
93-
#### Closing Braces Always on Their Own Line
82+
function Test-Code {
83+
[CmdletBinding()]
84+
param(
85+
[int]$ParameterOne
86+
)
87+
end {
88+
if (10 -gt $ParameterOne) {
89+
"Greater"
90+
} else {
91+
"Lesser"
92+
}
93+
}
94+
}
9495
95-
Once again, this makes code-folding much more sensible in many editors.
96+
# An Exception case:
97+
Get-ChildItem | Where-Object { $_.Length -gt 10mb }
98+
```
99+
The primary reason for this recommendation is practical: there are no exceptions necessary when following this rule, and when code is written following this style, _new lines_ of code can be inserted between any two lines with no risk of accidentally breaking the code by separating braces from their statement blocks. Thus, it's easier to follow, and makes errors less likely.
96100

97-
The exception to this rule may be in cases where the script block is a parameter, and further parameters must still be added. However, in the interests of improving code-folding, readability, and maintainability, placing such parameters _before_ the script block parameter should be considered, where possible.
101+
Because this choice was somewhat contentious in the community (about 1/3 of voters opposed), it's worth adding some addition reasonning here: First: in some historical consoles, it was necessary to write this way, so much of the early PowerShell code follows this style anyway. Second: PowerShell functions which accept scriptblocks (such as `ForEach-Object` and `Where-Object`) are common, and an _inherent_ part of the syntax of important PowerShell-based domain-specific languages such as DSC. Since it's **required** to place the opening brace on the end of the line in those cases, the only _consistent_ option is to follow OTBS.
98102

99103
#### Always Start With CmdletBinding
100104

@@ -103,23 +107,25 @@ All of your scripts or functions should start life as something like this snippe
103107
```powershell
104108
[CmdletBinding()]
105109
param()
106-
process {}
107-
end {}
110+
process {
111+
}
112+
end {
113+
}
108114
```
109115

110116
You can always delete or ignore one of the blocks (or add the `begin` block), add parameters and necessary valiation and so on, but you should **avoid** writing scripts or functions without `[CmdletBinding()]`, and you should always at least _consider_ making it take pipeline input.
111117

112118
#### Prefer: param(), begin, process, end
113119

114-
Having a script written in the order of execution makes its intent more clear. There is no functional purpose to having `begin` be declared _after_ `process`. Although it _will_ still be executed in the correct order, writing in such a fashion significantly detracts from the readability of a script.
120+
Having a script written in the order of execution makes the intent clearer. Since there is no functional reason to have these blocks out of order (they _will_ still be executed in the normal order), writing them out of order can be confusing, and makes code more difficult to maintain and debug.
115121

116-
As a general rule, unreadable scripts are also difficult to maintain or debug.
122+
More explicit code is more maintainable. While PowerShell allows leaving off the explicit name of the `end` block (and even has a `filter` keyword that converts the anonymous block to a `process` block), we recommend against using these features as it results in less explicit code.
117123

118124
#### Indentation
119125

120126
##### Use four *spaces* per indentation level
121127

122-
Usually you use the `[Tab]` key to indent, but most editors can be configured to insert spaces instead of actual tab characters when you indent. For most programming languages and editors (including PowerShell ISE) the default is four spaces, and that's what we recommend. Different teams and projects may have different standards, and you should abide by them in the interest of maintaining consistency of style in a given project.
128+
Usually you will press the `[Tab]` key to indent, but most editors can be configured to insert spaces instead of actual tab characters. For most programming languages and editors (including PowerShell ISE) the default is four spaces, and that's what we recommend. Different teams and projects may have different standards, and when contributing to a project, you should abide by the predominant style, of course.
123129

124130
```powershell
125131
function Test-Code {
@@ -145,11 +151,15 @@ function Test-Code {
145151

146152
Limit lines to 115 characters when possible.
147153

148-
The PowerShell console is, by default, 120 characters wide, but it allows only 119 characters on output lines, and when entering multi-line text, PowerShell uses a line continuation prompt: `>>> ` and thus limits your line length to 116 anyway.
154+
Keeping lines to a small width allows scripts to be read in _one_ direction (top to bottom) without scrolling back-and-forth horizontally. What, exactly, this width should be is a one of the favorite arguing points among developers on the internet (more splintered than emacs vs vi or gnu GPL vs MIT).
155+
156+
In this guide we use two particular sources for the maximum line width:
157+
158+
The PowerShell console is, by default, 120 characters wide, but it allows only 119 characters on output lines, and when entering multi-line text, PowerShell uses a line continuation prompt: `>>> ` and thus limits your line length to 116 anyway.
149159

150-
Additionally, keeping lines to a set width allows scripts to be read in _one_ direction (top to bottom) with no horizontal scrolling required. For many, having to scroll in both directions detracts from a smooth reading and comprehension of the script.
160+
Github's current maximum line width varies between 121 and 126 depending on your browser and OS (and thus, font). However, the 115 line length suggested by PowerShell would be enough to even allow side-by-side diffs to be displayed without scrolling or wrapping on the current "standard" 1080p monitor.
151161

152-
Most of us work on widescreen monitors these days, and there is little reason to keep a narrow line width, however, keeping files relatively narrow allows for side-by-side editing, so even narrower guidelines may be established by a given project. Be sure to check when you're working on someone else's project.
162+
Again, this is a particularly flexible rule, and you should always follow the guidelines of projects when you're contributing to other people's pojects. Although most of us work on widescreen monitors, not everyone can see well without magnification or extremely large fonts.
153163

154164
The preferred way to avoid long lines is to use splatting (see [Get-Help about_Splatting](https://technet.microsoft.com/en-us/library/jj672955.aspx)) and PowerShell's implied line continuation inside parentheses, brackets, and braces -- these should **always** be used in preference to the backtick for line continuation when applicable, even for strings:
155165

@@ -210,23 +220,23 @@ $yesterdaysDate = (Get-Date).AddDays(-$i)
210220

211221
#### Spaces around special characters
212222

213-
White-space is (mostly) irrelevant to PowerShell, but its proper use is key to writing easily readable code.
223+
White-space is (mostly) irrelevant to PowerShell, but its proper use is key to writing easily readable code.
214224

215225
Use a single space after commas and semicolons, and around pairs of curly braces.
216226

217-
Avoid unnecessary extra spaces inside parenthesis or square braces.
227+
Subexpressions `$( ... )` and scriptblocks `{ ... }` should have a single space on the _inside_ of the braces or parentheses to improve readability by making code blocks stand out -- and to further distinguish scriptblocks from variable delimiter braces `${...}`
218228

219-
Subexpressions `$( ... )` and script blocks `{ ... }` should have a single space _inside_ the enclosing braces or parentheses to make code stand out and be more readable.
220-
221-
Subexpressions `$( ... )` and variable delimiters `${...}` nested inside strings should not include additional space _surrounding_ them, unless it is desired for the final string to include them.
229+
Avoid unnecessary spaces inside parenthesis or square braces.
222230

223231
```powershell
224232
$Var = 1
225233
"This is a string with one (${Var}) delimited variable."
226234
227-
"This is $( 2 - 1 ) string with $( 1 + 1 ) numbers contained within."
235+
"There are $( (Get-ChildItem).Count ) files."
228236
```
229237

238+
Obviously, these rules should not be applied in such a way as to affect output.
239+
230240
#### Avoid Using Semicolons (`;`) as Line Terminators
231241

232242
PowerShell will not complain about extra semicolons, but they are unnecessary, and can get in the way when code is being edited or copy-pasted. They also result in extra do-nothing edits in source control when someone finally decides to delete them.

0 commit comments

Comments
 (0)