Development Strategy for C Programs
It is important that you develop your code development, debugging
and testing skills. We can provide guidance during office hours,
but it is important that you have a strategy that helps you develop,
test and debug your code on your own and with the minimal
assistance possible.
Below we provide information about a strategy you should follow
while implementing code. If you are taking cmsc216, and you are
looking for help during office hours, we expect you to follow
a similar strategy, otherwise we will not be able to help you :(.
The strategy can be summarized as follows:
Strategy Summary
-
Add code incrementally (e.g., code fragment
or a function) → If you write a lot of code you are making
the debugging task harder. The code you write should have
good indentation and good variable names
from the start. This will make the debugging task easier and
help TAs provide assistance.
-
Do not ignore compilation warnings.
- Create your own tests →
Make sure the code you just added is correct by creating
your own tests. Usually provided tests (e.g., public tests)
test several aspects of your code simultaneously. You
need a test that is specific to the code you just wrote.
Also, submit your code often to the submit server to
verify your code works as expected in the server.
- Rely on tools like Valgrind, gdb, splint →
Use tools like Valgrind to identify common errors
like reading unitialized data, performing invalid
read / write operations, or dynamic memory problems.
You need to run Valgrind after you add some code and not
once you have implemented all your code. Tools like splint
can help as well.
If you code is generating a seg fault / core dump, use
Valgrind or gdb (where option) to identify the problem.
If this does not work, step through the code using
the simple test you developed.
- Backup your code →
Once your code is working, create a backup of the
code before you continue working. Backups help as
sometimes you would like to go back to code you had
(or in case you delete your code by mistake).
- Take breaks → Many errors can be
found easily if you take a break and focus on something else.
-
Go back to step one.
Additional Information
-
One of the most important rules when developing code is
incremental code development: add a little bit, make sure
it works and move forward. How much is a little bit varies.
At any point you have to be able to answer the following
question: "up to what point my code worked?"
-
As you write your code, you need to write your own tests.
Even if tests are provided, writing your own tests will
allow you to thoroughly test your code and identify
errors early. If you are looking for help during office hours,
you need to have a test that illustrates the problem you
are having. This test must be as simple as possible.
In our classes we provide tests and expected output, but in
the real world you need to write your own tests :). Start
practicing now. Often students stop by looking for help
with a provided test (public test) and they just indicate
"my code is not passing the test". If your code is not
passing a provided test, simplify the test to narrow down
where the problem is taking place. Once you have narrow
down the problem, then we can provide help.
-
Knowing debugger basics is enough. If you know how to
set a breakpoint, step through code and change between
frames you are set. If you use incremental code
development chances are you may not use the debugger
that often (or at all). By the way, if using a print
statement helps you find your problems go ahead. However,
you should know debugger basics (you want to look cool
in that internship/job :)).
-
Visit Debugging in C Guidelines