Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: ifdef and loss of readability

31 views
Skip to first unread message

Richard Harnden

unread,
Apr 11, 2021, 4:05:43 PM4/11/21
to
On 11/04/2021 20:06, alessio211734 wrote:
> Hello,
> I'm saving all iterations of an algorithm to a file for debugging later.
> I would like to save this information only in the debug/test phase so
> that I used a macro.
> But inserting so many macros makes the code lose its readability
>
> example:
>
> 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
> ....
> }
>
> My code lose of readability but i don't know how to get around this
> problem.
>

Google groups can't handle '++'s ... get a proper newsreader.

Lew Pitcher

unread,
Apr 11, 2021, 4:42:05 PM4/11/21
to
On Sun, 11 Apr 2021 21:05:27 +0100, Richard Harnden wrote:

> On 11/04/2021 20:06, alessio211734 wrote:
>> Hello,
>> I'm saving all iterations of an algorithm to a file for debugging
>> later.
>> I would like to save this information only in the debug/test phase so
>> that I used a macro.
>> But inserting so many macros makes the code lose its readability
>>
>> example:
>>
>> void myclass::myAlgorithm()
>> {
[snip]
>>
> Google groups can't handle '++'s ... get a proper newsreader.

Yah. Google groups seems to require that the newsgroup name be URL-
encoded, but doesn't do it automagically. Posters have to manually post
to "comp.lang.c%2B%2B" instead of "comp.lang.c++"



--
Lew Pitcher
"In Skills, We Trust"

red floyd

unread,
Apr 11, 2021, 7:11:44 PM4/11/21
to
How about using a template? Following is pseudocode, with types
elided... And should probably be encapsulated in whatever class
that iters is.

#ifdef TEST_ALGORITHM
constexpr bool is_test = true;
#else
constexpr bool is_test = false;
#endif

template<bool save_data = false> void push_back_data(iters, iteration)
{
}

template<true> void push_back_data(iters, iteration)
{
iters.iterations.push_back(iteration);
}


template<bool save_data = false> void save_iterations(datafile)
{
}

template<true> void save_iterations(datafile)
{
iters.save(datafile);
}

...

void myClass:algorithm()
{
// ...
push_back_data<is_test>(iters, iterations);

// ...
save_iterations<is_test>(datafile);
}


}

Paavo Helde

unread,
Apr 12, 2021, 2:26:11 AM4/12/21
to
The simplest solution is to move #ifdef inside the helper functions and
trust the compiler to optimize away the calls if empty:

void DebugPush(auto& iters, auto iteration) {
#ifdef TEST_ALGORITM
iters.iterations.push_back(iteration);
#endif
}

void myclass::myAlgorithm()
{
// here code algorithm
....
DebugPush(iters, iteration);

// here code algorithm
....
DebugSave(iters);

// here code algorithm
....

}


Here, the most important aspect is to have a special prefix like "Debug"
above, which tells the reader that these functions do not participate in
the main logic of the algorithm.

0 new messages