Skip to content

Add new about_Comments document #11646

@surfingoldelephant

Description

@surfingoldelephant

Prerequisites

  • Existing Issue: Search the existing issues for this repository. If there is an issue that fits your needs do not file a new one. Subscribe, react, or comment on that issue instead.
  • Descriptive Title: Write the title for this issue as a short synopsis. If possible, provide context. For example, "Document new Get-Foo cmdlet" instead of "New cmdlet."

PowerShell Version

5.1, 7.4, 7.5

Summary

I was surprised to find little documentation on PowerShell comments when searching for a document to link a new PS user to.

Aside from the language specification, there doesn't appear to be any formal documentation on comments in general, despite it being an important aspect of PowerShell (or any language). There are documents on specific functionality that utilizes comments, but not comments as a general concept.

I propose creating a new about_Comments document that serves as an overview for PowerShell comments and lists/links to special-cased functionality.

Proposed Content

  • Formalize terminology for PowerShell's comment types.

    • The language specification uses "single line comment" and "delimited comment", albeit the latter is not widely recognised.
    • Throughout PowerShell's documentation, comments are inconsistently referred to as (amongst others): "inline comment"/"single comment"/"single line comment"/"single-line comment" and "block comment"/"comment block"/"delimited comment".
    • One possible formalization:
      • "Single-line comment" for # comments.
      • "Block comment" for <# #> comments that span multiple lines.
      • "Single-line block comment" for <# #> comments that start/end on the same line.
  • Discuss general use cases for comments and good practices (e.g., comment the "why" of code).

    • Single-line block comments are useful for placeholders or to comment(-out) portions of a line without interrupting the rest of the line. For example:
      # Comment out an element of an array.
      'Foo', <#'Bar', #>'Baz'
      
      # Add a placeholder without breaking code.
      param (
          [string] $P1,
          <# Insert type #> $P2
      )
  • Note that block comments cannot be nested. In the example below, Baz is not part of the comment.

    <# 
    Foo
    <# Bar #>
    Baz
    #>
  • List functionality that utilizes special-cased comments:

  • List string/token-enclosed comments recognised by PowerShell/.NET:

    • Regex comments ((?#) and #):
    • JSON comments (// and /* */):
      • Supported by ConvertFrom-Json and Invoke-RestMethod in PS v6+, but not Test-Json.
    • CSV comments (#):
      • Supported by all CSV cmdlets.
    • # comments in ConvertFrom-StringData's input string.

Proposed Content Type

About Topic

Proposed Title

about_Comments

Related Articles

https://learn.microsoft.com/en-us/powershell/scripting/lang-spec/chapter-02#223-comments
https://learn.microsoft.com/en-us/powershell/scripting/lang-spec/chapter-15#b12-comments
https://devblogs.microsoft.com/powershell/block-comments-in-v2

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-aboutArea - About_ topicsissue-doc-ideaIssue - request for new content

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions