CMSC131/CMSC132 Debugging Guidelines
Finding Bugs
To find bugs in your code, follow the steps below.
-
You should be writing student tests that verify the correctness of your code
as you develop it. If you stop by during office hours, we will ask you
for student tests that illustrate the problem you are experiencing. Remember
it is in your best interest to learn how to write your own tests; in the
real world there are no public tests :). Additional information regarding
JUnit Tests can be found at
JUnit Testing.
-
Although JUnit tests seems complex, they basically allow you to create
little programs where you test part of your code. In a method representing
a JUnit test, you can write code like you would write in a main method. Even if
you don't use assertions (what triggers the color indicating whether you
pass or not a test), writing tests using the JUnit infrastructure is
better than writing different main methods. You can just display the
result of a test (instead of using assertions).
-
Learn debugger basics. If you need to add output statements to identify
problems that is fine, but keep in mind that knowing debugger fundamentals will help you
a lot.
- If you have JUnit public tests, display the result your code
is generating for a test. You can do so by adding a System.out.println
before the assertion that verifies the test correctness. The
expected result for a test could be in the same test or in a
text file.
- If you cannot see the problem from the JUnit test, simplify it.
Copy the test to your student tests file and simplify it.
-
If you cannot see the problem after simplifying the test, run
the simplified test using the debugger (yes, you can run
the debugger on a JUnit test).
-
Do not make any assumptions when developing code. If you are not
sure about the specifications for a project, as for a clarification.
Read the description if you are not passing a test; you could have
missed some important information.
-
Do not write code that you don't understand, but works. This also
apply to help provided by a TA. If a TA provided some help, understand
exactly what the TA did.