(Swift for Beginners)
Swift Comments
In this article, you will learn about Swift comments, why and how to use them.
A comment is a programmer-readable explanation or annotation in the source code of a program. It is ignored by the compiler when the code is compiled.
Comments are intended for person reading the code to better understand the intent and functionality of the program. It can be helpful while working on a team to clarify the purpose of the code for other team members or while working solely it can act as a reminder to own.
Swift Comment and its Types
In swift there are two types of comments:
1. Single Line Comment
In Swift, any line starting with two slashes //
is a single line comment.Everything starting with two slashes //
is 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 slashes //
and provides meaningful explanation of the file as “: Playground – noun: a place where people can play”.
Example 1: Single Line Comment
//Sample program
//stores 3.14 in variable pi
let pi = 3.14159
print(pi)
The above example contains two single line comment:
// Sample program // stores 3.14 in variable pi
2. Multiline Comment
If your comment includes multiple line, you can enclose it inside /*...*/
.
Multiline comments start with a forward-slash followed by an asterisk (/*
) and end with an asterisk followed by a forward-slash (*/
). Everything in between /*
and */
is ignored by the swift compiler.
/* This is a multiline comment. After you write multiline comment add * following by / to end it */
Example 2: Multiline comment
/* Hardcoded pi value may be less accurate.
So you can calculate using in built data types for more accurate value
*/
let pi = Double.pi
print(pi)
The above example contains a multiline comment.
/* Hardcoded pi value may be less accurate. So you can calculate using in built data types for more accurate value */
Things to remember
Although comments are meaningful to better understand the intent of the code written. Here are things you need to consider while writing it:
- Do not enclose comments with // on multiline although it is valid and compiler ignores those lines. Instead enclose it inside multiline comment
/*...*/
Example://This is a comment. //Use it when necessary and precisely
The above way to write comment is correct but not recommended because you need to write multiline comments if the comment is greater than one line. The better way to write is using multiline comment as:
/* This is a comment. Use it when necessary and precisely */
- Single line comment can be written in a separate line or along with code in the same line. However, it is recommended to use comments in a separate line.Example:
let pi = 3.14159 //stores 3.14 in variable pi
This way to write comments is valid. But it’s better to write comment in a separate line as:
//stores 3.14 in variable pi let pi = 3.14159
- Even if you are a single developer in a team, and you are the only one writing the code, if it’s uncommented, you will have a hard time trying to figure its purpose in the program. So, use it precisely and provide a meaningful description.
- Make comment very simple and meaningful.
- Do not write unnecessary comments on your code.
- In most cases, use comments to explain ‘why’ rather than ‘how’.
Swift programming for Beginners – Swift Comments
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.