Code style for easy debugging:
No global variables
All variables are initialized, and the member variables are initialized in the constructor
Use const as much as possible
A detailed note
VC + + Compilation options:
Always use the/W4 warning level
Always use the/GZ compilation option in debug builds to discover errors in release releases
Compile without warning: Ensure no warnings after compilation, but carefully check before eliminating warnings
Debug Method:
1. Use Assert (principle: as simple as possible)
The assert only takes effect under debug and is not compiled under release.
Example:
char* strcpy(char* dest,char* source)
{
assert(source!=0);
assert(dest!=0);
char* returnstring = dest;
while((*dest++ = *source++)!= ‘\0’)
{
;
}
return returnstring;
}
2. Defensive programming
Example:
char* strcpy(char* dest,char* source)
{
if(source == 0)
{
assert(false);
reutrn 0;
}
if(dest == 0)
{
assert(false);
return 0;
}
char* returnstring = dest;
while((*dest++ = *source++)!= ‘\0’)
{
;
}
return returnstring;
}