Test-driven development

Programming and debugging can be part of the same development process.

If you were making a program, testing should really be a regular activity within the software development cycle. If you leave testing until the end, you will have a lot of code and it can be difficult to work through to find the problems. You should always plan to test early and often.

Test-driven development (TDD) is a method for developing computer programs that involves continually repeating the same process of build, test, change, build, test, change, etc. The programmer starts by building something that does not work and then makes the minimum amount of changes required to make it pass a test. Then this piece of code is refactored (rewritten) so that it meets acceptable standards.

Refactoring code

Refactoring sections of code may include:

  • improving names for variables and custom functions
  • placing sections of code in locations that make it easier to read
  • breaking large sections of code into smaller pieces with a logical flow

Refactoring the code has two main advantages:

  • Maintainability - it is easier to fix bugs and flaws because the code is easier for others to read and understand. If someone else has to change something after you have coded it, refactoring makes their job easier. One way of doing this is to refactor large sections of code into smaller individual sections of code.
  • Extensibility - it is much easier for others to add extra features and functions to the code if it is written in a standardised way.

Commenting code

Commenting is used to explain the function of different sections of code. This is to make it easier to read. This section of code has had comments added to make it more readable:

# store two values and print the sum first = input("Input the first number") #store first value second = input("Input the second number") #store second value print("The sum is",first+second) #print the sum

If programmers leave helpful comments in the code, it makes it easier for others to understand what purpose each section of code performs.

Lorna describes how she uses comments to explain the purpose of her programs