Prev: integer
Next: shared memory question
From: Ben Bacarisse on 7 Mar 2010 08:09 Rainer Weikusat <rweikusat(a)mssgmbh.com> writes: > pete <pfiland(a)mindspring.com> writes: > > [...] > >> To count down through an array with N elements, I use >> >> size_t i = N; >> >> while (i-- != 0) { >> array[i]; >> } > > And you excuse for writing confusing code which requests that the > machine does useless things is that software to work around you has > already been developed, right? Why not show everyone what you consider to be the right way to do this so that we can all have the chance to write better/clearer loops? Not everyone will agree, of course, but it is surely better to give examples of good code than to criticise code you don't like. -- Ben.
From: Rainer Weikusat on 7 Mar 2010 09:30 Ben Bacarisse <ben.usenet(a)bsb.me.uk> writes: > Rainer Weikusat <rweikusat(a)mssgmbh.com> writes: >> pete <pfiland(a)mindspring.com> writes: >> >> [...] >> >>> To count down through an array with N elements, I use >>> >>> size_t i = N; >>> >>> while (i-- != 0) { >>> array[i]; >>> } >> >> And you excuse for writing confusing code which requests that the >> machine does useless things is that software to work around you has >> already been developed, right? > > Why not show everyone what you consider to be the right way to do this > so that we can all have the chance to write better/clearer loops? Not > everyone will agree, of course, but it is surely better to give > examples of good code than to criticise code you don't like. Isn't this obvious? As written above, the calculation oversteps, because the postdecrement will return zero when i already had this value before decrementing it again. Also, the condition is known to be true so checking it before the first execution of the loop body is a pointless exercise. A sensible way to express this in C would be do array[--i]; while (i); This is politically incorrect for various reasons not the least one of which is that people are often proud of having written code thoughtlessly, presumably, because this demonstrate that it is below their dignity to care for something as miniscule as that, and which one will end up generating better code depends on the compiler and how it was used. Eg, I have reason to suspect that at least some versions of gcc transformate all loops to the 'condition in front of it'-form (I don't know the English term for this, the German one would be 'abweisende Schleife') before they are feed to the various weird loop optimizers intended to turn thoughtlessly written FORTRAN into sensible machine code. But as R. Pike aptly observed in http://www.lysator.liu.se/c/pikestyle.html A program is a sort of publication. It's meant to be read by the programmer, another programmer (perhaps yourself a few days, weeks or years later), and lastly a machine. The machine doesn't care how pretty the program is - if the program compiles, the machine's happy - but people do, and they should. and the major problem is actually not that the machine has to deal with the crud but that some day, some other human will need to decode the text in order to understand the algorithm which will effectively be performed by the program and the larger the amount of "compiler knows what I mean" expressions in there is, the more complicated becomes this task. Especially since people tend to confuse their intentions everytime, not just in isolated examples. One useless calculation isn't a big problem. Thousands of lines of code filled with expressions which appear to do all kinds of useless operations are a major obstacle because it is not possible to distinguish the 'production' and the 'pollution' parts of the code without understanding its purpose first.
From: Ben Bacarisse on 7 Mar 2010 10:14 Rainer Weikusat <rweikusat(a)mssgmbh.com> writes: > Ben Bacarisse <ben.usenet(a)bsb.me.uk> writes: >> Rainer Weikusat <rweikusat(a)mssgmbh.com> writes: >>> pete <pfiland(a)mindspring.com> writes: >>> >>> [...] >>> >>>> To count down through an array with N elements, I use >>>> >>>> size_t i = N; >>>> >>>> while (i-- != 0) { >>>> array[i]; >>>> } >>> >>> And you excuse for writing confusing code which requests that the >>> machine does useless things is that software to work around you has >>> already been developed, right? >> >> Why not show everyone what you consider to be the right way to do this >> so that we can all have the chance to write better/clearer loops? Not >> everyone will agree, of course, but it is surely better to give >> examples of good code than to criticise code you don't like. > > Isn't this obvious? As written above, the calculation oversteps, > because the postdecrement will return zero when i already had this > value before decrementing it again. What do you mean by "oversteps"? The code above works correctly when N is zero and it executes the body exactly N times as it should. > Also, the condition is known to be > true so checking it before the first execution of the loop body is a > pointless exercise. The condition can't be know to be true. It may be false at the start. > A sensible way to express this in C would be > > do array[--i]; while (i); > > This is politically incorrect I think this is technically incorrect because it does not handle the case where N == 0. <snip> -- Ben.
From: Rainer Weikusat on 7 Mar 2010 10:41 Ben Bacarisse <ben.usenet(a)bsb.me.uk> writes: > Rainer Weikusat <rweikusat(a)mssgmbh.com> writes: >> Ben Bacarisse <ben.usenet(a)bsb.me.uk> writes: >>> Rainer Weikusat <rweikusat(a)mssgmbh.com> writes: >>>> pete <pfiland(a)mindspring.com> writes: >>>> >>>> [...] >>>> >>>>> To count down through an array with N elements, I use >>>>> >>>>> size_t i = N; >>>>> >>>>> while (i-- != 0) { >>>>> array[i]; >>>>> } >>>> >>>> And you excuse for writing confusing code which requests that the >>>> machine does useless things is that software to work around you has >>>> already been developed, right? >>> >>> Why not show everyone what you consider to be the right way to do this >>> so that we can all have the chance to write better/clearer loops? Not >>> everyone will agree, of course, but it is surely better to give >>> examples of good code than to criticise code you don't like. >> >> Isn't this obvious? As written above, the calculation oversteps, >> because the postdecrement will return zero when i already had this >> value before decrementing it again. > > What do you mean by "oversteps"? Don't try to play 'stupid' with me. You understood me quite well, have 'surprisingly' completely ignored my text in order to construct a straw man and are a live demonstration of the exact type of behaviour I criticized. And very rightfully, sine I have to read quite lot of code I didn't write myself and I claim to know something about the problems the people who enjoy creating 'bunte Getueme' actually fabricate.
From: Ben Bacarisse on 7 Mar 2010 10:53
Rainer Weikusat <rweikusat(a)mssgmbh.com> writes: > Ben Bacarisse <ben.usenet(a)bsb.me.uk> writes: >> Rainer Weikusat <rweikusat(a)mssgmbh.com> writes: >>> Ben Bacarisse <ben.usenet(a)bsb.me.uk> writes: >>>> Rainer Weikusat <rweikusat(a)mssgmbh.com> writes: >>>>> pete <pfiland(a)mindspring.com> writes: >>>>> >>>>> [...] >>>>> >>>>>> To count down through an array with N elements, I use >>>>>> >>>>>> size_t i = N; >>>>>> >>>>>> while (i-- != 0) { >>>>>> array[i]; >>>>>> } >>>>> >>>>> And you excuse for writing confusing code which requests that the >>>>> machine does useless things is that software to work around you has >>>>> already been developed, right? >>>> >>>> Why not show everyone what you consider to be the right way to do this >>>> so that we can all have the chance to write better/clearer loops? Not >>>> everyone will agree, of course, but it is surely better to give >>>> examples of good code than to criticise code you don't like. >>> >>> Isn't this obvious? As written above, the calculation oversteps, >>> because the postdecrement will return zero when i already had this >>> value before decrementing it again. >> >> What do you mean by "oversteps"? > > Don't try to play 'stupid' with me. You understood me quite well, have > 'surprisingly' completely ignored my text in order to construct a > straw man and are a live demonstration of the exact type of behaviour > I criticized. And very rightfully, sine I have to read quite lot of > code I didn't write myself and I claim to know something about the > problems the people who enjoy creating 'bunte Getueme' actually > fabricate. No, I claimed you code was wrong whereas pete's was right. -- Ben. |