CSC300: A basic setup: How to write code [14/37] Previous pageContentsNext page

The amount of time spent on a function does not amount to much. If you are not working effectively, you can spend months on a single function and never get it correct.

So how do you work efficiently? Key things:

Here is a step-by-step

  1. You need to work through some examples before you start. I've typically listed some examples for you. You should walk through these on paper and see what you expect.

  2. Don't do everything at once. Pick a part of the problem. Use a small, typical example. You can simplify things a bit to start. For example, suppose that the array is always nonempty.

  3. Think about how you would do it, on paper. Draw pictures of the state before and after. Once you have a picture, you can think what you need to do in this case to accomplish what you want.

  4. Write a test that will succeed if you get the code right and fail if you don't.

  5. Try to write some code to accomplish the task.

  6. If the code does not pass your test, use the DEBUGGER! Set a breakpoint and the start the debugger, or use the Trace class.

  7. Go back to step 3 and repeat 3-7 until the code works on your first example.

  8. What about other examples? Repeat steps 2-8 until your code is completely correct.

Previous pageContentsNext page