Naming rules and conventions
Contents
7. Naming rules and conventions#
Author: Tue Nguyen
There are only two hard things in Computer Science: cache invalidation and naming things.
(Phil Karlton)
7.1. Overview#
When writing a program, we often have to give names to many things
Ex: variables, functions, classes, and modules
Thus, it is crucial to learn the rules and conventions
Rules are specifications that we MUST follow
Conventions are more like suggestions of best practices by experienced programmers and are widely accepted by the community. Thus, we are not required to follow, but we SHOULD.
For now, we only focus on variable names. Naming for other things is similar
7.2. Naming rules#
There are 3 rules when naming a variable
Can consist characters from 3 following groups only
Digits (
0-9
)Latin letters (
a-z
andA-Z
)Underscores (
_
)
Cannot start with a digit
Cannot coincide with one of Python’s reserved words
Reserved words or keywords are words that have special meaning to Python (see the list below)
False class finally is return
None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise
Notes
For now, you don’t have to remember all of these keywords. You will remember them as you progress
Python is case-sensitive, meaning that it distinguishes between lowercase and uppercase. Thus,
firstname
,FirstName
, andFIRSTNAME
are different
a) Ex 1: valid names
age
num_jobs
file_name
_xyz
b) Ex2: invalid name
warnings! # contains a special character !
4oceans # starts with a digit
break # coincides with break keyword
7.3. Naming convention#
Here are some best practices to follow when naming
Use all lowercase
Ex:
name
instead ofName
orNAME
Follow
snake_case
conventionWord are separated by
_
so that the name looks like a snakeEx:
gross_profit
,first_name
,top_score
One exception: class names should follow
CamelCase
Start each word in the name with a capital letter followed by lower letter so that the name looks like a camel with humps
Ex:
Person
,BankAccount
Should be meaningful and easy to remember
Ex:
interest_rate
instead ofr
orir
Should have a reasonable length
Ex:
sales_apr
instead ofsales_data_for_april
Avoid names of popular functions and modules
Ex: avoid
print
,math
, orcollections
a) Ex 1: names having good styles
first_name
cum_gpa
class_rank
b) Example 2: names having bad styles
fn # ambiguous
FirstName # not snake case
print # name of a popular function
os # name of a popular module
sys # name of a popular module
salary_data_of_10000_households # too long
7.4. Summary#
Naming rules
Can consist characters from 3 following groups only
Digits (
0-9
)Latin letters (
a-z
andA-Z
)Underscores (
_
)
Cannot start with a digit
Cannot coincide with one of Python’s reserved words
Naming convention
Use all lowercase
Follow
snake_case
conventionOne exception: class names should follow
CamelCase
Should be meaningful and easy to remember
Should have a reasonable length
Avoid names of popular functions and modules