Efficient coding considered harmful?

Neil Weinstock nsw at cord.UUCP
Sat Nov 12 07:25:48 AEST 1988


In article <771 at wsccs.UUCP> dharvey at wsccs.UUCP writes:
>The recent postings on the net on this topic has prompted me to respond
>about a teacher at our college (not necessarily reflecting the opinion
>of all faculty members) who would fail me for the following code:
>
>if( ! something) {
>	++j;
>	...
>}
>
>but would pass me with flying colors for the following:
>
>if ( something != 0)
>	{
>		j = j + 1;
>		...
>	}
>
>As a matter of fact, he would under no circumstances allow me to
>use the '++', '--', '+=', '-=', '/=', '*=', or '%=' operators.

I have heard this issue thrashed out before.  I feel very strongly that
the operators you list can *enhance* readability when used appropriately, and 
therefore I would assert that the teacher you refer to is extremely misguided 
(boy, I'm being nice).

In general, when doing an increment operation or some such, I find it
convenient to think of it as an operation on a variable, not as an assignment
(fine shades of meaning, to be sure).  C's various wonderful assignment
operators reflect this naturally, and it is one of the many reasons I like C.

This issue becomes important when you get big lvalue expressions:

	array[row*2+offset][col+loopvar] += 8;

becomes

	array[row*2+offset][col+loopvar] = array[row*2+offset][col+loopvar] + 8;

This is a contrived example, but I frequently encounter such stuff.  I would 
assert that the first is infinitely more readable than the second.

So, while the operators in question are certainly abusable, they have their
place.  It is difficult to imagine using C voluntarily without them, and I
shudder to think of what some code would look like without them.

.- -- .. --. .- .-. ..- .-.. . ... .- -- .. --. .- .-. ..- .-.. . ...
|  Neil Weinstock | att!cord!nsw     | "I think my cerebellum just  |
|  AT&T Bell Labs | nsw at cord.att.com |      fused." - Calvin        |
.- -- .. --. .- .-. ..- .-.. . ... .- -- .. --. .- .-. ..- .-.. . ...



More information about the Comp.lang.c mailing list