Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Yet another MSVC weird behaviour

Author: Miguel A. Ballicora

Date: 10:16:14 12/28/01

Go up one level in this thread


On December 28, 2001 at 09:33:30, David Rasmussen wrote:

>On December 27, 2001 at 16:56:21, Michel Langeveld wrote:
>
>>
>>Although I think the problem you posted before is already solved I still answer
>>your question. It's good to write:
>>
>>int i;
>>
>>for (i=0; i < 8; i++)
>>{
>>   /* i = 0,1,2,3,4,5,6,7 here */
>>}
>>
>>/* i = 8 here */
>>
>>But you have to consider that if you use this construction that after this for
>>i=8 in the end.
>
>I'm not sure what you're saying here, but as for good style, this is recommended
>(in C++ notation):

When it comes to styles, people get religiously attached. So, I will my opinion
without saying that is recommended or not (I am talking C):

>for (int i = 0; i != 8; ++i)
>{
>  ...
>}
>
>There are several points here:
>
>1. The variable is declared close to where it is used. Enhances readability and
>maintainability. Can't be done in C though.
>2. The increment is written "++i" to emphasize that it is an incrementation and
>nothing else. You only write it for it's side effect, you dont use the value of
>the expression.
>3. Most importantly to this discussion: The continuation condition "i != 8" is
>more clear, since the invariant that holds after the loop (the post condition)
>will always by definition be !(i != 8) which is i == 8. If you had "i < 8", your
>post condition is !(i < 8) which is i >= 8. So you dont really know that i == 8
>when you're done. If you do something to i in the loop (maybe as a bug), i == 8
>might not be true at the end.

I prefer

for (i = 0; i < N; i++) {
    something();
}

or
for (i = 0; i < N; ++i) {
    something();
}

1) it is idiomatic. That is very, very, very important to keep bugs being
introduced the first time you write the code and readability of idiomatic
constructs is generallly (at least for me) very high.
2) i++ or ++i I do not mind in this case.
3) i < N is defensive programming. As you say, if for some reason in your
release version i went to N+1, this loop will stop and the program will
continue. In your version, it will crash or do really nasty things because the
loop will continue for ever.
Now, I do not want to be defensive in a debug version, I want the program to
stop. So, an improved version will be

for (i = 0; i < N; ++i) {
    something();
}
assert (i==8);

However, for short loops I do not mind to do this assert and keeping the idiom
is what I care most. Maybe I should do the assert anyway.

4) use of {} brackets is a matter of style an nothing else but we just saw an
example of the drawback of using
for ()
{
}

because you can make the mistake of doing

for ();
{
}

That will never happen if you use

for () {
}


Well, all this IMHO and were not my ideas, but what I read on some books
and took what I like best. Those books were supposed to be good... but,
this is not an exact science :-) :-)

Regards,
Miguel


>
>/David



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.