2.3 Producing Robust Programs Flashcards
To get a 9 (45 cards)
What is input validation?
Checking data input by the user meets specific criteria or rules before processing
Defensive design considerations: Type check
The input is in the correct data type (e.g. integer, real, string)
Defensive design considerations: Range check
The input is within a correct range (e.g. between 1 & 2)
Defensive design considerations: Presence check
Some data has been entered (e.g. reject blank inputs)
Defensive design considerations: Format check
The input is in the correct format (e.g. dd/mm/yyyy)
Defensive design considerations: Length check
The input has the correct (min/max) number of characters (e.g. passwords)
What can a programmer make their program by using input validation techniques?
By using input validation techniques, a programmer can make their program:
1) More robust
2) More user friendly
3) Prevent further errors occurring later in the algorithm
What does input validation help prevent?
The easiest way for a user to accidentally or intentionally misuse a program is when entering data. This can be prevented using input validation.
Anticipating misuse: Division by zero
A programmer should always check that a variable isn’t zero before attempting a division by it to reduce the risk of their program crashing
Anticipating misuse: Communication error
Online systems require connections to host servers.
If this connection is dropped, unable to be established, or the system is overloaded, it could potentially cause a program to crash or hang when loading/saving data.
Anticipating misuse: Communication error (solution)
A programmer should enable ways for the user to cancel requests or for them to fail gracefully, reporting the connection error.
The program may be able to automatically resume when the connection is available again.
Anticipating misuse: Printer and other peripheral errors
If a program outputs a hardcopy, the printer may run out of paper, ink, or have a jam.
The programmer should not assume that an output to a printer was successful and always have options to reprint reports or receipts.
Anticipating misuse: What are disk errors?
Programs that read and write to files used to handle many types of exceptions, including:
1) The file/folder not being found
2) The disk being out of space
3) The data in the file being corrupt
4) The end of the file being reached
Anticipating misuse: How are disk errors solved?
Robust programs will handle all these situations by checking files and data before attempting to use them for further processing
What are common ways to increase the security of a password-based authentication system?
1) Force users to use strong passwords and change them regularly
2) Limit the number of failed authentication attempts before access to an account is lost
3) Ask for a random selection of characters from the password on each authentication
What is authentication?
Authentication can confirm the identity of a user before they’re allowed to access certain pieces of data or features of the program
Data used by systems should be secure. How can this be achieved? (authentication)
1) Username and password to access systems
2) Recovering a password requires clicking on a link within the email that is sent to the registered address
3) Encryption of data files
Online bots can submit data automatically to online forms. How can this be protected against?
This can be protected against using software such as reCAPTCHA that verifies the user is human.
Programmers should also be aware of the potential for SQL hacks and other methods used by hackers
What does a well-maintained program do?
A well-maintained program makes it easy for other programmers to understand what the code does.
They should be able to change parts of the source code without the risk of causing problems elsewhere in the code (e.g. knock on effects)
Maintainability: comments
Use comments to explain the purpose of the program, explain sections of code (typically selections, iterations and procedures), explain unusual approaches that were necessary, visually divide sections of a program
Maintainability: white space
Use white space to make sections of a program easier to see
Maintainability: indentation
Use indentation for every selection and iteration branch (allows programmers to see the flaw of a program more clearly and pick out the different features)
Maintainability: descriptive variable names
Use descriptive variable names, sub programs and parameters and explain their purpose with a comment when declared.
This helps programmers understand what they do and make it easier to keep track of them (e.g. names refer to what they actually are)
Maintainability: procedures and/or functions
Use procedures and/or functions to structure the code and eliminate duplicating code (sub programs make it easier to see how different parts of a program work, which help them understand the overall program faster)