Dart - Comments

The comments are notes that developers can add in source code and are ignored by compilers. It’s an art of describing what your code does and may be why. These comments are not evaluated.

It increases the code readability for other developers as well as self that allows to provide information of what tricky things are done within the code for better understanding and maintenance.

In this article, we will look into different types of comments supported by Dart.

The comments in Dart can be done in following different ways:

  • Single-line comments //
  • Multi-line comments /* ... */
  • Single-line Doc comments ///
  • Multi-line Doc comments /** ... */

Single-line comments in Dart

Single line comment can be done by // i.e double slash or double forward slash.

// this is a single-line comment

Multi-line comments in Dart

Multi-line comment also known as block comment. It can be done by /* ... */.

/*
  this is a multi-line comment
  we can also wrap code that is quite useful during debugging or development
*/

Dart guide suggests us to single-line comment //. The block comment (/* ... */) should only be used temporarily to comment out a section of code.

Single-line Doc comments in Dart

Doc comments parsed by dartdoc generates beautiful doc pages from them. A comment is any comment that appears before a declaration is called as Doc comment.

Doc comments also supports Markdown formatting

Single line Doc comment can be done by /// i.e triple slash or triple forward slash.

You can find usage of it in dart:io library available on GitHub

/// this is a single-line comment

Multi-line Doc comments in Dart

Multi line Doc comment can be done by **/** ... \*/**. Below is an example of multi-line Doc comment.

/**
  * ** Important:** Browser-based apps can't use this library.
  * Only the following can import and use the dart:io library:
  *   - Servers
  *   - Command-line scripts
  *   - Flutter mobile apps
  *   - Flutter desktop apps
  */

Markdown formatting is also supported by Doc comments

Dart guide suggests us to single-line doc comment /// over the multi-line Doc comment (/** ... */).

Hope you find this useful!

Keep helping and happy 😄 coding

Avatar

A web geek, an industry experienced web developer & tutor/instructor residing in India 🇮🇳

Previous

Related