Naming variables

Using the right name for a variable is important. There are some conventions and some syntactical rules to name variables properly. Variables have to be one word, they have to start with a letter or underscore, and all remaining characters must be a letter, a number, or an underscore. If these rules are not followed, the program will produce an error.

There are two best practices to take into account when choosing a variable name. First, the variable name should reflect its function in the program. This makes it easier to keep track of how the program functions. Second, if the variable name consists of several words, these should be separated using either camel case or snail case to make it easier to read.

In camel case, the first word starts with a lowercase letter, while all the following words start with an uppercase letter. In snail case, each word is lowercase and is separated by an underscore. It is best practice to use either one or the other for a given program and stick with it, though this can get tricky, especially if multiple people are working on the same project.

Variables are a key part of what makes programming an effective means of expression. Storing and labeling data can make it easier to reference and keep track of, but it also makes it possible to build more sophisticated operations. This will become clear when I introduce compound statements later in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset