Our code convention philosophy: Code is far more read than it is written, so we will favor read-time convenience to write-time convenience.
These conventions are based on Simon McConnell’s framework for code conventions from the book Code Complete.
TBD
English Language when naming variables, functions, classes and comments
Use descriptive language when naming functions/variables. They should be self-explainatory.
#BAD
mxpl = 7
current = datetime.datetime.now()
def date():
return current#GOOD
max_people = 7
current_date = datetime.datetime.now()
def getCurrentDate():
return current_datelet camelCaseVariable = "This looks good"{
"camelCaseVariable" : "This looks good"
}snake_case_variable = "This looks good!"This is numbers directly in the functions that serve a specific value.
# BAD
if password_length > 7:
print("Password has to be shorter")# GOOD
MAX_PASSWORD_LENGTH = 7
if password_length > MAX_PASSWORD_LENGTH:
print("Password has to be shorter")Never access other classes variables directly.
// BAD
class Person {
constructor(name) {
this.name = name;
}
}
kent = new Person('kent')
kentsName = kent.name
kent.name = 'Eline'//GOOD
class Person {
constructor(name) {
this.name = name;
}
//This is encapsulation
getName() {
return this.name
}
setName(name) {
this.name = name
}
}
kent = new Person('kent')
kentsName = kent.getName()
kent.setName('Eline')UPPERCASE_LETTERS with snake_case
MAX_PASSWORD_LENGTH = 99Use positive boolean names
#BAD
notFound = true
#GOOD
found = trueTypical boolean names:
#To indicate whether something is done
done = true
isDone = true
#to indicate whether an error has occured
error = false
hasError = false
#to indicate whether a value has been found
found = true
isFound = true
#To indicate whether an operation has been successful
success = true
ok = false