A naming convention is a shared pattern for constructing identifiers so their form communicates what they contain and how they are used. Good names preserve intent even when a declaration and its use are far apart, helping future readers understand code without reconstructing its context. section 2.1.1
Useful conventions make both individual names and larger structures easier to read:
- Name variables for their contents and roles: table, row_index, column_index, and item reveal more than single-letter alternatives. section 2.1.1
- Use plural names for iterable collections and singular names for items drawn from them, such as employees and employee. section 2.1.1
- Make totals explicit by combining total with what is counted, as in total_employees or total_managers. section 2.1.1
- Apply consistent parts-based names when a value needs more detail, such as first_name, middle_name, and last_name. section 2.1.1
From shorthand to intent
A nested loop becomes easier to follow when t, i, j, r, and it become table, row_index, column_index, row, and item. The same principle applies to files: when every expense report must be named expenses.xlsx, directories can provide the meaningful separation and hierarchy. section 2.1.1 section 2.1.2
table = [[12, 11, 4], [3, 22, 105], [0, 47, 31]]
for row_index, row in enumerate(table):
for column_index, item in enumerate(row):
process_item(row_index, column_index, item)
Compare a nested loop using descriptive names for the table, row, column, and item. section 2.1.1
