Member-only story
In Python, when we look at a piece of code, the first thing we notice is not the number of loops or the patterns used, but the variables and comments. This is because they are the elements in the code that are closest to natural language and the easiest for the brain to digest and understand.
For example, if you take a look at the following code, can you explain what it does?
# Remove spaces in t and assign to v
v = update(t.strip())
You may conclude:
- Calling
strip()
ont
, sot
is probably a Pythonstr
here update()
as name implies, it will updatet
- The result of
update()
is assigned tov
, but what doesv
mean here?
But with the same piece of code, if I slightly adjust the variable names and add a bit of commentary, it will become entirely different:
# Remove spaces from product name input
product_name = extract_name_from_input(input_string.strip())
Now how does the new code read? Does the intent of the code become much easier to understand? This is the magic of variables and comments.
From the computer’s perspective, a variable is a marker used to locate something in memory. Comments are similar; the computer doesn’t care if your comments are…