About Comments - Images: Japanese
Hey there, everyone!
Next up, let's talk about comments and commenting out.
'Comments' are a feature found in pretty much every programming language. Adding a comment, or turning something into a comment, is called "commenting out." So 'comment' is the noun, and 'comment out' is the verb.
Let's start by taking a fresh look at what a comment actually is.
The syntax for comments varies across languages, but one thing they all share is this: anything marked as a comment is never executed as code.
That's why comments are used for things like jotting down quick notes, writing explanations for a block of logic, or temporarily disabling code you don't want to run right now.
With that said, let's go over Swift's comment syntax. First up: the single-line comment, sometimes called a line comment. Take a look at the sample below.
// This is a single-line comment.
Notice the // at the start of the line. In Swift, writing // causes everything after it on that same line to be treated as a comment.
If we added a single-line comment to the Hello world program from earlier, it would look something like this.
print("Hello world") // This is the 'Hello world program'.
One thing to watch out for: since it's a single-line comment, anything on the lines that follow is not treated as a comment.
// This is a comment. This is NOT a comment. // This is a comment. This is NOT a comment.
Make sure you don't mix that up.
Next, let's look at comments that span multiple lines — commonly known as 'block comments'. They look like this.
/* Everything from here all the way down here is a comment. */
It's wrapped in /* and */. Anything enclosed between /* and */ is treated as a comment. Pretty straightforward.
One thing to keep in mind when using block comments: Swift allows nested block comments.
What does that mean? In most other languages, you're not allowed to put /* and */ inside another /* and */ — so Swift is actually a bit unusual compared to the norm.
In the majority of other languages, the first '*/' the parser encounters is treated as the end of the comment, so everything after it is no longer considered a comment — and you'll get an error.
/* In most other languages, */ <-- the comment is considered to end right here. So this part would NOT be treated as a comment. */
In Swift, however, the parser correctly identifies the outermost */, so the following sample runs without any errors.
/* /* /* /* /* In Swift, this is totally fine. (・ω<) */ */ */ */ */
This is a pretty unusual spec compared to most other languages, so it's worth keeping in mind — especially if you're coming from another language.
And that covers everything about comments!
In the next article, we'll get into strings and escape sequences. That's all for now — see you next time!
This article was written by Sakurama.
Author's beloved small mammal |
桜舞 春人 Sakurama HarutoA Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him. |
If you find any errors or copyright issues, please contact us.