GCP1 - Selecting “good” variable names#

This file is adopted with modifications from GCP 1 - Selecting “good” variable names by Geo-python

Binder

Throughout the course we will learn about some good coding practices

1. Some “not-so-good” variable names#

To illustrate the point, consider a few not-so-good examples below.

s = "440"

or

sid = "440"

Any idea what these variables are for?

Let’s look at another example.

floridaautomatedweathernetworkobservationstationidentificationnumber = "440"

The above variable provides more information
but in a format not easy to read, nor something you want to type more than once.

2 Selecting “good” variable names#

A good variable name should:

  1. Be clear and concise.

  2. Be written in English.

  3. Not contain special characters. Stick to the standard printable ASCII character set to be safe.

  4. Not conflict with any Python keywords, such as for, True, False, and, if, or else. These are reserved for speical operations in Python and cannot be used as variable names.

With this in mind, let’s now look at a few better options for variable names.

3. Formatting “good” variable names#

There are several possibilities for “good” variable name formats, of which we’ll consider two:

Recommendation 1: pothole_case_naming#

pothole_case_naming uses lowercase words separated by underscores _. This is our suggested format as the underscores make it easy to read the variable, and don’t add too much to the length of the variable name. As an example, consider the variable temp_celsius.

fawn_station_id = "440"

Here, our new variable name conveys all of the essential information we need, while remaining easy to read.

Recommendation 2: camelCase naming#

camelCase or CamelCase uses capitalization of the first letter of words in a variable name to make it easier to read. In some cases the first letter of the variable may be capitalized. The variable tempFahrenheit was one example of camelCase.

fawnStationID = "440"
stationID = "440"

Again, this variable name is clear and easy to read.

pothole_case_naming is the easiest to read and seems to be most common amongst Python programmers.

For your homework, project, and exam, you can use either option, as long as you are consistent in the use.