Believe me when I say that your own code can become difficult to understand even after only a few hours later. Especially when you don’t follow the correct naming convention which I explain later, you can wonder for hours what your older self meant by this or that code.
That’s why it’s especially important to always describe what your code does and why. For this reason, it’s useful to leave a note or reminder for your future self or even for other developers so they don’t need to decode your code.
Swift and basically all programming languages have comments that explain what a particular code is doing.
There are two types of comments in Swift:
A single-line comment will comment out a single line of code and is denoted by two forward slashes // preceding it:
// This code prints "Hello" print("Hello")
You can also use a single line comment after a line of code:
print("Hello")// This code prints "Hello"
A multi-line comment will comment out multiple lines and is denoted by /* at the beginning of the comment, and */ at the end of the comment:
/* This is all commented out.
None of this is going to run! */
Now is your turn! Go ahead and comment out the second line of your code. Then, comment out the whole code we wrote so far. This is what it looks like:
Notice two things here. First, you can have a single-line comment inside the multi-line comment. Second, once you run the code, the console gets empty because there’s nothing to compile and run since everything is commented out.