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

Chapter 3: Refactor your robot!

Refactor robot initialisation

face Josiah Wang

Let’s continue refactoring your robot to make it even more object-oriented!

So far, you have a single Robot class, which is a nice abstraction to what kind of properties a robot can have and what kind of actions it can perform.

Now, if your code is somewhat similar to mine, I still have quite a lot of functions in main.py that can be potentially refactored into classes.

Task 1: Move robot initialisation into a new file

One obvious function left in my main.py is the initialise_robot() function, which depends on four other functions to generate the robot’s name, id, initial position and initial direction. Since they are all related, and are actually not used by any other functions, the first thing you can immediately do is to move all these functions into another file, say robot_init.py. So, do that, then import the initialise_robot() function from main.py with from robot_init import initialise_robot, and make sure that your code still works correctly!

Once you are happy, add the new robot_init.py (or whatever you called it) to your repo, and commit all changes!