Naming Conventions
When choosing names for items in your program, Python doesn't care what you use so long as you avoid the reserved words. But a good naming convention can help to produce source code that is easy to read and understand. This section describes the naming convention that should be followed for the course. Camel Case Notation.
Unless otherwise indicated, the names of all multi-word identifiers must use the camel case notation. That is, the first letter of each word, except possibly the first, should be capitalized. For example: Identifier Length.
All identifiers should be of sufficient length to provide a descriptive name.
You do not have to create excessively long names, but shorter names including single character ones are typically not very descriptive. Note, that some single letter variable names can be used when appropriate to the given problem. Examples include loop variables (i.e. Variable NamesVariables and constants should have meaningful names that relate to their contents. The names for all user-defined variables should:
response, firstName, currentEntry, listLength All non-trivial constant values in a program should be assigned meaningful names that:
MAX_FRIENDS, PI, TAX_RATE, LOWER_LIMIT, MAX_LINE_LENGTH Constants often describe a limit within the program. In these cases, it is appropriate to use the prefix Function NamesFunctions should have descriptive meaningful names that relate to their purpose; The name should give some idea as to what the function does. As with variables, the names for all user-defined functions should:
getEntry(), displayError(), isEmpty(), findName(), printCalendar() There are any number of conventions that can be used when naming functions. One common approach is to include a verb at the beginning of the name to express the implied action ("do this"). printAddress(), getLength(), findMaxValue(), isFull() Class NamesThe simplest and shortest names should be reserved for class names. Classes should be named with short, generic nouns that reflect their contents. They should start with a capital letter and follow the camel case notation if the name consists of multiple words. Bag, Date, FileReader, Histogram, SearchTree, HashTable Some classes may be private to the module in which they are defined. In which case, they are only meant to be used within that module. In this case, the class name should be preceded with a single underscore. _ArrayIterator, _ListNode, _TreeNode While this does not prevent a programmer from importing the class, it is a simple notation to help flag the improper use of a private class. Data AttributesData attributes generally should not be accessible from outside the class itself. Most object-oriented programming languages provide a mechanism to hide or protect the data attributes from outside access in order to enforce the abstraction provided by the class. Python does not provide a means to protect attributes to prevent their misuse. But we can use our own notation to flag an attribute as being private to the given class. We then have to trust the user of the class to not attempt a direct access to the private attributes. The names of all private data attributes should:
self._julianDay, self._xCoord, self._yCoord. For naming data attributes of storage classes, see the special section on storage classes. Class MethodsUser-defined Methods. The naming scheme used for user-defined methods should follow that of user-defined functions as described above. Operator Methods. Some methods define special Python operators, in which case you must use the name prescribed by Python. The naming convention used by Python for all of the operator methods consists of two underscores preceding the operator name and two underscores following the name. __add__(), __mul__(), __lt__(), __float__(), __str__() Helper Methods. Some classes may also include helper methods. Helper methods are like any other method, but they are commonly used in the implementation of a class to allow for the subdivision of a larger method into smaller parts or to reduce code repetition. Helper methods are not meant to be used outside the class in which they are defined. Thus, the naming scheme used for helper methods should follow that used with private data attributes (precede their name with a single underscore). _getIndex(), _isValidDate(), _reduceToNormalForm() ModulesThe names used for Python modules and program files should be descriptive but not excessively long. Python source files will be named with a
Naming HintsThe most important criterion when choosing a name is: how easily can another programmer (not just yourself) understand the program? If understanding a name was not important, we could just name all variables
if listIsEmpty : addItem( item )
|