Organizing Your FTC Code for Debuggability and Growth

intermediate55 min

Why organize code

Small methods and helper classes make it obvious where drive, arm, or sensor logic lives. That speeds up fixes during build season.

Hardware init method

Move all hardwareMap.get() calls into initHardware() and call it at the top of runOpMode().

initHardware example

private DcMotor leftMotor, rightMotor;

private void initHardware() {
    leftMotor = hardwareMap.get(DcMotor.class, "left_drive");
    rightMotor = hardwareMap.get(DcMotor.class, "right_drive");
}

OpMode that calls initHardware

@TeleOp
public class ModularOpMode extends LinearOpMode {
    private DcMotor leftMotor, rightMotor;
    @Override
    public void runOpMode() {
        initHardware();
        waitForStart();
        while (opModeIsActive()) {
            leftMotor.setPower(0.5);
            rightMotor.setPower(0.5);
        }
    }

    private void initHardware() {
        leftMotor = hardwareMap.get(DcMotor.class, "left_drive");
        rightMotor = hardwareMap.get(DcMotor.class, "right_drive");
    }
}

Naming and file layout

Match method names to subsystems (drive(), controlClaw()). Keep one main OpMode per file when possible.

Organization tips

  • Keep each file focused on one purpose (e.g., hardware, drive logic, sensors).
  • Use consistent naming conventions.
  • Document your code with comments and JavaDoc.
  • Avoid duplicating code—use helper methods or classes instead.
  • Review and refactor your code regularly.

JavaDoc and comments

Document public helpers with JavaDoc; use short inline comments only where logic is non-obvious.

JavaDoc example

/**
 * Sets the power for both drive motors.
 * @param power The power level (-1.0 to 1.0)
 */
public void setDrivePower(double power) {
    leftMotor.setPower(power);
    rightMotor.setPower(power);
}

Backups and version control

OnBot Java has no built-in Git. Copy working OpModes to a team repo or shared drive before big edits. See FTC Docs: Programming Resources.

Practice: Refactor a Monolithic OpMode

Take a long OpMode file and break it into at least two classes (e.g., hardware and logic). Add comments and JavaDoc to your methods. Test that your code still works.

  • Identify sections of code that can be moved to helper classes.
  • Refactor the code and update your OpMode to use the new classes.
  • Add JavaDoc and comments to explain your logic.
// Example of a monolithic OpMode (all logic and hardware in one class)
@TeleOp
public class MonolithicOpMode extends LinearOpMode {
    private DcMotor leftMotor, rightMotor;
    private Servo clawServo;
    @Override
    public void runOpMode() {
        leftMotor = hardwareMap.get(DcMotor.class, "left_drive");
        rightMotor = hardwareMap.get(DcMotor.class, "right_drive");
        clawServo = hardwareMap.get(Servo.class, "claw_servo");
        waitForStart();
        while (opModeIsActive()) {
            double leftPower = -gamepad1.left_stick_y;
            double rightPower = -gamepad1.right_stick_y;
            leftMotor.setPower(leftPower);
            rightMotor.setPower(rightPower);
            if (gamepad1.a) {
                clawServo.setPosition(1.0);
            } else if (gamepad1.b) {
                clawServo.setPosition(0.0);
            }
            telemetry.addData("Left Power", leftPower);
            telemetry.addData("Right Power", rightPower);
            telemetry.addData("Claw Position", clawServo.getPosition());
            telemetry.update();
        }
    }
}

Further Reading Resources