FalconX-Robotics / frc2018

Code for our FRC 2018 robot.
MIT License
2 stars 5 forks source link

General vs. Specific Commands #1

Open magic-robot-man opened 6 years ago

magic-robot-man commented 6 years ago

What should the criteria be for having specific forms of a more general command? For instance, we have a TurnAngle command that can turn towards any angle. However, we also have a TurnLeft and TurnRight which are pretty much TurnAngle for specific angles.

Should commands like TurnLeft and TurnRight exist, or should we always use more general forms such as TurnAngle? And if so, should the more specific commands simply call upon their more general counterparts and pass in specific arguments?

TheBrianKong commented 6 years ago

maybe have a command that is called TurnAngle, with parameter accepting angle? eg. TurnAngle (double degrees)

moreheadm commented 6 years ago

More specific functions tend to make the intent of code that uses those functions clearer. For example, consider calling turnAngle(-90). Does that turn the robot to the left or to the right? You'd have to look at the documentation or source code to be sure.

For implementation, you could call turnRight(-angle) in order to implement turnLeft or vice versa, and then you only have to implement one function anyway.

There are cases when it make sense to have an enum type as a parameter to a function. This makes more sense when you have many options, the implementation between the options varies, and/or you are reusing the same suffix across multiple functions. For example, if you hypothetically had a fast, medium, and slow mode and you had functions driveForwardFast, driveForwardMedium, driveForwardSlow, turnRightFast, turnRightSlow, etc. then an enum parameter would make sense.

moreheadm commented 6 years ago

Thinking about this more, for command based programming, it may make sense to use an enum or create TurnLeft and TurnRight as subclasses of Turn(). For example,

public class TurnAngle extends Command {
    public TurnAngle(TurnEnum turnType, double angle) {

    }
    .......
}

Or

public class TurnAngle extends Command {
    public TurnAngle(double angle) {
        ....
    }
   ...
}

public class TurnLeft extends TurnAngle [
     public TurnLeft(double angle) {
         super(-angle);
     }
    // no implemenation needed here
}

In the second option, TurnAngle would contain all the implementation, and TurnLeft/TurnRight would only need a constructor.