Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preconditions to python functions #17

Open
aaron97neu opened this issue Nov 16, 2018 · 0 comments
Open

Add preconditions to python functions #17

aaron97neu opened this issue Nov 16, 2018 · 0 comments
Labels
enhancement New feature or request

Comments

@aaron97neu
Copy link
Member

There is little to no input verification on the arguments passed to functions, it seems as if invalid inputs are attempted to be handled further down in the code. It is typically good practice to verify the input to a function before doing anything else. This leaves less strange cases to handle later and assists in debugging. This is especially important for an untyped language such as python

For example, see the function orient_to in the branch commutersAsObjects:

# Initialize the bearing, needed for relative instructions
 def orient_to(commuterName, direction):
         # Orient_to(commuterName, direction)
         # commuterName: commuter object to change bearing
         # direction: Direction in degrees or cardinal directions

         # Start with a bearing of due north
         bearing = 0

         # if the direction is in degrees
         # isReal tests is the obj is not NaN or inf
         if isReal(direction):
             commuterName.direction = direction;
             return direction

         # Set the bearing based on cardinal/relative dir
         if direction.upper() == "NORTH":
                 bearing = 0
         elif direction.upper() == "EAST":
         ...

The commuterName should be verified as a commuter object, and direction should be verified as a valid bearing, cardinal direction, or right/left. Something similar to this could be used

 def orient_to(commuterName, direction):
         # Orient_to(commuterName, direction)
         # commuterName: commuter object to change bearing
         # direction: Direction in degrees or cardinal directions
  
         # PRECONDITIONS:
         # commuterName must be a valid commuter object
         # direction must be a vaild bearing or a cardinal direction, or left/right

          # Check to make sure commuter is the correct object
         if type(commuterName) is not commuter:
             return -1;
         # Verify the direction is valid
         if not (direction in ["NORTH","EAST","SOUTH","WEST","LEFT","RIGHT"] or not (isReal(direction) and direction > 0 and direction < 360))
             return -1

         # Start with a bearing of due north
         bearing = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant