I need help choosing proper names for variables with long actual names. I have read pep8 docs, but I couln't find addressed such issue.
Would you rename very_long_variable_name
to something like vry_lng_var_nm
or would you leave it as it is. I notice that pythons build in libraries have very short names and I'd like to follow the conventions if it exists for this case.
I know I can name it something not very descriptive and add description, which would explain its meaning, but what do you think should be the variables name.
PEP8 recommends short variable names, but achieving this requires good programming hygiene. Here are some advices to keep variable names short.
Variable names are not full descriptors
First, do not think of variable names as full descriptors of their content. Names should be clear mainly to allow to keep track of where they come from and only then can they give a bit about the content.
Put details in comments
Use comments and doc strings for description of what is going on, not variable names.
Too specific name might mean too specific code
If you feel you need a very specific name for a function, it might be that the function is too specific itself.
Notice how, in the previous example, using
grade
as a function argument allowed to remove it from the function name without loss of clarity.Keep short scopes for quick lookup
Finally, encapsulate logic in shorter scopes. This way, you don't need to give that much detail in the variable name, as it can be looked up quickly a few lines above. A rule of thumb is to make your functions fit in your IDE without scrolling and encapsulate some logic in new function if you go beyond that.
Here I lost track of what
names
was and scrolling up will make me lose track of even more. This is better:Spend time thinking about readability
The last but not least is to devote time thinking about how you can make your code more readable. This is as important as the time you spend thinking about the logic and code optimization. The best code is the code that everyone can read, and thus everyone can improve.