This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 9: Refactoring the robot

Refactoring the robot

face Josiah Wang

Now, back to good old Python!

I will assume that your robot project is now a big lump of code that is hard to read and decipher. ๐Ÿ˜ต๐Ÿ˜ต

Time to tidy things up a bit! ๐Ÿงน๐Ÿงน๐Ÿงฝ๐Ÿงฝ

I would like you to refactor your robot project, so that it is modular and self-explanatory.

You have a mammoth task ahead of you now! ๐Ÿ˜๐Ÿ˜๐Ÿ˜

This whole process might be a bit overwhelming, so don’t worry, I’ll be here to help you get started.

Hopefully by the end of this, you will understand how to structure code in a modular and self-explanatory fashion. You should then be ready to tackle any programming problem that is at a larger scale than what you have done so far!

Now, this refactoring task might be a bit big. Let’s just create a new experimental git branch before we start, so that we can revert to the old version if anything goes wrong! And just for the sake of practising what you have learnt about branches earlier!

Create a new branch in your robot project repo, and call it refactor, and switch to the new branch.

user@MACHINE:~/robot$ git branch refactor
user@MACHINE:~/robot$ git branch
* main
  refactor
user@MACHINE:~/robot$ git checkout refactor
Switched to branch 'refactor'