Use descriptive variable names
func computeTime(daysElapsed):
The first word in a function name should be a verb, in a class it should be a noun
Avoid vague comments
# TODO: add timezones
setTime()
if (a ==1):
doSomething()
elif (a == 2)
doSomethingElse()
elif (a ==3)
doYetAnotherThing()
else
doDefaultThing()
match a:
1:
doSomething()
2:
doSomethingElse()
3:
doYetAnotherThing()
_:
doDefaultThing()
if objectExists():
if objectIsGreen():
if objectIsMoving():
return true
else:
return false
else:
return false
else:
return false
if not objectExists():
return false
if not objectIsGreen():
return false
if not objectIsMoving():
return false
return true
Avoid horizontally long code
var colors = {1: "green", 2: "red", 3: "orange", 4: "purple"}
var colors = {}
colors[1] = "green"
colors[2] = "red"
colors[3] = "orange"
colors[4] = "purple"
Players[i].player.inventory += item
Players[current].AddInventoryItem(item)
func doSomethingComplex:
# Setup
[ 30 lines of code ]
# Process
[ 60 lines of code ]
# Cleanup
[ 20 lines of code ]
func doSomethingComplex:
var values = setup()
process(values)
cleanup()
Keep code DRY (don't repeat yourself)
process(obj1)
process(obj2)
process(obj3)
for obj in [obj1, obj2, obj3]:
process(obj)