Android Programming with Kotlin for Beginners
上QQ阅读APP看书,第一时间看更新

More on code comments

As you become more advanced at writing Kotlin programs, the solutions that you use to create your programs will become longer and more complicated. Furthermore, as we will see in later chapters, Kotlin was designed to manage complexity by having us divide up our code into separate classes, usually across multiple files.

Code comments are a part of the Kotlin files that do not have any function in the program execution itself; that is, the compiler ignores them. They serve to help the programmer to document, explain, and clarify their code to make it more understandable to themselves later, or to other programmers who might need to use or change it.

We have already seen a single-line comment:

// this is a comment explaining what is going on

The preceding comment begins with the two forward slash characters, //. The comment ends at the end of the line. So, anything on that line is for people only, whereas anything on the next line (unless it's another comment) needs to be syntactically correct Kotlin code:

// I can write anything I like here
but this line will cause an error

We can use multiple single-line comments, as follows:

// Below is an important note
// I am an important note
// We can have as many single line comments like this as we like

Single-line comments are also useful if we want to temporarily disable a line of code. We can put // in front of the code and it will not be included in the program. Refer back to this code, which tells Android to load our layout:

// setContentView(R.layout.activity_main)

In this situation, the layout will not be loaded, and the app will have a blank screen when run as the entire line of code is ignored by the compiler.

Note

We saw this in Chapter 5, Beautiful Layouts with CardView and ScrollView when we temporarily commented out one of the lines of code in a function.

There is another type of comment in Kotlin known as the multiline comment. The multiline comment is useful for longer comments that span across multiple lines and for adding things such as copyright information at the top of a code file. Like the single-line comment, a multiline comment can be used to temporarily disable code; in this case, usually across multiple lines.

Everything in between the leading /* character and the ending */ character is ignored by the compiler. Take a look at the following examples:

/*
   You can tell I am good at this because my
   code has so many helpful comments in it.
*/

There is no limit to the number of lines in a multiline comment; the type of comment that is best to use will depend upon the situation. In this book, I will always explain every line of code explicitly in the text, but you will often find liberally sprinkled comments within the code itself that add further explanation, insight, or context. So, it's always a good idea to read all the code thoroughly:

/*
   The winning lottery numbers for next Saturday are
   9,7,12,34,29,22
   But you still want to make Android apps?
*/

Tip

All the best programmers liberally sprinkle their code with comments!