In this chapter, we will learn about comments in the dart, and why it is useful.
Dart Comments
Comments are texts that are ignored by the program. This is generally useful because this helps in making code easier to read and understand.
There are three kinds of comments that dart supports.
- Single-line Comments
- Multi-line Comments
- Documentation Comments
Single-line Comments
Single line comments in dart start with double slash ( // ). For example,
void main() {
// this is the single line comment
print(‘Hello World!);
}
Here,
// this is the single-line comment
is a comment. This line is ignored by the compiler.
These are used to provide short insights into the specific code line.
Double-line Comments
Double-line comments start with /* and end with */. Anything between them is considered as comments and ignored by the compiler.
For example,
void main() {
/* this is the multi line comment
hello world program
*/
print(‘Hello World!);
}
Here,
/* this is the multi-line comment
hello world program
*/
is a multi-line comment. These are generally used to explain the flow and process of the code.
Documentation Comments
Documentation comments start with three slashes. For example,
void main() {
/// this is the documentation comment
/// hello world program
print(‘Hello World!);
}
Here,
/// This is the documentation comment
/// hello world program
is the documentation comments.
The document comments are used for documenting the project or a package.
Why use Dart Comments?
When we write comments, it will be easier to understand the code in the future. It will also help other developers to understand the code, and what it does when they look at the comments.
However, that being said, the code should be well-structured and follow the best practices and the comment should just help in understanding what the particular code does.
Next chapter, We will learn about Dart Data Types.