6.8 - Testing & Debugging Programs
Keeping code clear and readable
When programmers work in teams, they often contribute different parts to a larger program. For the code to work well together, it must be easy for others to read and understand. This helps avoid confusion and makes it simpler to fix issues or add new features later.
Programs can be complex, and code written by one person might need to be reviewed or changed by someone else.
Methods for making code readable
There are several techniques to improve how easy code is to read. These include adding explanations, organising the layout, and choosing descriptive names.
Using comments
Comments are notes added to code that explain what a section does. They are ignored by the computer when running the program but help humans understand it.
How to use comments:
- Start comments with symbols like // or #, depending on the programming language.
- Use them to describe key parts, such as what a loop or condition checks.
- Add enough to clarify, but avoid too many as they can clutter the code.
Applying indentation
Indentation means adding spaces or tabs to shift code inwards, showing its structure.
Benefits of indentation:
- Use it inside statements like IF conditions or FOR loops to mark where they begin and end.
- This helps to clearly show where statements begin and end.
- In languages like Python, correct indentation is required for the code to run properly.
Choosing meaningful variable names
Variables are storage locations for data in a program. Giving them clear names helps others (and yourself) remember their purpose.
Guidelines for variable naming:
- Use descriptive names like
userIDinstead of vague ones likex. - This is especially important in large programs with many variables.
Example of readable code
Here is an example in pseudocode that combines these techniques:
This example demonstrates:
- The comments explain the purpose of sections.
- Indentation shows the structure of the IF-ELSE block.
- Variable names like
correctPassandinputPassclearly describe what they store.
Understanding errors in programs
Errors, also known as bugs, are mistakes in code that stop a program from working correctly. Testing helps find and fix them to make the program robust, meaning it handles inputs reliably without crashing.
There are two main categories of errors
- Syntax errors - These occur when code breaks the rules of the programming language, like a spelling mistake or wrong punctuation. The program cannot run at all, and it usually highlights the problem line.
- Logic errors - These happen when the code runs but produces the wrong result, such as calculating something incorrectly. They are harder to spot because the program appears to work.
Finding and fixing errors
- Syntax errors are often easy to identify as the program refuses to start and points to the issue.
- Logic errors require careful testing by comparing what the program does against what it should do.
- Fixing involves changing the code and re-testing to ensure the problem is gone.
Testing programs effectively
Testing involves running a program with various inputs to check if it behaves as expected. This reveals weaknesses and helps improve robustness. Repeat testing after fixes to confirm everything works.
Categories of test data
Use a range of data to test thoroughly, covering what the program might encounter in real use.
Types of test data:
- Normal data - Expected inputs that the program should accept and process correctly, like typical user entries.
- Boundary data - Inputs at the extreme limits of what is allowed, which should also be accepted.
- Invalid data - Inputs that should be rejected, often triggering an error message, like values outside the allowed range.
Example of selecting test data
Suppose you are testing a program that sets a new password, which must be between 6 and 12 characters long.
Here are four test inputs:
| Test input | Category | Expected outcome |
|---|---|---|
| "password" | Normal | Accepted as it is 8 characters |
| "abcde6" | Boundary | Accepted as it is exactly 6 characters |
| "" (empty) | Invalid | Rejected with an error message |
| "thisIsMyPassword" | Invalid | Rejected as it is 16 characters |
This range ensures the program handles both valid and invalid cases properly.