The Code Refinery
Turn a plan into a working program: break problems down, write readable Python, and track down syntax, logic and runtime errors.
Get the method right under pressure
Free interactive practice on the steps that lose marks under exam pressure.
Start revising freeWhat you'll cover
Refining your code
Writing a program is only the start. Refining code means making it clear and correct: breaking problems down, writing readable code, and tracking down errors. This module uses short Python 3 examples.
Key terms
Learn these before you begin:
Match each idea to its description
- Syntax error
- Logic error
- Runtime error
- Readability
- Breaks the rules of the language
- Runs but gives the wrong output
- Crashes while the program is running
- Making code easy for people to read
Name the error
A programmer forgets the colon at the end of an 'if' line in Python, so the code will not run. What type of error is this?
- A syntax error
- A logic error
- A runtime error
- No error at all
Readable code
Readable code is easier to understand and fix. Good habits: - Use meaningful identifiers (a name like total, not t). - Indent code inside loops and if-statements. - Add comments to explain tricky parts. - Use white space and blank lines to group ideas.
Readable code
Select the TWO techniques that make code more readable.
- Using meaningful variable names
- Adding comments to explain the code
- Putting the whole program on one long line
- Naming every variable x
Breaking problems down
Two ideas help you tackle big problems. Decomposition means breaking a problem into smaller sub-problems you can solve one at a time. Abstraction means hiding unnecessary detail so you focus on the key parts. A flowchart or pseudocode is an abstraction: a plan for the code before you write it.
Match each technique to its meaning
- Decomposition
- Abstraction
- Pseudocode
- Refining code
- Breaking a problem into smaller parts
- Hiding unnecessary detail
- A plain-language plan for a program
- Improving code that already works
Developing a program
Put the steps of developing a program from a plan in a sensible order.
- Decompose the problem into smaller parts
- Write pseudocode or draw a flowchart
- Write the Python code
- Test it and fix any errors
Readable code frame
Good code uses meaningful _____, so a total is called total not t. Code inside a loop is _____ to show it belongs there. A _____ explains tricky code to a reader. Code that breaks the rules of the language gives a _____ error.
Name the error
A program runs all the way to the end but prints the wrong average. What type of error is this?
- A logic error
- A syntax error
- A runtime error
- A readability error
Techniques and errors frame
Breaking a problem into parts is called _____. Hiding detail to focus on the key parts is _____. An error that crashes the program while it runs is a _____ error. An error that runs but gives the wrong output is a _____ error.
Classify the error
Classify each programming error.
- The code has 'prnt(x)' written instead of 'print(x)', so it will not run. This is a...
- The program tries to divide a number by zero and crashes. This is a...
- The code runs, but it adds two numbers where it should multiply them, giving a wrong answer. This is a...
Explain good practice
Explain how decomposition and readable code make a program easier to develop and fix.
- Say what decomposition means and why it helps
- Give two ways to make code readable
- Explain how readable code makes errors easier to find