PS: Coding Style

less than 1 minute read

Description:

When it comes to Powershell, there are two main styles that people will follow (actually three, but the third is just like one of them):

To Resolve:

  1. Styles:

    • OTBS
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    function Get-Noun {
       end {
          if($Wide) {
                Get-Command | Sort-Object Noun -Unique | Format-Wide Noun
          } else {
                Get-Command | Sort-Object Noun -Unique | Select-Object -Expand Noun
          }
       }
    }
    
    • Stroustup: Same as OTBS but will place “else” in a new line.

    • Allman

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    function Get-Noun
    {
       end
       {
          if($Wide)
          {
                Get-Command | Sort-Object Noun -Unique | Format-Wide Noun
          }
          else
          {
                Get-Command | Sort-Object Noun -Unique | Select-Object -Expand Noun
          }
       }
    }
    

References

“Where to put braces #24”
“PowerShellPracticeAndStyle”

Comments