Skip to content

Commit d61c67d

Browse files
authored
Merge pull request #117 from 1RedOne/patch-1
Document OTBS, add examples of opening / closing braces
2 parents 3644d26 + c0b6284 commit d61c67d

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,28 @@ end{}
8181

8282
You can always delete or ignore one of the blocks (or add the `begin` block), add parameters 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.
8383

84-
#### Open braces on the same line
85-
Code folding is nicer in many editors.
86-
(TODO: This is in discussion in [#24](https://github.com/PoshCode/PowerShellPracticeAndStyle/issues/24))
84+
#### Brace yourself: Follow the one-true-brace style.
85+
Open braces always go on the same line.
86+
87+
This style really won in the PowerShell community partly because the style is one of two used in C languages --it's a variant of the K&R (Kernighan and Ritchie) style from their book The C Programming Language-- but also because for the first few years of PowerShell's existence, this was the only style that could be typed at the command line.
88+
89+
Code folding is nicer in many editors when a scriptblock is placed on the end of the same line, as in this example.
90+
91+
````
92+
function Get-Noun {
93+
end {
94+
if ($Wide) {
95+
Get-Command | Sort-Object Noun -Unique | Format-Wide Noun
96+
} else {
97+
Get-Command | Sort-Object Noun -Unique | Select-Object -Expand Noun
98+
}
99+
}
100+
}
101+
````
102+
#### Closing braces start a new line
103+
Note the above example again, community guidelines recommend following the ['One-True-Brace-Style'](https://www.wikiwand.com/en/Indentation_style#/K&R_style) placing your closing braces on their own line. This practice makes it easier to pair up matching opening and closing braces when looking to see where a particular scriptblock ends, and allows one to insert new lines of code between any two lines.
87104

88-
#### Closing braces always on their own line
89-
Because that's how they're supposed to be!
90-
(TODO: This is in discussion in [#24](https://github.com/PoshCode/PowerShellPracticeAndStyle/issues/24))
105+
To reiterate, these are community best practices, and a lot of the code you'll find online from community leaders will follow these guidelines. That doesn't mean that those who follow different style guidelines are wrong. You may be the one to set the course for your company or your own project; we simply offer this guidance for your consideration.
91106

92107
#### Prefer: param() begin, process, end
93108
That's the order PowerShell will execute it in

0 commit comments

Comments
 (0)