Re: Code Comprehension



jj@xxxxxxxxxxxx wrote:

I regularly find myself staring at a bit of (usually terse) code for
frustratingly long periods of time before I can, hopefully, understand
what it does. Does this get easier with experience/practise? Is there
anything I can do to help improve things

http://groups.google.com/group/comp.lang.c/msg/aca808679c718d37
"Rewrite the code as part of reading it for the first time.
Nothing else will get you so far so fast. " --Jim Hill, Apr 3 1993


or am I just a bit dim?

Here's an example of the kind of thing I'm talking about, even knowing
what it does it takes me quite a while to figure out *how* it does it:

void fun(char *a, const char *b) {
int apos = 0, i, j;

if (!a || !b)
return;

for (i = 0; a[i]; i++) {
for (j = 0; b[j] && a[i] != b[j]; j++)
;
if (!b[j]) {
a[apos++] = a[i];
}
}

a[apos] = '\0';
}


Rewrite the code.
That's what I did:
http://groups.google.com/group/comp.lang.c/msg/199b5e83a0422c61

char *str_squeeze_f(char *s1, const char *s2)
{
char *const p1 = s1;
const char *const p2 = s2;
char *p3 = s1;

while (*s1 != '\0') {
s2 = p2;
while (*s2 != '\0' && *s1 != *s2) {
++s2;
}
if (*s2 == '\0') {
*p3++ = *s1;
}
++s1;
}
*p3 = '\0';
return p1;
}

--
pete
.