Simple Driving DRYer - Dutton-Christian-Robotics/Learning-to-Code GitHub Wiki

In the previous example we saw how to save time by taking commonly called commands and putting them into a function. Imagine, though, that we're in a competition. We might have multiple OpModes—even a dozen—all of which will be doing some variation of the same thing. How do we make sure not to repeat ourselves when we're dealing with so many OpModes?

The answer comes in the form of another principle: the "separation of concerns." If you think about it right now, our OpModes have been doing two things:

  • Identifying what things our robot can do (e.g. drive) and giving a way to actually do those things.
  • Giving the robot a specific set of instructions for what we want the robot to do during the OpMode.

The second item is what's specific to that OpMode. The first item consists of the things that all OpModes need to know about and have access to. To prevent repeating ourselves, we're going to separate our "concerns." Code that is reusable—the robot capabilities—will go into its own Java file, where it can be called on by the OpModes that implement a specific set of instructions.

In the robot class, code separate functions for forwards, backwards, and stop.