Start Coding

YAML Comments

Comments in YAML (YAML Ain't Markup Language) are essential for adding explanations, notes, or temporarily disabling code within your YAML files. They enhance readability and maintainability of your configurations.

Single-Line Comments

YAML supports single-line comments using the hash symbol (#). Anything following the # on the same line is treated as a comment and ignored by YAML parsers.

# This is a single-line comment
key: value # This is an inline comment

Multi-Line Comments

While YAML doesn't have a built-in multi-line comment syntax, you can achieve a similar effect using the YAML Block Scalars feature with a comment indicator.

comment: |
  # This is a multi-line comment
  # It can span multiple lines
  # And is useful for longer explanations

Best Practices

  • Use comments to explain complex configurations or non-obvious choices.
  • Keep comments concise and relevant to maintain clarity.
  • Update comments when modifying related code to avoid outdated information.
  • Use inline comments sparingly to prevent cluttering your YAML structure.

Comments in Different YAML Structures

Comments can be used with various YAML structures, including YAML Lists and YAML Dictionaries.

# List with comments
fruits:
  - apple    # Red or green
  - banana   # Yellow
  - cherry   # Red

# Dictionary with comments
person:
  name: John Doe  # Full name
  age: 30         # In years
  city: New York  # Current residence

Commenting Out Code

Comments are also useful for temporarily disabling parts of your YAML configuration without deleting them.

database:
  host: localhost
  port: 5432
  # username: admin  # Commented out for local development
  # password: secret # Commented out for security reasons

Considerations

When working with YAML comments, keep these points in mind:

  • Comments are not preserved when YAML Parsing and re-generating YAML.
  • Be cautious with comments in sensitive areas, as they might expose confidential information.
  • Some YAML parsers may have limitations or special handling for comments.

Understanding YAML comments is crucial for creating well-documented and maintainable YAML files. They play a vital role in YAML for App Config and various other YAML use cases.