
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write Multiple Line Comment in Swift
In this article, we will discuss Swift's comments, why, and how to use them correctly in Swift.
Always keep one thing in mind while writing code i.e.
The code must be written for people to read and understand as much as possible.
Similarly to the Swift language, comments are very useful for making your code more easily understandable to you and other developers. All the comments in the code are completely ignored by the Swift compiler.
There are two types of comments in Swift ?
Single Line Comments
Multiline Comments
Single Line Comments
Syntax
Swift allows us to make short explanations by using single-line comments.
// Write your message here...
Example
We used two forward slashes (//) when writing a single-line comment.
// this function is created to print full name func printFullName(firstName: String, lastName: String) { // print full name here print("\(firstName) \(lastName)") } // calling the function with first and last name printFullName(firstName: "Rose", lastName: "Bush")
Output
Rose Bush
Explanation
The purpose of this function can be easily understood by reading the first line in the above example, which is a single-line comment. This function was created to display the full name.
Multiline Comments
Syntax
Multiline comments start with a forward slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/).
/* You can write a description on multiple lines here. */
Example
The text between /* and */ is multiline comments.
/* Use this function to print the full name. Parameters: firstName: A string value that accepts the first name lastName: A string value that accepts the last name */ func printFullName(firstName: String, lastName: String) { // print full name here print("\(firstName) \(lastName)") } // calling the function with first and last name printFullName(firstName: "Rose", lastName: "Bush")
Output
Rose Bush
Explanation
The purpose and structure of the function have been described before the function definition.
Benefits of Using Comments
Make code easier to understand ? Comments make the code easier to understand for future reference. It is also helpful to other developers.
For Debugging ? Comments are very helpful to identify incomplete or buggy code when debugging. A single comment can be written if your code is incomplete or if you have a problem that needs to be addressed later.
Conclusion
We should always use comments to explain why we did something rather than how we did it. Poorly written code should not be explained through comments.
A comment can tell you why a piece of code works. Remember that you write your code to be consumed by other programmers first, hence understanding the principles of writing high-quality comments contributes to project quality a lot