How to Code It

If there was a systematic way to translate a specification into a working program, we wouldn't need programmers. However, this guide should provide you with some ideas and points to help you when you're stuck.

  • The left-hand column provides some ideas and guiding question on decomposing a problem into a program plan.
  • The right-hand column gives more specific advice on encoding loops, conditionals and other programming constructs.

The basics

How to plan what code to write:

  1. What is the data?
    1. What data is your program taking in? This could be from a file, user input or values from the problem specification.
    2. What data is your program required to produce?
  • How do I store the data?
  • Which pieces of data should be stored together (for example, in a list)?
  • In each part of the task, how many pieces of data are being referred to? Does your data storage make this referral convenient?
  • What processing do I need to do?
  • Are you repeating the same stages a number of times? How many tasks are in the repeated stages?
  • What types of input can you expect? How many routes through your program should there be?
  • Some techniques

    Think about the simplest case

    Some problems can seem very daunting as a whole. Try thinking about the simplest case. What if there was only one very simple input - how would you deal with this on its own?

    Imagine it as a real world problem

    Imagine if you were trying to solve the problem by hand. What stages would you go through? Try to understand the underlying sub-stages in your thought processes.

    Solve the smallest pieces first

    Write down all the tasks that this program involves. Are any of these things that you know how to do? Leave the harder tasks until you have something working.

    Look at examples

    Have you seen all or part of this task tackled somewhere already? Which parts are the same? Which parts are different?

Specific programming pieces

What loop should I build?

Is it one of the known patterns?

  1. Getting variable amounts of data from a file/the user
  2. Moving through a list/dictionary to look for particular features (eg. maximum value)

Should I use a for loop or a while loop?

  1. Use a for loop if you know *at the time of entering the loop* how many iterations are needed
  2. Use a while loop when the number of iterations will depend what happens as the loop is executed

Should I use an index or an iterator?

  1. Use an index (eg. "for index in range(len(list))" or "while index … index = index + 1") if you need to refer to adjacent items in the list or if position in the list is important.
  2. Use an iterator (eg. "for item in list") if you only need to refer to one item in the list at a time and position is not important.

What conditional should I build?

  1. How many different categories of processing are there?
  2. How are they distinguished? (what condition(s) should you use).
  3. Is there any case that would not be caught properly by any of your conditions?

What function should I build?

  1. Is there a repeated question or sub-task in your program that can be expressed in a short phrase (eg. "process input", "find maximum", is largest?")
  2. What does the function need to know to do its job? (parameters)
  3. What is the answer or product you want the function to produce? (return value)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License