Increasing readability
This chapter does not introduce any new functionality of the language. Instead, we will show you ways to improve the readability of your code.
Compound assignment operators
Compound assignment operators are a way to make assignments more concise if you are only applying a single mathematical operation to a variable. You don't have to remember the name (most people don't), however, you should know how they work because they are commonly used throughout programming. Take a look at the following example where the variable a is incremented by one.
Output:
We can make the incrementation more concise by using the += operator to express the same operation as before.
There are corresponding operators for subtraction (-=), multiplication (*=), and division (/=) as well.
Trailing commas
Whenever you encounter a comma-separated series of parameters or fields, you can add a trailing comma. A trailing comma is a comma after the last element of the list, as can be seen in the following example.
Trailing commas are entirely optional; however, they can often increase readability by making your code more streamlined. In case you are already working with a version control system, trailing commas can help produce a more stable history as well. Some code formatting tools also use the presence or absence of trailing commas to determine whether a list of parameters should be placed on separate lines or a single one.
Naming conventions
You can choose any name for your variables and types. However, there are a couple of conventions (or best practices) you should consider sticking with to make your code more streamlined and readable. The standard library and all examples in this course apply these conventions as well.
Both types and variables should be named in something called snake case, which is a naming convention where each compound word of a name is separated by an underscore. Additionally, variables should be kept entirely in lowercase letters, such as name, first_name, or age_group_winner. In contrast, you should capitalize the first letter of every word when naming types, such as Address, User_Record, or Customer_Support_Request. The latter naming convention is also often referred to as capital- or upper snake case.