Crafty 23.1: search.c

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

jarkkop
Posts: 198
Joined: Thu Mar 09, 2006 2:44 am
Location: Helsinki, Finland

Crafty 23.1: search.c

Post by jarkkop »

line 453
while (0);

redundant code?
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Crafty 23.1: search.c

Post by bob »

jarkkop wrote:line 453
while (0);

redundant code?
Nope. Goes with the "do { }" where the "}" is right before the while(0);

This makes the code easier to read, in that I want to get to the bottom of the loop without using a goto xxx where it is hard to figure out where xxx is. A "break" or "continue" will get you to that point.
wgarvin
Posts: 838
Joined: Thu Jul 05, 2007 5:03 pm
Location: British Columbia, Canada

Re: Crafty 23.1: search.c

Post by wgarvin »

bob wrote:
jarkkop wrote:line 453
while (0);

redundant code?
Nope. Goes with the "do { }" where the "}" is right before the while(0);

This makes the code easier to read, in that I want to get to the bottom of the loop without using a goto xxx where it is hard to figure out where xxx is. A "break" or "continue" will get you to that point.
Yuck. Using a loop that doesn't loop, merely so you have somewhere to break to? Just to avoid one of the safest and least obnoxious uses of goto.

This kind of trick doesn't work when other loops are involved (since C has no multi-level break). Of course its up to your personal coding style, but personally I would not write "do { } while(0)" anywhere but in a macro.
jarkkop
Posts: 198
Joined: Thu Mar 09, 2006 2:44 am
Location: Helsinki, Finland

Re: Crafty 23.1: search.c

Post by jarkkop »

please check again from code because I couldn't a pair for do.

There is one

do
while(0);

but this seems extra.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Crafty 23.1: search.c

Post by Sven »

jarkkop wrote:please check again from code because I couldn't a pair for do.

There is one

do
while(0);

but this seems extra.
It *is* extra. The closing bracket in line 452 belongs to the while loop starting in line 246. The mentioned "do ... while(0);" is within that loop, from lines 258 to 401. So the "while(0);" in line 453 is indeed empty code.

Of course, not a bug. Just redundant. The compiler removes it.

Sven
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Crafty 23.1: search.c

Post by Sven »

wgarvin wrote:
bob wrote:This makes the code easier to read, in that I want to get to the bottom of the loop without using a goto xxx where it is hard to figure out where xxx is. A "break" or "continue" will get you to that point.
Yuck. Using a loop that doesn't loop, merely so you have somewhere to break to? Just to avoid one of the safest and least obnoxious uses of goto.

This kind of trick doesn't work when other loops are involved (since C has no multi-level break). Of course its up to your personal coding style, but personally I would not write "do { } while(0)" anywhere but in a macro.
I can only second that. However my major point is that in a "do { } while(0)" construct, even more if it spans many lines, the use of "break" and "continue" makes the whole code difficult to read because both keywords have the same effect in this special case although you think that "continue" does something different in the first moment.

Code that needs "goto" or such a "do ... while(0)" construct should be completely restructured IMO. I would use some inline functions to make the whole search function considerably smaller.

But of course, this is a matter of coding style, and the code seems to be correct.

Sven
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Crafty 23.1: search.c

Post by bob »

wgarvin wrote:
bob wrote:
jarkkop wrote:line 453
while (0);

redundant code?
Nope. Goes with the "do { }" where the "}" is right before the while(0);

This makes the code easier to read, in that I want to get to the bottom of the loop without using a goto xxx where it is hard to figure out where xxx is. A "break" or "continue" will get you to that point.
Yuck. Using a loop that doesn't loop, merely so you have somewhere to break to? Just to avoid one of the safest and least obnoxious uses of goto.

This kind of trick doesn't work when other loops are involved (since C has no multi-level break). Of course its up to your personal coding style, but personally I would not write "do { } while(0)" anywhere but in a macro.
I've found it makes the code much easier to read. Search is quite long. A "goto xyz" could go anywhere in a thousand lines of code. A break simply jumps to the end of the do { } while(0); and since I use indent to maintain indentation levels, it is really easy to see where this is going. All a matter of taste, of course.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Crafty 23.1: search.c

Post by bob »

jarkkop wrote:please check again from code because I couldn't a pair for do.

There is one

do
while(0);

but this seems extra.
we changed this from a do to a while() loop sometime back, and that extra while(0) was not noticed since the compiler removes it. I've fixed it. Thanks..
Last edited by bob on Mon Nov 30, 2009 1:52 am, edited 1 time in total.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Crafty 23.1: search.c

Post by bob »

Sven Schüle wrote:
jarkkop wrote:please check again from code because I couldn't a pair for do.

There is one

do
while(0);

but this seems extra.
It *is* extra. The closing bracket in line 452 belongs to the while loop starting in line 246. The mentioned "do ... while(0);" is within that loop, from lines 258 to 401. So the "while(0);" in line 453 is indeed empty code.

Of course, not a bug. Just redundant. The compiler removes it.

Sven
You are correct. At some point this was changed from a do to a while at the top of the loop, and that superfluous while (0); was not removed since the compiler dumps it. I've removed it...

thanks