Comments in represent text that is useful to the programmer or developement team. Think of them as notes or reminders to yourself of important concepts or ideas that need a little explaining above the syntax of the raw code.
In Swift there are two kinds of comments: single-line comments and multi-line comments. These comments will be completely ignored by the compiler at compile-time.
Famous software engineer Martin Fowler once wrote,
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”
In Swift, as in many other languages, single line comments are prefaced with two consecutive forward slashes (
//). They don’t have to be their own line, single line comments can be appended at the end of a line to give more context.
// I am a single-line comment!print("Hello World")
A single-line comment can also be used to comment after a line of code:
print("Hello World") // Me, too!
Multi-line comments allow for greater context and readability of a concept or idea that your program is doing. It is also commonly used for metadata information like authorship and copyright information. In Swift, multi-line comments are made opened with
/* and closed with
/*This space can be used to write notes to our future selves about ideas that we wantto implement and just haven't or if our algorithm isn't easily legible throughthe code, we can explain it here. This is especially important if we are partof a team separated by time and space and communication occurs here instead of an office.*/
- Anonymous contributors
Learn Swift on Codecademy
- Anonymous contributorsAnonymous contributors