It improves the readability of the code for other programmers as well as for yourself, allowing you to provide information on how complex things are handled in the program for deeper understanding and maintenance. We’ll explore the different types of comments Dart supports in this article.”
Different Type of Comment in a Dart in Ubuntu 20.04?
In Dart, you can make comments in a variety of ways as follows:
Single-line Documentation comments.
Multi-line Documentation comments.
How to Use the Comment in the Dart in Ubuntu 20.04?
Between the dart statements, you can add comments wherever in the program. The compiler/interpreter just dismisses the comments in our code, resulting in no change to the program’s final output. In a nutshell, comments serve as a brief, comprehensible explanation for specific code. Note that comments should not be used to explain badly written code. Always attempt to produce clear, readable code before adding comments. We have different comment types used in the dart script as follows:
Example # 1: Using the Single Line Comment in a Dart in Ubuntu 20.04
A single-line comment is expressed with the double forward-slash (//) and contributes substantially to the newline character. A single-line comment in Dart is used to comment on a line until it is broken.
We have our first single-line comment used in the dart main section of the code. The single-line comment has the “Area” formula, which we have used in the variable “Area.” To clear the readers, we have used Area’s formula here. Then, we again use a single-line comment to demonstrate what’s happening next in the print function.
You can notice that the single-line comment text is ignored here by the dart compiler; only the “Area” is displayed, which we have passed inside the print function.
Example # 2: Using the Multi-line Comment in a Dart in Ubuntu 20.04
Multi-line comments might be employed when you need to add comments to many lines. Dart Multiline comments are used to comment out entire sections of code. It starts and ends a multi-line comment with “/*” and “*/,” respectively. Between the section of multiline comment “ /* and */,” the compiler ignores everything. On the other hand, a multi-line comment cannot be wrapped inside another multi-line comment.
Now, we have a representation of the multiline comment in the main method of the above dart program. We have used the multiline comment for giving the details that we are going to create an integer list with the list syntax in dart. Then, we have constructed a variable “List” that contains the number of the lists with the dart list representation. Again, we have used multiline comments for detailing the print function. The list will be printed on the screen using the print function.
Upon interpretation, we have only a list as an output. The multiline comment text is not displayed on the screen. It is eliminated by default by the compiler.
Example # 3: Using the Doc Single Line Comment in a Dart in Ubuntu 20.04
Documentation comments are a type of comment that is normally used to produce documentation or a resource for a task or software product. A documentation comment is a single-line or multi-line comment with the /// or /* characters at the start.
You can use a doc single-line comment on successive lines, which is the same as a multi-line comment. The Dart interpreter excludes these lines except for those put inside the curly brackets. Triple slash or triple forward slash can be used in a single-line Doc comment.
After creating the main method, we have used three forward slashes for the documentation single-line comment. The doc single-line comment is used for the declaration of the variable. This is the property of doc single-line comment used before the code’s variables, function, or class definition. The variable “string1” is declared, initializing the string value. Then, we have six lines of the documentation single-line comment before the print function. We can use as many single-line comments in the code. But we have to use the single-line comments for the valid information in the code so that the clarity of the code is not compromised.
Only the statement inside the print function is displayed on the screen. The doc single-line comments are displayed on the shell.
Example # 4: Using the Doc Multiline Line Comment in a Dart in Ubuntu 20.04
These characters “**/**…*/**” are used to make multi-line Doc comments. A multi-line Doc remark is shown below. The following example explains using documentation multiline comments in the dart code.
Inside the main method, the doc multiline comments are used with the backslash character “/” and the double-asterisk character “**.” We have used the forward-slash character only once. This means we entered the comment section and used the asterisk to create doc further multiline comments. Then, the doc multiline comments ended with an asterisk character and the forward-slash character “/.” After that, we have a statement in the print function displayed only on the screen.
The doc multiline comments are not used for execution purposes. They are ignored by the compiler, as shown on the shell screen.
Although we consider ourselves programmers, most of the text in a source code is designed for human reading. A few seconds of writing a simple, precise comment can save one of many person-hours. Every programming language supports comments in the specific code for its reader’s convenience. Although, dart language has numerous ways to create comments in the code. We have a general representation of the dart comments with the four examples.