Re: Code Comprehension
- From: Pascal Bourguignon <pjb@xxxxxxxxxxxxxxxxx>
- Date: Fri, 25 Aug 2006 03:36:45 +0200
jj@xxxxxxxxxxxx writes:
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 or am I just a bit dim?
Learning about program proofs, assertions, etc, and program
transformation, can help.
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:
The example is badly written, even if "idiomatic" C.
void fun(char *a, const char *b) {
int apos = 0, i, j;
if (!a || !b)
return;
if((a==NULL)||(b==NULL)){return;}
/* Avoid the confusion between boolean ( ! && || )
logical (bits) ( ~ & | ^ )
and pointer ( == != *x &x ) types. */
for (i = 0; a[i]; i++) {
for(i=0;a[i]!='\0';i++){
/* Avoid confusion between boolean and characters */
for (j = 0; b[j] && a[i] != b[j]; j++)
;
for(j=0;(b[j]!='\0')&&(a[i]!=b[j]);j++);
/* idem */
if (!b[j]) {
if(b[j]!='\0'){
a[apos++] = a[i];
}
}
a[apos] = '\0';
}
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'; }
Note that any program that calls itself a compiler should produce the
same object code for both these versions.
When a or b are NULL it does nothing.
The for(j=0;...) loop finds the position of the first character in b
that is not a[i]. If there is none (when j reaches the length of b,
b[j]='\0'), the the ith character of a is copied over a the beginning of a.
At the end, the new end of the string a is marked at apos.
So, this function removes from a all the characters that are not in b.
char a[]="Hello World!";
fun(a,"abcdefghijklmnopqrstuvwxyz");
assert(strcmp(a,"elloorld")==0);
I would have written it as:
void remove_characters(char* string,const char* bag){
int dst,src;
if((string=NULL)||(bag==NULL)){return;} /* Actually no, my _strings_
are not NULL! */
for(dst=0,src=0;a[src]!='\0';src++){
if(strchr(bag,string[src])!=NULL){
string[dst++]=string[src];}}
string[dst]='\0';}
--
__Pascal Bourguignon__
.
- Follow-Ups:
- Re: Code Comprehension
- From: Rob Thorpe
- Re: Code Comprehension
- From: Steve O'Hara-Smith
- Re: Code Comprehension
- From: Logan Shaw
- Re: Code Comprehension
- References:
- Code Comprehension
- From: jj
- Code Comprehension
- Prev by Date: Re: Code Comprehension
- Next by Date: Re: Code Comprehension
- Previous by thread: Re: Code Comprehension
- Next by thread: Re: Code Comprehension
- Index(es):
Relevant Pages
|