When we have multiple commands, each piping their output to one another, for readability / single-line width it's generally best to put each command on a different line; like so:
Write-Verbose'Before the stuff in the piped code'$myArrayOfThings|Where-Object {$_.SomeText-like'pattern*'} |ForEach-Object {[PSCustomObject]@{
MyText=$_.SomeText-replace'^pattern',''AnotherProperty=$_.SomethingElse
}}
Write-Verbose'No longer in the piped code'Should we define a best practice / guideline for this scenario?
Generally I indent all lines after the initial pipe to show they're a continuation of the previous line. However I don't indent again after subsequent lines because there would be no additional benefit.
The downside with this approach is that there's no "closing line"; which looks nasty if you the following line is a close for a parent block; e.g.
if ($someFlag) {
$myArrayOfThings|Where-Object {$_.SomeText-like'pattern*'} |ForEach-Object {[PSCustomObject]@{
MyText=$_.SomeText-replace'^pattern',''AnotherProperty=$_.SomethingElse
}}
} # here we 're now have 8 spaces between this line's indentation level and the preceding line's.An option to tidy this up could be to put a comment to represent the close of he piped "block" / maybe with something after the comment character to show it's a close for the piped input; e.g.
if ($someFlag) {
$myArrayOfThings|Where-Object {$_.SomeText-like'pattern*'} |ForEach-Object {[PSCustomObject]@{
MyText=$_.SomeText-replace'^pattern',''AnotherProperty=$_.SomethingElse
}}
# |
}Another option could be to put parentheses around the block.
if ($someFlag) {
($myArrayOfThings|Where-Object {$_.SomeText-like'pattern*'} |ForEach-Object {[PSCustomObject]@{
MyText=$_.SomeText-replace'^pattern',''AnotherProperty=$_.SomethingElse
}}
)
}Do others have any thoughts or preferences on how best to approach these scenarios?
When we have multiple commands, each piping their output to one another, for readability / single-line width it's generally best to put each command on a different line; like so:
Should we define a best practice / guideline for this scenario?
Generally I indent all lines after the initial pipe to show they're a continuation of the previous line. However I don't indent again after subsequent lines because there would be no additional benefit.
The downside with this approach is that there's no "closing line"; which looks nasty if you the following line is a close for a parent block; e.g.
An option to tidy this up could be to put a comment to represent the close of he piped "block" / maybe with something after the comment character to show it's a close for the piped input; e.g.
Another option could be to put parentheses around the block.
Do others have any thoughts or preferences on how best to approach these scenarios?