Re: Code Comprehension



Pascal Bourguignon wrote:
Logan Shaw <lshaw-usenet@xxxxxxxxxxxxx> writes:
Pascal Bourguignon wrote:

My god! This is twice as hard to read! You do know that whitespace
is allowed in C, right? :-) No wait, forget the smiley. I'm
serious.

I've not observed that whitespace improves anything. On the
contrary, they may be misleading.

Of course it can be misleading. If you TRY to communicate badly,
you can certainly succeed. Or if you do not try but merely put
in whitespace randomly without regard to what it might indicate,
you can find yourself communicating badly as well. But that's
not a very good argument that whitespace can't be used to good
effect.

Yes, that still retains some C idioms, but you have to assume
some familiarity with the language. (I noticed even you did
that, because you did not eliminate the "for" loop, which is
deep in the C idiom, and replace it with a "while" loop.)
Things like the low precedence of "||" and the fact that 0 by
itself is a legal character constant aren't very complicated
and are hard to miss if you have much experience with C.

The point is that types are important to understand code, and if you
consistenly mislead the reader by using consistenly the wrong
operations for the type, you don't help readability.

It's not the wrong type. Anyone who is familiar with the basics
of C knows that '\0' and 0 are both integral values. So where
is the inconsistently? There is none. They are different
notations for basically the same thing. The only difference is
that a 0 by itself might have an integral type with a larger
range of values than the type of '\0', but since the value of
both falls in the range of all integral types in C, that doesn't
really matter.

I've also put the ";" on its own line to draw as much attention
as possible to the fact that the "for" loop has a null body.

Well that's one strong reason why to use while instead of for. But I
reserved a more violent end to that loop anyways, substituting it with
strchr.

Yes, that is a good argument to use a while loop. Not question
about that. However, with any loop that has an empty body, I
find it preferable to put the semicolon on the following line
just to emphasize it. Either that, or sometimes it works well
to use a pair of braces, like these:

for (c = str; *c != '4' && *c != 0; c++) { }
while (*(c++) != 0) { }

- Logan
.



Relevant Pages

  • Re: Performance - 0 ticks ???
    ... Of course timing repeated execution of the operation is misleading too... ... Ian Griffiths - http://www.interact-sw.co.uk/iangblog/ DevelopMentor - http://www.develop.com/ "Frank Hileman" wrote:> By the way, Jon Skeet is correct: timing a single short operation produces> meaningless data. ... You must loop in an operation that takes a few 10s of> milliseconds, minimum, to obtain anything useful, and even that is fairly ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: unique numbers using srand( ) and rand( ) functions in C++
    ... > I'd suggest that you acquaint yourself with current standard headers ... > a total of 50 groups, which is in itself extremely misleading. ... > of numbers up to a point the loop determines. ... This must be done regardless of any ...
    (comp.lang.cpp)
  • Re: Nested Do...Loops and Date Cell Defined as Date in VBA code
    ... However the 'loop without do' message can be misleading. ... You also need to begin and end any "If - End If" statements within the same loop. ... I have three nested Do...loops and when I run the macro, ... and emissions sheet which are in workbook#1 and cooling tower spreadsheet ...
    (microsoft.public.excel)
  • Re: do/while false
    ... "Angus Graham" wrote in message ... > isn't really a loop at all which is misleading. ... The advantage of a do loop is that it is guaranteed to execute at least ...
    (comp.lang.cpp)