Re: How do you debug?
On Oct 2, 10:23 am, Zeppe
<ze...@remove.all.this.long.comment.yahoo.it> wrote:
desktop wrote:
When I write code I use a lot of:
std::cout << "TEST1\n";
...
...
<some code>
...
...
std::cout << "TEST2\n";
etc.
But is there some better way do this kind of debugging (maybe with some
preprocessor commands)?
I know that I could use the gdb debugger but often I prefer the above
procedure.
In general, a temporary instrumentation of the code in order
to perform debug is not a good choice. It is good, anyway, to
put some assertions from time to time and some preconditions
checks (boundary checks, etc) that are compiled in debug only.
You can do that with the standard assert (relying on the fact
that in Release you can disable the assert by specifying
_NDEBUG and thus improving the code efficiency) or with
something similar written by you.
The symbol to suppress assert is NDEBUG, not _NDEBUG. But of
course, you almost never want to use it. On larger projects,
it's also usual to have some sort of logging facilities, with
different log levels. If some subsystem seems to be causing
problems, you turn up the log levels in that subsystem.
A good debugger anyway is often essential - for linux, a friendly
frontend of gdb is kdbg.
I'll admit that I've never found much use for a debugger
professionally. If you write the code so that it's easy to
understand, and have it correctly code reviewed, there generally
aren't any bugs in it anyway. (I forget who said it, maybe
Hoare: "Code is either so simple that it obviously has no
errors, or so complicated that it has no obvious errors."
Obviously, you should strive for the first.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34