CS 61A lect6 code standards
CS61A lect6 code standards
Choosing Names
Names typically don’t matter for correctness
but they matter a lot for composition
Names should convey the meaning or purpose of the values to which they are bound.
The type of value bound to the name is best documented in a function’s docstring.
Function names typically convey their effect (print), their behavior (triple), or the value returned (abs)
Which Values Deserve a Name
Repeated compound expressions
Meaningful parts of complex expressions
More Naming Tips
• Names can be long if they help
document your code:average_age = average(age, students)
is preferable to
1 | #Compute average age of students |
• Names can be short if they represent
generic quantities: counts,
arbitrary functions, arguments to
mathematical operations, etc.
n, k, i - Usually integers
x, y, z - Usually real numbers
f, g, h - Usually functions
