Edexcel International Advanced Level Computer Science, point 17.2.2
The tools themselves are described in 6.3 (IDE, version control, APIs) and 9.3.1 (testing and debugging). In Unit 4 you must be able to select and use appropriate tools to write, test and refine a program in the IDE of your choice.
Using the IDE well in the exam
| Task | IDE tool | Tip |
|---|---|---|
| Typing code quickly and correctly | Autocomplete, syntax highlighting, indentation help | Accept suggestions only if they are what you want |
| Finding syntax errors | Syntax checker and error underlining | Read the message and line number |
| Running the program | Run button or terminal | Run often, after every small change |
| Finding a logic error | Debugger: breakpoints, step over, step into, variable watch | Pause just before the suspect line |
| Seeing what a variable holds | Variable watch, memory inspection | Faster than many print statements |
| Checking the call stack in recursion | Call stack view | Look for runaway recursion |
| Renaming a variable everywhere | Refactoring (rename symbol) | Safer than find and replace |
| Looking up how a function works | Documentation pop-up or help() |
help(str.split) |
| Running part of the code | Run selection, or the interactive shell | Try an expression before using it |
| Working with several files | Project explorer, tabs | Keep related code together |
Good habits in a timed practical:
- Save often.
- Run after each small step, so errors are easy to locate.
- Use the debugger rather than guessing.
- Keep the provided coding files organised, and don't overwrite given code you were told to keep.
- Leave time to test and tidy (comments, names, clean code, Topic 19).
Other tools
Automated testing
Write tests as code that run automatically and report failures.
import unittest
def is_even(n):
return n % 2 == 0
class TestIsEven(unittest.TestCase):
def test_even(self):
self.assertTrue(is_even(4))
def test_odd(self):
self.assertFalse(is_even(7))
def test_zero(self): # boundary case
self.assertTrue(is_even(0))
if __name__ == "__main__":
unittest.main()
assertstatements give a simple version:assert is_even(4).- Run all tests after every change (regression testing).
- Benefits: fast, repeatable, catches mistakes early, and shows the code meets its requirements (9.3.1).
Version control (Git)
Record changes, go back, and work in branches (6.3.2). In a project: commit after each working step, with a clear message.
Linters and formatters
- A linter (flake8, pylint) checks code against style rules and finds likely errors (unused variables, long lines).
- A formatter (black) reformats code consistently (9.2.1).
Profilers and timing
Measure where a program spends its time, to find what to optimise (9.1.2).
import time
start = time.perf_counter()
total = sum(range(1_000_000))
print(f"{time.perf_counter() - start:.4f} seconds")
timeit gives more accurate timing of small pieces of code.
Documentation and comments
Docstrings describe what a subprogram does: help(my_function) shows them. Tools can turn them into documents.
Package managers and virtual environments
pip installs libraries (NumPy, Pandas, 18.1). A virtual environment keeps each project's libraries separate.
The interactive shell
Try ideas immediately before adding them to the program:
>>> "a,b,c".split(",")
['a', 'b', 'c']
Choosing tools for a task
| Need | Tool |
|---|---|
| Find a crash cause | Read the error and traceback, then the debugger |
| Check results are right every time | Automated tests |
| Work in a team or keep history | Version control |
| Keep code tidy | Linter and formatter |
| Find slow code | Profiler or timing |
| Learn a library function | help() and the documentation |
Worked example
Task: this function should return the second largest number but gives the wrong answer for some inputs. Show how to use tools to find and fix the fault.
def second_largest(nums):
largest = second = 0
for n in nums:
if n > largest:
second = largest
largest = n
elif n > second:
second = n
return second
- Write tests first:
assert second_largest([5, 9, 7]) == 7
assert second_largest([9, 9, 4]) == 9 # duplicates
assert second_largest([-5, -2, -9]) == -5 # negatives
- Run them: the third fails (the function returns 0).
- Use the debugger: put a breakpoint on the
forline and step through with[-5, -2, -9]: the watch window showslargestandsecondstay at 0, because no negative number is greater than the starting value 0. - Fix: start both from negative infinity, so that any real number beats the starting value:
def second_largest(nums):
largest = second = float("-inf")
for n in nums:
if n > largest:
second = largest
largest = n
elif n > second:
second = n
return second
- Re-run all the tests: all three pass. Running the earlier tests again (regression testing) shows the fix hasn't broken the duplicate case.
Tools used: tests to show the fault, the debugger's variable watch to find the cause, and tests again to prove the fix.
Exam tip: use the debugger and small tests rather than guesswork. Run your code frequently. Name the tool and what it did if asked ("the debugger's variable watch showed that second never changed").
Check yourself
Why run your code after every small change?
Any error is then in the new code, so it is quick to find and fix.
What is regression testing?
Re-running earlier tests after a change to check that nothing that worked has been broken.
What does a linter do?
Checks code for style problems and likely errors, such as unused variables or lines that are too long.
Give one advantage of automated tests over testing by hand
They are fast, repeatable and can be run after every change.