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

Merging to main branch

face Josiah Wang

Phew - that must have been hard work! That’s probably equivalent to a coursework assignment! I really think this exercise is worth the trouble!

My own refactored implementation can be found at this link if you want to see how I did it. Remember that my implementation is not necessarily the ‘correct’ or the best solution!

Once you are happy with your refactored code, add and commit your latest main.py to the refactor branch.

user@MACHINE:~/robot$ git add -u
user@MACHINE:~/robot$ git commit -m "Refactored code to be modular and self-explanatory."
[refactor dec3cd6] Refactored code to be modular and self-explanatory.
 1 file changed, 334 insertions(+), 119 deletions(-)
 rewrite main.py (89%)
user@MACHINE:~/robot$ git log --oneline -5
dec3cd6 (HEAD -> refactor) Refactored code to be modular and self-explanatory.
0ebdde4 (main) Improved robot in main.py so that the robot automatically navigates to cell (9,9) for its Ribena.
47a4835 Improved robot in main.py so that it automatically rotates 90 degrees clockwise whenever it hits a wall.
57cec2e Added extensive comments to main.py. I hope my future self will appreciate my effort!
ba232e3 Automated robot initialisation by randomising the robot's initial coordinates and direction.

You may also delete main_old.py as it is no longer needed (or you can just leave it there in the working directory, or even commit it to the repo if you really want to).

Finally, let’s merge your refactor branch into the main branch. I don’t expect any merge conflicts (unless you have been committing to the main branch, then you will have to sort it out yourself!)

user@MACHINE:~/robot$ git checkout main
user@MACHINE:~/robot$ git merge refactor -m "Merging refactor branch to main branch"
Updating 0ebdde4..dec3cd6
Fast-forward (no commit created; -m option ignored)
 main.py | 417 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 316 insertions(+), 101 deletions(-)
user@MACHINE:~/robot$ git log --oneline -5
dec3cd6 (HEAD -> main, refactor) Refactored code to be modular and self-explanatory.
0ebdde4 Improved robot in main.py so that the robot automatically navigates to cell (9,9) for its Ribena.
47a4835 Improved robot in main.py so that it automatically rotates 90 degrees clockwise whenever it hits a wall.
a73ea84 Improved robot in main.py so that it automatically moves forward in the same initial direction until it hits a wall.
57cec2e Added extensive comments to main.py. I hope my future self will appreciate my effort!

Notice that git said “Fast-forward (no commit created;...” when merging. This is because we did not commit anything new to main since branching out to refactor. So git simply needs to move the main branch forward to the latest commit and treat the history as a linear progression.

And congratulations on getting this far! You can take a short break, or just continue straight on to the last short chapter on debugging.