English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about Swift comments, why and how to use them.
Comments are readable explanations or annotations by programmers in the source code of the program. The compiler ignores them when compiling the code.
Comments are there to help readers of the code better understand the intent and functionality of the program. It can be very helpful when working in a team to clarify the purpose of the code for other team members, or it can serve as a reminder to oneself when working alone.
Swift has two types of comments:
In Swift, any line starting with two forward slashes // All lines starting with it are single-line comments, all with two forward slashes // All lines starting with it are ignored by the compiler.
You can find a simple example at the top of the playground:
//: Playground - noun: a place where people can play
It starts with two forward slashes // Starting with it, and providing meaningful explanations for the file, such as: "Playground" - noun: a place where people can play.
//Sample program //stores 3.14 in variable pi let pi = 3.14159 print(pi)
The above example includes two single-line comments:
// Sample program // stores 3.14 in variable pi
If your comments contain multiple lines, you can enclose them in/*...*/.
Multiline comments start with a forward slash (/*) followed by an asterisk and a forward slash (*/) is ignored by the Swift compiler./* and */All the content between these.
/* This is a multiline comment. Add a space after you write multiline comments*, followed by/to end it */
/* The hardcoded value of pi may not be as precise. Therefore, you can use built-in data types to calculate more accurate values */ let pi = Double.pi print(pi)
The above example includes multiline comments.
/* The hardcoded value of pi may not be as precise. Therefore, you can use built-in data types to calculate more accurate values */
While comments are very meaningful for better understanding the intent of the code written, it is important to note that:
Do not include comments within multiline comments.//It is valid, but the compiler ignores these lines. Instead, it should be placed in a multiline comment./* ... */in
For example:
//This is a comment. //Use it when necessary and precisely
The method of writing comments mentioned above is correct, but it is not recommended to do so because if the comment is longer than one line, it needs to be written as a multi-line comment. A better way to write is to use multi-line comments as:
/* This is a comment. Use it when necessary and precisely */
Single-line comments can be written on a separate line, or they can be written on the same line as the code. However, it is recommended to use comments on separate lines.
For example:
let pi = 3.14159 //stores 3.14 in variable pi
This method of writing comments is effective. However, it is better to write comments on a separate line:
//stores 3.14 in variable pi let pi = 3.14159
Even if you are the only developer in a team and the only one writing code, it is still difficult to find the use of the code in the program without comments. So, use it accurately and provide a meaningful description.
Make comments very simple and meaningful.
Do not write unnecessary comments on the code.
In most cases, use comments to explain 'why' rather than 'how'.