Skip to content

Commit 3644d26

Browse files
authored
Merge pull request #105 from AWahlqvist/add-unary-operator-section
Added example for unary operators and whitespace
2 parents 20f46f9 + b2665bb commit 3644d26

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Lines should not have trailing whitespace. Extra spaces result in future edits w
156156

157157
You should use a single space around parameter names and operators, including comparison operators and math and assignment operators, even when the spaces are not necessary for PowerShell to correctly parse the code.
158158

159-
One notable exception is when using colons to pass values to switch parameters:
159+
A notable exception is when using colons to pass values to switch parameters:
160160

161161
```PowerShell
162162
# Do not write:
@@ -166,6 +166,25 @@ $variable=Get-Content $FilePath -Wai:($ReadCount-gt0) -First($ReadCount*5)
166166
$variable = Get-Content -Path $FilePath -Wait:($ReadCount -gt 0) -TotalCount ($ReadCount * 5)
167167
```
168168

169+
Another exception is when using [Unary Operators](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators#unary-operators):
170+
171+
```PowerShell
172+
# Do not write:
173+
$yesterdaysDate = (Get-Date).AddDays( - 1)
174+
175+
$i = 0
176+
$i ++
177+
178+
# Instead write:
179+
$yesterdaysDate = (Get-Date).AddDays(-1)
180+
181+
$i = 0
182+
$i++
183+
184+
# Same principle should be applied when using a variable
185+
$yesterdaysDate = (Get-Date).AddDays(-$i)
186+
```
187+
169188
#### Spaces around special characters
170189

171190
White-space is (mostly) irrelevant to PowerShell, but its proper use is the key to writing easily readable code.

0 commit comments

Comments
 (0)