DoRevision

The Code Refinery

Turn a plan into a working program: break problems down, write readable Python, and track down syntax, logic and runtime errors.

⏱️ 18 min 🎯 14 activities
Best used for
Homework Intervention

Get the method right under pressure

Free interactive practice on the steps that lose marks under exam pressure.

Start revising free

What 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:

Read the symptom

  • Syntax error
  • Logic error
  • Runtime error
  • Readability
  • The editor underlines the line in red and nothing runs
  • It runs cleanly and prints 7 when the answer is 12
  • It works for a while, then stops halfway with an error
  • Another person can follow your code without asking you

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.

Which technique is at work?

  • Decomposition
  • Abstraction
  • Pseudocode
  • Refining code
  • Splitting a game into scoring, drawing and input routines
  • Calling a sort function without caring how it sorts
  • Writing the plan in English before opening the editor
  • Renaming variables and removing repetition after it 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.

identifiers indented comment syntax numbers deleted loop logic

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.

decomposition abstraction runtime logic iteration selection syntax comment

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