Indentation? If yu had a run-time variable, it would be this:
void myclass::myAlgorithm()
{
// here code algorithm
....
if (test_algorithm) {
iters.iterations.push_back(iteration);
...
}
// here code algorithm
....
if (test_algorithm) {
iters.save(datafile);
...
}
// here code algorithm
....
}
You can indent #ifdefs similarly.
void myclass::myAlgorithm()
{
// here code algorithm
....
#ifdef TEST_ALGORITHM
iters.iterations.push_back(iteration);
...
#endif
// here code algorithm
....
#ifdef TEST_ALGORITHM
iters.save(datafile);
...
#endif
// here code algorithm
....
}
Nested #if/#ifdef directives without indentation can be hard to follow.
Another readabiilty barrier is to avoid flipping between #ifdef
and #ifndef. It's hard to grok that "n" in the middle of #ifndef.
A good practice is to use the value 1 for true, and then use #if
to test it.
#if TEST_ALGORITHM
then if you need #ifndef TEST_ALGORITHM, somewhere, you instead use
#if !TEST_ALGORITHM
This will work even if TEST_ALGORITHM is not defined. The preprocessor
treats undefined identifiers in expressions as being 0.
You can use a compile-time constant and if statements, by the way.
Any viable compiler from the last 30 years will remove the dead code.
if (TEST_ALGORITHM) { // must be defined as 0 or 1
// code here is dead if TEST_ALGORITHM is 0.
}
and the nice thing is that the interior still has to compile.
The useful thing about #ifdef is that the code in between,
when it is deselected, does not have to be compilable.
#ifdef WIN32
HANDLE foo = SomeFunctionOnlyOnWindowsEx(arg, ...)
...
#endif
When WIN32 is absent, the compiler does not see this, which is a good
thing because the function would not be declared or defined!
--
TXR Programming Language:
http://nongnu.org/txr
Cygnal: Cygwin Native Application Library:
http://kylheku.com/cygnal