liveBook · Manning

Refactoring

As explained in Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi

The systematic restructuring of existing software code without changing its externally observable behavior.

Refactoring systematically changes or simplifies internal code without changing externally observable behavior. For robot movement, it consolidates repeated motor-control logic while preserving function names and arguments. section 3.10

A practical refactoring pass for robot movement:

  • Find repeated logic, such as setting both motor throttles, waiting, and stopping the motors. section 3.10
  • Centralize that logic in a movement function accepting duration, speed, and left- and right-motor factors. section 3.10
  • Keep readable operations such as forward as small wrappers that pass arguments to the centralized implementation. section 3.10
  • Save the reusable implementation as commonmotor.py and import it for later robot controls. section 3.10
Listing 3.7 commonmotor.py: Consolidating common logic into a common function
#!/usr/bin/env python3
from adafruit_crickit import crickit
import time
import os
 
DC_ADJUST_R = float(os.environ.get('ROBO_DC_ADJUST_R', '1'))
DC_ADJUST_L = float(os.environ.get('ROBO_DC_ADJUST_L', '1'))
ADJUST = dict(R=DC_ADJUST_R, L=DC_ADJUST_L)
MOTOR = dict(R=crickit.dc_motor_1, L=crickit.dc_motor_2)
THROTTLE_SPEED = {0: 0, 1: 0.5, 2: 0.7, 3: 0.9}
 
def set_throttle(name, speed, factor=1):
    MOTOR[name].throttle = THROTTLE_SPEED[speed] * ADJUST[name] * factor
 
def movement(duration=0.2, speed=3, factor_r=1, factor_l=1):
    set_throttle('R', speed, factor_r)
    set_throttle('L', speed, factor_l)
    time.sleep(duration)
    set_throttle('R', 0)
    set_throttle('L', 0)
 
def forward(duration=0.2, speed=3):
    movement(duration, speed)
 
def backward(duration=0.2, speed=3):
    movement(duration, speed, factor_r=-1, factor_l=-1)
 
def right(duration=0.2, speed=3):
    movement(duration, speed, factor_r=0.5)
 
def left(duration=0.2, speed=3):
    movement(duration, speed, factor_l=0.5)
 
def spin_right(duration=0.2, speed=3):
    movement(duration, speed, factor_r=-1, factor_l=1)
 
def spin_left(duration=0.2, speed=3):
    movement(duration, speed, factor_r=1, factor_l=-1)

Shows a complete Python motor library that centralizes throttle conversion, motor adjustment, timed movement, and directional wrappers. section 3.10

Consolidating robot movement

Before refactoring, each movement operation repeats throttle, timing, and stopping code. Afterward, movement(duration, speed, factor_r, factor_l) performs the shared work, while forward(duration, speed) delegates to it without changing how callers invoke the function. section 3.10

How Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi covers it

How deep it goes
Explains shared-logic extraction, parameter passing, wrappers, and library reuse, but does not develop broader refactoring techniques.
Examples
Uses repeated DC-motor movement code, a centralized movement function, directional wrappers, and commonmotor.py.
What it assumes
Starts with existing Python movement functions and assumes a working motor library whose external interface must remain stable.
What you can do after
Apply it via Python functions to simplify motor-control code without altering callers or robot movement behavior.
How it connects
Connects refactoring with code duplication, functions, maintainability, reusable libraries, and functools.

Also explained in

Compare how 22 books explain refactoring →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi. The section links point to the parts of the book it draws on.