Unnecessary parenthesis

Ray Lubinsky rwl at uvacs.CS.VIRGINIA.EDU
Fri Jul 8 23:36:56 AEST 1988


In article <8209 at brl-smoke.ARPA>, gwyn at brl-smoke.ARPA (Doug Gwyn ) writes:
> My main objection to excessive parentheses is that they
> make the code less readable, not more.  There are a few
> cases where the C precedence rules run counter to intuition,
> and in such cases sparing use of technically redundant
> parentheses can help the code reader.  However, they should
> not be used just because the code WRITER is unsure.  (Most C
> programmers I know have a copy of the chart from K&R 1st Ed.
> p. 49 taped up near their terminal.)

When I read code I tend to browse, slowing down for the parts the require more
thought.  Something like

	if (a < b && b < c && c < d && d < e)

is going to stop my scanning a lot more often than

	if ((a < b) && (b < c) && (c < d) && (d < e))

NOT because I am unaware of C precedence rules but because the first example is
devoid of visual structure.  Maybe your eye can parse the first as easily as
the second, but I have to drop into look-at-it-more-closely mode.  If this
happens to often it reduces my overall comprehension of the code.  I'm sure
we'd both agree that the FULL parenthesization of the expression:

	if ((((a < b) && (b < c)) && (c < d)) && (d < e))

would be abominable -- in my case because it makes the expression hard to
scan without thinking about it.

As for the "return" business I think it's more clear that

	return a + b * c;

is returning a value when it's written as

	return(a + b * c);  OR EVEN  return(a+(b*c));

because in scanning mode I expect a non-void function to return an expression
and I catch the concept of here-is-an-expression a lot more quickly when I see
those entirely unnecessary parentheses.  I also think that entirely
unnecessary white space helps readability, but a little execess
parenthesization can be an acceptable substitute.

-- 
| Ray Lubinsky,                    UUCP:      ...!uunet!virginia!uvacs!rwl    |
| Department of                    BITNET:    rwl8y at virginia                  |
| Computer Science,                CSNET:     rwl at cs.virginia.edu  -OR-       |
| University of Virginia                      rwl%uvacs at uvaarpa.virginia.edu  |



More information about the Comp.lang.c mailing list