Docsity

Defensive Design and Maintainability in Programming - Prof. Akerman, Slides of Computer Science

Partial preview of the text

Download Defensive Design and Maintainability in Programming - Prof. Akerman and more Slides Computer Science in PDF only on Docsity!

Title: L2 Defensive Design Date: 09 May 2025 Topic: LOGIC AND LANGUAGES Theme: PROGRAMMING In this lesson: ▪ To understand Defensive Design ▪ To understand Maintainability

The purpose of defensive design is to ensure that a program runs correctly and continues to run no matter what actions a user takes. This is done through planning for all possibilities (contingencies) and thinking about what a user may do that the program does not expect. Maintainability : Ensuring that programmes are easy to understand, modify and update. This ensures that a programme can be easily maintained over time.

Anticipating Misuse A programmer must try to ensure that whatever a user enters when they run a programme, it will not crash or give an incorrect result. What will happen if the user enters “12345” or “1.234” or “*^) %” or “ABcD” Please enter a 3-digit number: What should happen?

Please enter a 3-digit number: Ideally, the programme should recognise when the user input is invalid and repeat the request. How many times should the request be repeated? Should a message indicate the error? Should the user be offered some guidance? Where should the programme go if it doesn’t get a valid entry?

Types of validation checks Check Example Range check A number or date is within a sensible/allowed range Type check Data is of the right type, such as integer, letter or text Length check Text entered is not too long or too short – for example, a password is between 8 and 15 characters Presence check Checks that data has been entered, i.e. the field has not been left blank Format check Checks that the format of, for example, a postcode or email address is correct

Types of validation checks What sort of validation check is made in this algorithm? postcode = input("Please enter postcode:") if postcode.length < 6 OR postcode.length > 8 then print("Invalid postcode") endif Rewrite the algorithm so that the program keeps asking the user to enter a postcode until the entry is valid

Verification Validation can only check that the data entered is reasonable. Verification is used to double-check that the data has been typed in correctly. For example, a user setting a new password may be asked to type it in twice ▪If the two passwords don’t match, they will be asked to enter the password again ▪This is known as double-entry^ verification

Authentication Routines Authentication of an individual is used to make sure a person is who they claim to be. What is a common method of online authentication, for example when you log in to a website with which you have previously registered?

Entering a password Often you will get three attempts and then you will be locked out. Why is there a limit to the number of tries?

Why only three tries? It may be that you have forgotten your password, and you need to be given a reminder, so three tries is enough for the average user. BUT a hacker may be trying out dozens of likely passwords to try and get the correct one ▪ There are software programs which will try out every combination of letters, numbers and special characters – this is known as a brute-force attack. ▪ Use a password of 8 characters or more to make it more difficult to hack!

MAINTAINABILITY

Programs need to be maintained: ▪to improve the code, fix bugs or add new features to the program ▪It may be carried out by the original programmer or different programmers It is important that programs are written in a way to make them easily maintainable. This includes: ▪The use of sub programs (functions and procedures) ▪Using appropriate naming conventions ▪Indentation ▪Commenting

Using Sub-programmes Subprograms include functions and procedures. ▪Well written subprograms will take inputs (through parameters) and if necessary return a value. ▪They should be written so that they can be reused multiple times in the program or by other programs. ▪The two programs below are for a function that works out the area of a circle. Which is more easy to reuse and will help create maintainable code?

Naming Conventions The two programs below are the same algorithm. Which is easier to understand? Why? a = float(input()) b = float(input()) c = float(input()) d = a + b + c e = d / 3 print(e) num1 = float(input()) num2 = float(input()) num3 = float(input()) total = num1 + num2 + num average = total / 3 print(average)

Naming Conventions The two programs below are the same algorithm. Which is easier to understand? Why? a = float(input()) b = float(input()) c = float(input()) d = a + b + c e = d / 3 print(e) num1 = float(input()) num2 = float(input()) num3 = float(input()) total = num1 + num2 + num average = total / 3 print(average) It is important to use meaningful names for variables, constants, functions and procedures. This makes code easier to read and understand