Techniques for coding software
Solve the problem before writing the code.
- Try several examples on paper.
- Determine the formula(s) needed (also determines required inputs) and the steps required.
- Before rushing to a solution, list different ways to solve the problem.
Break problems into smaller steps.
- Try three major steps:
1. Input
2. Calculations
3. Output.
If three steps are still too big, break these steps down further.
- Make the program work for the easiest example. Then make it work for a harder problem. Keep repeating until all problems solved.
- If feeling overwhelmed, stop focusing on what you don't know how to do and concentrate on what you can do. Then do it.
Once you've built those pieces, how they fit together may become more apparent.
- Finding Classes - Write down the problem and solution.
In the text, nouns are candidates for classes. Each class should stand for something and it should represent just one thing.
The code
- Don't write the whole program. Write a little and compile it. Debug often as you go along.
- If a section of code starts
getting too complicated (too many steps, too many things going on or
maybe just too hard to keep all in your head at one time) take a step
back from it. There may be a simpler solution, or it might be time to
break this section up into multiple, separate sections. Each function
or method should do one thing well.
- Sometimes I test things in main() just to make sure they work by themselves then move the code into a method or function.
- Don't stop with your first
draft. Don't change too much at once. First, make it work. Then make
the code clean, the output nice, and then make the code faster.
- Before rushing to a solution, list the different ways you could go about a task.
For instance, a for loop and a while loop both accomplish similar things, but are best suited to different situations.
Testing
-
Try at least three different examples.
- Test programs at their boundaries.
- No input
- Bad input
- Correct input but at the edge of acceptable.
- Make sure input cannot violate the limits of the program.
For example, enter negative numbers or very large numbers and see what happens to the program.