Re: Code Comprehension



Logan Shaw <lshaw-usenet@xxxxxxxxxxxxx> writes:

Pascal Bourguignon wrote:
So that gives:
void fun(char *a, const char *b) {
int apos = 0, i, j;
if((a==NULL)||(b==NULL)){return;}
for(i=0;a[i]!='\0';i++){
for(j=0;(b[j]!='\0')&&(a[i]!=b[j]);j++);
if(b[j]!='\0'){
a[apos++] = a[i]; }}
a[apos]='\0'; }

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. That is visual clutter at its worst. Whitespace is
legal for a reason, and that is to help make things clearer
by allowing the formatting to be used as a tool to help show
the structure.

I've not observed that whitespace improves anything. On the
contrary, they may be misleading. Try to understand read this:

if ( 0 == a<<1 ) { printf("yes"); }else{ printf("no"); }


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.


And anyway, I really doubt the more obvious C idioms are a
significant reason why many would find this code hard to read.

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.
Putting the ";" right up against the ")", to me, just hides
that fact, which is something that needs to be highlighted.

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.

--
__Pascal Bourguignon__
.



Relevant Pages

  • Re: object system...
    ... but you shall not reason contradictory. ... while Halt loop ... Any finite halting problem is decidable, ...
    (comp.object)
  • Re: Which Is Better?
    ... The reason they gave was from a DBA perpspective like it was ... MERGE really necessary here - do you do any inserts in that loop, ... statistics for both approaches and compare them to see what could ... Did you capture any other statistics aside from run time ...
    (comp.databases.oracle.misc)
  • Re: [opensuse] BASH - howto read line in file and preserve leading whitespace?
    ... it strips the leading whitespace from each line making the subsequent write impossible. ... so, either quote, or don't quote the values on both sides of the test comparator ... then echo the entire line verbatim and skip the rest of the loop. ... Finally, if the input file happens to end at the end of the last line, then that last line will not be processed and will NOT appear in the output, because the "while read" will exit with an exit status above 0 on hitting the end of file, and so the rest of the loop will not get a chance to run that one last time. ...
    (SuSE)
  • Re: measuring clock cycles per second
    ... In fact, I am not trying to convince you, I'm trying to find a reason why you would think it's useless. ... Let's assume that a program only executes a single loop and this loop ... Even if the average execution time of #1 is exactly 1/4 of the sampling interval and the average execution time of #2 is exactly 3/4 of the sampling interval, then, provided the standard deviation of the respective probability distributions of times is nonzero, the total time taken by the loop will, in general, be unequal to the sampling interval, even if by a mere 1%. ... Rather than the poor resolution, I find the fact that measurement itself leads to changes in timing more difficult to work with, especially when -g is used (not sure about the reason). ...
    (comp.os.linux.development.apps)
  • Re: Need Thread Advice
    ... The event object concept seems more reasonable. ... The code is written so that when a button is clicked, I open a com port ... but the release code hung in the whileloop. ... reason for this is volatile typically does not have the correct memory ...
    (microsoft.public.vc.mfc)