Kids Coding
Clean Code
Names that mean something. Short functions. Comments only when the code cannot say it.
Names that mean something
x is a box. score is a box you can remember tomorrow. Prefer words.
Clear names
def area(width, height):
return width * height
print(area(3, 4))If you have to explain what a line does, a better name usually removes the need. Good names are the cheapest comments you will ever write.
Short functions
- One job per function.
- If you need a comment to explain a block, that block might want a name.
- Comments are for why, not for repeating the code.
A comment that helps
# Lives drop only on a wrong letter, not on a repeat
lives = 3Real life
Labelled boxes in a closet: “cables”, not “stuff”.
A name that means something is faster tomorrow. score beats x.
Label the box
def pocket_left(had, spent):
return had - spent
print("Left:", pocket_left(20, 6))Change one word. Run it. That is how this idea shows up outside the lesson.
Try it
Exercise 1: Rename this idea: def f(a, b): return a + b into something readable and call it.
Show solution
def add(left, right):
return left + right
print(add(2, 3))