Basics

Crystal Comments

Crystal Comment Syntax

Crystal comments use # for single-line annotations.

Introduction to Crystal Comments

Comments are essential in programming for documenting code, explaining the logic, and making the code more understandable for others or your future self. In Crystal, comments are straightforward and use the # symbol for single-line annotations.

Single-Line Comments

Single-line comments in Crystal are created by placing a # symbol before the text of the comment. Everything after the # on that line will be considered a comment and ignored by the Crystal compiler.

Here's an example of how to use single-line comments in Crystal:

Using Comments for Documentation

Comments can be used to provide documentation for your code. This is especially useful for explaining complex logic or providing insights into the purpose of certain code blocks. Here's an example:

Best Practices for Writing Comments

While comments are useful, they should be written with care. Here are some best practices:

  • Be concise: Write comments that are short but informative.
  • Stay relevant: Ensure comments are related to the code they annotate.
  • Keep updated: When code changes, update comments accordingly to prevent misinformation.
  • Avoid obvious comments: Do not comment on code that is self-explanatory.
Previous
Loops