DMD 1.019 and 2.003 releases
Regan Heath
regan at netmail.co.nz
Wed Jul 25 03:22:43 PDT 2007
Derek Parnell wrote:
> On Tue, 24 Jul 2007 20:55:41 -0400, Robert Fraser wrote:
>
>> for(int i = 100; i; i--) // Takes a second to mentally figure out what's going on
>
> I'm still not explaining myself I guess. Yes, it doesn't take much to work
> out what the compiler is going to generate for that code. But that is not
> the issue I'm addressing.
>
> If one see's a line of code like that one has trouble recognising that what
> was written may not have been what was intended to be written. How do we
> know that the code shouldn't have been ...
>
> for(int i = 100; i>1; i--)
Well.. I'd say that is unlikely.
The more likely meaning was:
for(int i = 100; i > 0; i--)
But! (to support your side of the argument here) the short form:
for(int i = 100; i; i--)
actually means:
for(int i = 100; i != 0; i--)
which is subtly different in the case where i is (mistakenly) set to a
negative value inside the loop!
So, in this case I would have to agree that "i > 0" has a different
meaning to plain "i" and prevents a bug as well.
In general, where the meaning is the same (i.e. "i == 0") I favour "if
(i)" and have no trouble reading either form (which I think is a benefit
we should all acquire you never know whose code you'll be reading next!)
Regan
More information about the Digitalmars-d-announce
mailing list