Running leftmotorstate motorstatereversed

Info icon This preview shows pages 220–223. Sign up to view the full content.

RightMotorState == MotorState.Running) || (LeftMotorState == MotorState.Reversed && RightMotorState == MotorState.Reversed)) { Drive(duration); return true; } // The motors are running in opposite directions, // so we don't move, we just rotate about the // center of the rig if ((LeftMotorState == MotorState.Running && RightMotorState == MotorState.Reversed) || (LeftMotorState == MotorState.Reversed && RightMotorState == MotorState.Running)) { return Rotate(duration); } // We didn't expect to get here return false; } 196 | Chapter 6: Dealing with Errors
Image of page 220

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Notice that we updated our documentation for the public method as we changed the contract. We also have to return values from all of the exit points of our method. That has exposed another problem with our implementation: we never supported one motor at the stop condition, and the other at the driving or reversing condition. Well, that’s fine—we can return an error if we hit those conditions now. One problem with this contract is that we can’t tell why our error occurred. Was it due to the state of the motors, or a problem with Rotate ? We could create an enum that lets us distinguish between these error types: enum TurtleError { OK, RotateError, MotorStateError } Then we could use the enum as shown in Example 6-9 . Example 6-9. Indicating errors with an enum // Run the turtle for the specified duration // Returns one of the TurtleError values if a failure // occurs, or TurtleError.OK if it succeeds public TurtleError RunFor(double duration) { if (LeftMotorState == MotorState.Stopped && RightMotorState == MotorState.Stopped) { // If we are at a full stop, nothing will happen return TurtleError.OK; } // The motors are both running in the same direction // then we just drive if ((LeftMotorState == MotorState.Running && RightMotorState == MotorState.Running) || (LeftMotorState == MotorState.Reversed && RightMotorState == MotorState.Reversed)) { Drive(duration); return TurtleError.OK; } // The motors are running in opposite directions, // so we don't move, we just rotate about the // center of the rig if ((LeftMotorState == MotorState.Running && RightMotorState == MotorState.Reversed) || (LeftMotorState == MotorState.Reversed && RightMotorState == MotorState.Running)) { if (!Rotate(duration)) { Returning Error Values | 197
Image of page 221
return TurtleError.RotateError; } } return TurtleError.MotorStateError; } OK so far, although it is starting to get a bit tortuous, and we’re going up only one call in the stack. But let’s build and run anyway: Arthur is at (0,0) and is pointing at angle 0.00 radians. Arthur is at (0,10) and is pointing at angle 0.00 radians. Arthur is at (0,10) and is pointing at angle 0.00 radians. Arthur is at (0,-15) and is pointing at angle 0.00 radians. Arthur is at (0,-15) and is pointing at angle 0.00 radians. Arthur is at (0,-18.5355339059327) and is pointing at angle 0.00 radians. Yup; we’re no better off than before, because all we’ve done is to pass the responsibility up to the client, and they are still free to ignore our pleadings. Given that the problem is a result of our oversight in the first place, what is the likelihood that we’ll remember to check the error message?
Image of page 222

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 223
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern