Reputation: 2052
Recently, working with a significant amount of code, I observed that sometimes adding some extra printf() statements and commenting them is very useful for debugging in later stages (makes it easier) when the code has to be modified. But there are debates about over-commenting the code and absence of proper comments. I am not sure where this practice stands? Also, a drawback I find in such a way of commenting is that it makes the code look uglier. Here's an example :
...... //code ...... pkt_bytes_decd = avcodec_decode_audio4( aCodecContext, pFrame, &frame_fin, &packet );Is it a bad practice ? Can someone discuss the pros and cons from their experience ?
//printf("%d bytes from packet decoded\n",pkt_bytes_decd); ...... ...... //code
Upvotes: 3
Views: 1476
Reputation: 36067
You need to distinguish between permanently logging information for troubleshooting at customer site and just logging information when you are developing. I find the latter can mostly be replaced by using the debugger and with normal // "why-comments"
. If it is too cumbersome to use the debugger for whatever reason then I personally find it better to have a logging mechanism that is not affected from whether it is release or debug mode.
There is always a risk with having two different versions of the program when you are developing, the release version and the debug version. If the versions differ too much you may get some nasty surprises later. In fact they are like two different programs.
For instance debugging statements generally make the program run slower so if you have some timing problem (e.g. race conditions) in your code they may become hidden but of course suddenly pop out when you are running the program in release mode (or more likely when you are showing the program to a customer/user).
++sp; // move the stack pointer beyond last element to mark underflow
++sp; // increment sp
Upvotes: 1
Reputation: 188
If you want to include the debugging statements in final code, then use a command-line option to set a global, and then check that global for each printf:
if(DEBUG) printf("extra info");
If you want the debugging statements, but not in final code, use the preprocessor:
$ gcc -DDEBUG <files>
#ifdef DEBUG
printf("extra info");
#endif
Upvotes: 4
Reputation: 347
Compiler doesn't retain comments. So Its ok to write printf in comments. Not a Bad Practice.. But overdoing it is bad. Key to it is :- "balance"
PROS :- 1) To debug for a quick check becomes lil easy.
CONS :- 1) Comments themselves can be more confusing then code. 2) While debugging ,Programmer may tend to be highly dependent on comments thus ignoring Code Flow. 3) Commenting while writing code is multi tasking. And thats slows down coding.:-(
Upvotes: 0
Reputation: 116477
It is much cleaner to use some macro that depends on debugging flag being enabled or disabled.
This way, you do not need to uncomment anything to debug. Simply enable debug flag, and it immediately enables logging in all such places.
Sometimes another approach is used - function like log(level, message)
is called, and it will emit message only if level is above set threshold (typically called error, warning, info, etc). It is not as efficient, but makes debugging much easier.
Upvotes: 4