Star Student Builder logoStar Student Builder
← Code Makers Lesson 5Std 6-8

Tidy Blocks

A long program is hard to read and harder to fix. Break it up. One block per part, each with a name that says what it draws, and the bottom of your program becomes a summary anyone can follow.

πŸ’‘ The idea

Give every part of a drawing its own named block, and the program reads like a list of parts.

  • Write all your to ... end blocks first, then call them in order at the bottom.
  • Each block should do one job, and its name should say what that job is.
  • Test each block on its own before joining them up.
  • A block can set its own colour and width, so it does not depend on what came before.
  • If a block is getting long, that is a hint that it is really two blocks.

πŸ“˜ Words to know

procedure
another word for a named block of code
structure
how a program is arranged into parts
test
run one part on its own to check it

πŸ‘€ Watch this

A tree: a fat brown trunk block and a green leaves block, joined by the last two lines.

to trunk
colour brown
width 10
forward 90
end
to leaves
colour green
circle 45
end
trunk
leaves
What this program draws
What it draws

✍️ Your turn

  1. Run it, then delete the last line. Only the trunk is left.
  2. Swap the last two lines around and explain what changes.
  3. Change circle 45 to circle 70 for a bigger crown.
  4. Add fill on inside leaves to make the crown solid, and see that a block controls its own pen.
  5. Write a third block called grass and call it first.

🎯 Challenge: Add a block called fruit that draws a red dot, then put two fruits on the tree. Give the trunk its brown colour back as well.

Show me one answer
to trunk
forward 90
end
to fruit
colour red
dot 16
end
trunk
fruit
goto 40 90
fruit

Your program
Ask GuruAcharya Samayeshwar