Producing Robust Programs
This section explains Producing Robust Programs focusing on: Defensive design considerations, Authentication, Validation, Maintainability, Using subprograms to produce structured code, Testing, Programming errors, Selecting and using suitable test data.
Defensive Design Considerations
Defensive design ensures that programs handle unexpected inputs and reduce the likelihood of errors.
Key Strategies:
Anticipating misuse:
- Consider how users might input incorrect data.
- Include measures to handle such cases gracefully.
Input validation:
- Check data before processing to ensure it is valid.
Error handling:
- Write code to manage errors without crashing the program.
Authentication
Authentication verifies the identity of a user to protect sensitive data.
Examples of Authentication:
- Username and password: Requires users to enter valid credentials.
- Multi-factor authentication: Combines methods like passwords, SMS codes, or biometrics.
Good Practices:
- Encrypt passwords to protect them.
- Limit login attempts to prevent brute force attacks.
Validation
Validation ensures that inputs meet specific criteria before being processed.
Common Validation Techniques:
Range check: Ensures a value falls within a specified range.
Example: Age must be between 0 and 120.
Length check: Confirms the input has an acceptable number of characters.
Presence check: Ensures required data is provided.
Format check: Confirms data matches a specific pattern.
Example: Email addresses must contain @.
Type check: Ensures data is of the correct type (e.g., integer, string).
Maintainability
Maintainable code is easy to understand, debug, and modify.
Key Practices:
Comments:
Add explanations to clarify what the code does.
Example:
python
# Calculates the area of a rectangle
area = width * height
Naming Conventions:
Use descriptive names for variables and functions.
Example: customerName instead of cn.
Indentation:
Use consistent indentation to structure code logically.
Example:
python
if age > 18:
print("Adult")
Using Subprograms to Produce Structured Code
Subprograms include procedures and functions that allow programs to be broken into manageable parts.
Benefits of Subprograms:
Reusability:
Code can be reused in multiple places, reducing duplication.
Readability:
Programs are easier to understand by breaking tasks into smaller, logical steps.
Debugging:
Errors are easier to locate and fix within a specific subprogram.
Procedures:
Subprograms that perform tasks but do not return a value.
python
def greet():
print("Hello")
Functions:
Subprograms that return a value after execution.
python
def square(num):
return num * num
Built-in Functions:
Predefined functions available in programming languages.
Examples: len(), print(), abs().
Testing
Testing ensures that a program behaves as expected and meets requirements.
Types of Testing:
Iterative Testing:
Tests are conducted during development to identify issues early.
Final/Terminal Testing:
Performed when development is complete to ensure the program works as intended.
Testing Techniques:
- Dry runs: Manually stepping through the code.
- Trace tables: Used to follow the flow of variables during execution.
Programming Errors
Programming errors fall into two main categories:
Syntax Errors:
Occur when the code violates the rules of the programming language.
Examples:
- Missing punctuation.
- Incorrect indentation.
Effect: The program will not run until the error is fixed.
Logic Errors:
Occur when the program runs but produces incorrect results.
Examples:
- Using the wrong mathematical operator.
- Incorrect condition in an if statement.
Selecting and Using Suitable Test Data
Types of Test Data:
Normal Data:
- Values that are expected and valid.
- Example: Entering 25 for a person’s age.
Boundary Data:
- Values on the edge of acceptable ranges.
- Example: Testing ages 0 and 120 when the valid range is 0–120.
Invalid Data:
- Data that should be rejected by validation.
- Example: Entering -5 for an age or a string like "twenty".
Testing Tables:
Testing tables organise test cases, expected outcomes, and actual outcomes.
Example:
Test Case | Input | Expected Output | Actual Output | Pass/Fail |
---|---|---|---|---|
Normal input | 25 | "Valid age" | "Valid age" | Pass |
Boundary input | 0 | "Valid age" | "Valid age" | Pass |
Invalid input | -5 | "Invalid age" | "Invalid age" | Pass |
Summary of Key Concepts
Defensive Design: Ensures the program is robust against errors and misuse.
Authentication: Protects sensitive data by verifying user identity.
Validation: Ensures data inputs are appropriate and reliable.
Maintainability: Comments, naming conventions, and indentation make code easier to manage.
Subprograms: Break down programs into manageable, reusable sections.
Testing: Identifies and resolves errors through systematic approaches.
Errors: Syntax and logic errors need to be detected and corrected.
Test Data: Normal, boundary, and invalid data ensure comprehensive testing.
Understanding these principles is essential for creating efficient, error-free, and maintainable programs.