APCO 1P93 Lecture Notes - Lecture 11: Data Validation, Defensive Programming
Document Summary
If a program reads bad data as input, it will produce bad data as output: programs should be designed to accept only good data, input validation. All input should be inspected before processing. If it"s invalid, it should be rejected and the user should be prompted to enter the correct data. Input validation is commonly done with a loop that iterates as long as input is bad. Priming read is the first input to be tested: //make sure it is not lower than 0. While score < 0 or score > 100. Display error: the score cannot be less than 0 . Writing validation functions: for complex validation, it is recommended to write a function, this process can make the code look cleaner. Validating string input: some strings must be validated such as those programs that ask for a specific string input like yes , or programs that specify a string to be a specific length like password validation.