Re: Code Comprehension
- From: Pascal Bourguignon <pjb@xxxxxxxxxxxxxxxxx>
- Date: Mon, 28 Aug 2006 02:16:40 +0200
Logan Shaw <lshaw-usenet@xxxxxxxxxxxxx> writes:
Pascal Bourguignon wrote:
Logan Shaw <lshaw-usenet@xxxxxxxxxxxxx> writes:
Sure, an array type in some other languages might be a higherThat's my point! As Knuth said, programs should be written to be
level of abstraction, but that's not true of C. Please explain
why you think C arrays provide a higher level of abstraction
than C pointers. They don't, really. The only difference
between *(a+i) and a[i] is the syntax. There is no semantic
difference at all.
read by people, and
only accessorily compiled by compilers.
I agree. And I think the pointer version is easier to read than
the array version.
The array version says "I have an array, and it has a definite
starting point, and I want to look at the character this many
past the one at the starting point". The pointer version says
"I am currently looking at this character right here --->".
With the pointer version, you can forget about the name of the
array and just concentrate on the fact that you have a character,
and that later on, you might look at the character after that.
Compare the following two. First:
a[i] = x;
i++;
a[i] = y;
i++;
And second:
*p = x;
p++;
*p = y;
p++;
To me, the lines of code in the second version are more parallel to
each other. It is easier to look at p being dereferenced and p
being incremented than it is to look at a[i] being accessed and
i being incremented. a[i] is a more complicated expression than
*p. By using a pointer instead of an array, you factor out that
detail (where the beginning of the array is) from the section of
code so that you don't need to be concerned with it when it's not
relevant. To me, that makes the code easier to read and cleaner.
And that's why I prefer it.
You get a better idea of what is going on at the processor level, ok.
But you lose semantics, the meaning of the program. You don't know
anymore that you are processing the characters of a string. Therefore
the program is more difficult to understand. Allright, you know that
here, a pointer is incremented, and there a byte is assigned. but this
doesn't help to know what this function is really doing. (All
functions move bytes and twiddle pointers, that's why the processors
always do).
You're right. That's why you should abstract away these assembler
level details.
typedef char* string; /* if you really need to implement strings are
pointers to chars... I would use a struct. */
void remove_characters(string str,const string bag){
...}
Doesn't this hide the fact that passing a "string" by value
doesn't cause the actual string to be copied? So aren't
you forming an incomplete abstraction?
Abstracting is hiding. If it hides anything, then it's a good abstraction.
How is such an abstraction really useful?
In as much as it hides irrelevant details, yes.
I would say that if you can't really abstract something, you're not
making anything easier by making it look like you are.
Sometimes abstracting consists only in renaming.
http://mitpress.mit.edu/sicp/full-text/book/book.html
--
__Pascal Bourguignon__ http://www.informatimago.com/
"Our users will know fear and cower before our software! Ship it!
Ship it and let them flee like the dogs they are!"
.
- References:
- Code Comprehension
- From: jj
- Re: Code Comprehension
- From: pete
- Re: Code Comprehension
- From: Pascal Bourguignon
- Re: Code Comprehension
- From: Logan Shaw
- Re: Code Comprehension
- From: Pascal Bourguignon
- Re: Code Comprehension
- From: Logan Shaw
- Code Comprehension
- Prev by Date: Re: Code Comprehension
- Next by Date: Re: Writing a shell program in C
- Previous by thread: Re: Code Comprehension
- Next by thread: Re: Code Comprehension
- Index(es):
Relevant Pages
|