Naming convention for actually choosing the words in Python, PEP8 compliant

naming-conventions, python

Solution

I believe that the need for complex variable naming conventions goes away with good object-oriented design. In the Spolsky article, much focus is on how variable naming helps preventing errors. I believe that those errors will more often occur when you have many variables in the same scope; this can be avoided by grouping data into objects - then, a single naming context will have only few variables, which don't need combined names.

The other purpose of a naming convention is to better remember the names. Again, object-orientation helps (by hiding much data from users that look from the outside); what you then need is a convention for naming methods, not data. In addition, tools can help which provide you with a list of names available in a certain scope (again, those tools rely on object-orientation to do their job).

In your specific example, if column is an object, I would expect that `len(table)` gives me the number of columns in a table, `sum(column)` or `column.sum()` gives me its sum; and the current column is just the variable in the for loop (often `c` or `column`).

Problem

I’m looking for a better way to name everything in Python. Yes, I’ve read PEP8, Spolsky’s wonderful rant, and various other articles. But I’m looking for more guidance in choosing the actual words. And yes I know A Foolish Consistency is the Hobgoblin of Little Minds. But, you can keep consistent with PEP8 etc, and still not have consistent variable/method/class names which are easy to remember. By consistent, I mean that if you were presented with the same decision twice, you would produce the same name. As an example, there are multiple PEP8 compliant ways to name the items below: - number of columns in the table - current column number - column object - sum of column Yeah, sure, it is easy to make a decision to use something like `num_col` and `count_col` rather than `col_num` and `col_count` (or v.v.). But, I would like to see an example that has seen some testing/refining over time. I often start with a given convention, and then it starts to break down as I venture into a new area. I guess what I am looking for is not only what the prefix/root/tag/suffix should do (which was partly covered for Apps Hungarian in the Spolsky article), but (many) examples for each, or a rule for generating each.

Original source