wu :: forums
« wu :: forums - Desin pattern while debugging »

Welcome, Guest. Please Login or Register.
Apr 17th, 2025, 6:13pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: SMQ, Grimbal, towr, william wu, Eigenray, Icarus, ThudnBlunder)
   Desin pattern while debugging
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Desin pattern while debugging  (Read 929 times)
mistaken_id
Junior Member
**





   


Posts: 132
Desin pattern while debugging  
« on: Aug 11th, 2010, 6:30pm »
Quote Quote Modify Modify

I have a function  say foo() in a class which does the following
 
foo(){
 
//Do some simple integer initialization and some string operations
 
writetodatabase()
 
//Do some simple integer initialization and some string operations
 
writetoFilesystem()
 
}
 
 
I want to perform load testing so I want to insert the following steps  
 
foo(){
 
//Do some simple integer initialization and some string operations
int startime = getTime();
writetodatabase();
int finishtime = getTime();
 
Time taken to write to db is finishtime  - starttime
 
timediff = finishtime - starttime
 
//Do some simple integer initialization and some string operations
int startime = getTime();
writetoFilesystem()
int finishtime = getTime();
 
Time taken to write to filesystem is finishtime  - starttime
 
timediff = finishtime - starttime
 
}
 
 
Is there an elegant way to do this ... basically I dont want to calculate start time , finishtime , and timediff when I am not in debug mode....
 
Is there a design pattern which allows me to do this elegantly based on a class member variable  
 
bool debug
 
 
So if deug is true iI want to call the bigger version of the function else I want to call the smaller version.
 
 
In shortwords, I want to change the behavior of a function based on a variable....
 
 
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Desin pattern while debugging  
« Reply #1 on: Aug 12th, 2010, 12:41am »
Quote Quote Modify Modify

Anything wrong with just using if/then? Or #ifdef (although that would mean recompiling between versions; but it has the advantage the non-debug version is totally clean of debug code).
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Desin pattern while debugging  
« Reply #2 on: Aug 12th, 2010, 12:56am »
Quote Quote Modify Modify

For this precise case, getTime() is typically much faster than any kind of file or DB IO.  So you can safely just leave it there in all cases.  You need to deactivate only the place where you display or log the time used.
 
If the debugging code is more complex (like a verification of coherence of your data), as towr says, you can surround all debugging code with a if(debug){ ... } where debug is a global variable or constant.
 
I don't do much C/C++ any more but I believe most C/C++ compilers #define a constant telling if you are compiling in debug mode.
See http://gcc.gnu.org/ml/gcc-help/2009-07/msg00326.html.
« Last Edit: Aug 12th, 2010, 12:56am by Grimbal » IP Logged
Hippo
Uberpuzzler
*****





   


Gender: male
Posts: 919
Re: Desin pattern while debugging  
« Reply #3 on: Aug 12th, 2010, 4:21am »
Quote Quote Modify Modify

on Aug 12th, 2010, 12:56am, Grimbal wrote:

If the debugging code is more complex (like a verification of coherence of your data), as towr says, you can surround all debugging code with a if(debug){ ... } where debug is a global variable or constant.

 
Usually the ifdebug test is done on source code preprocessing, not in the generated code.
IP Logged
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Desin pattern while debugging  
« Reply #4 on: Aug 12th, 2010, 6:57am »
Quote Quote Modify Modify

Ah, yes.  You are right.
It would be a if(debug) if it is a variable, one that you want to switch on and off at run time.
If it is the debug mode the program is compiled in, a #ifdef DEBUG (or #ifndef NDEBUG) makes more sense.
IP Logged
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board