Reflection
Debugging can be extremely difficult. Often you will be stuck in the situation of needing to track down errors in code written by someone else (as in today's exercise). When you write your own code, you have control over its quality. What can you do to improve your chances of getting it right the first time?Novice programmers have a tendency to start writing code before they really understand what it is supposed to do. Here are some guidelines that experienced programmers have shared over the years:
- Think away from the computer. Avoid the temptation of coding first by thinking about the problem to be solved away from the computer. Have paper handy. Start with small, concrete examples that you can work out by hand.
- Start small. If you don't see how to solve a problem, try solving just part of the problem. Try solving a simpler problem that might be related.
- Write down what needs to be done in your native language. You are likely far more comfortable speaking your native language than you are in coding Java, and errors in your strategy for solving a problem will be more obvious to you. If you can't express your problem in spoken language, it will not be any easier to write Java code to solve it. Programmers use pseudocode to first describe the steps of an algorithm at a high level, then use that outline as a basis for writing Java code.
- Write test cases. Whether or not you prefer a labor-saving framework such as JUnit, you always need to construct simple examples of what the code should actually do. Whenever you go to implement a method, ask yourself, "If this method was working correctly, how would I be able to tell"?
- Draw pictures. Walk through confusing code on paper or markerboard. Draw the state of variables and refer to your drawings as you evaluate each expression. Update your drawing as variables are reassigned and erase or cross out previous values.
- Document each method before you implement it. In writing the documentation, you focus on what one method needs to do -- and you won't be tempted to try and solve the entire problem at once.
- Choose good variable names. If you choose meaningful variable names, expressions will be easier to write since it will be obvious what data is being manipulated.
- Follow good style. By being consistent in your coding style, you will be able to look at the code you write and know certain things just from a glance. If you see a word like
maxTime
, you can be certain that this is a variable. If you seeFamily
, you know there must be aFamily
class. Likewise,addMember(...)
must be a method. Proper indentation will reveal the structure of your code. After every opening brace ({
), the indentation should be one level deeper than before the brace. Just by looking a line's indentation you can see easily how it's contained inside loops, conditional statements, and methods. Eclipse helps you to clean up indentation and whitespace of your code withControl-Shift-I
to fix indentation of a selection, orControl-Shift-F
to reformat a selection.
As you leave lab and walk across campus, ask yourself some questions:
- Why should I use a debugger when
System.out.println
can show me the values of our variables whenever we want? - Why is most software sold without any guarantee?
- Am I a random code generator? Or do I take a more systematic approach to writing code?