In this last Lecture you wrote your first line of code. Now we need to start getting into more interesting topics. In this lecture, we will be talking about comments. I like to think of comments like this: I imagine myself working on a piece of art with other artists around the world. Then I got stuck on the problem of how I will share important details about what I have just added to this artwork without confusing the next artist. The same thing happens when you are collaborating with people on a project. You will always need to provide detailed information about your code so that the next human who reads through will be able to make sense of it. Dart like most other programming languages allows you to document your code through the use of comments. This allows you to write any text which will be ignored by the compiler alongside your code. In Dart, commenting can be done in three ways:
Single-Line Commenting ( // )
Multi-Line Commenting ( /**/ )
Documentation Commention ( /// )
Single Line Commenting (//): as the name implies is used when you comment on just a single line of your file. For example:
void main(List<String> args){
This is not a comment
// This is a Single Line comment
Single Line Comments do not extend to the next line
}
But in some cases, you want to add many lines of comment because it makes your comment more human-readable. For this, some people will decide to use multiple single-line comments.
void main(List<String> args){
// This is a Single Line comment
// This is another Single Line comment
// This is the third Single Line comment
}
But Dart provides us with another Syntax for this (Multi-Line Comment).
void main(List<String> args){
/* This is another Multi Line comment
It extends to the next line.
*/
}
This way, you can write more lines of comment and express yourself more clearly without loosing the purpose of writing comments in the first place - clarity. The third way of commenting Dart provides us with is (Documentation Commenting). This is very helpful for people who write codes that are accessed on public domains. For example:
void main(List<String> args){
add(2,3);
}
///This Function returns the sum of two numbers
int add(int firstNumber, int secondNumber){
return firstNumber+ secondNumber;
}
Some functions and classes in Dart programming language won't be easily understood if they weren't properly documented. By the way, you don't need to worry about the codes you just typed, the purpose of this lecture is to expose you what commenting is, what it does and how it is used in Dart programming language.