Unnecessary parentheses (Was: Help: VAX C problem)

Shaun Case shaunc at gold.gvg.tek.com
Wed Apr 3 08:26:28 AEST 1991


In article <1#.gqcm at rpi.edu> xor at aix01.aix.rpi.edu (Joseph Schwartz) writes:
>
>Just curious...how often do you folks purposely insert unnecessary parens
>into expressions?  Sometimes I'll do it just to make the grouping clearer
>(just in case the next person to read the code doesn't know all the
>precedence rules by heart).  I'm talking about expressions in general,
>not just in conjunction with return or sizeof.
>

I probably use more "unnecessary" parens than most people -- I go to a lot
of trouble to make sure my code is readable and maintainable.  I figure 
that if the compiler is smart enough to optimize away unnecessary parentheses,
why not use them if it make the code easier to follow?

I often have rather large compilcated compound if statements, and the format
I have found that works best for me is:

if (
        (test 1)
     || (test 2)
     || (test 3)
     || (test 4)
   )
   {
	whatever();
	whatever2();
	etc();
   }

This ensures that when someone changes the code, it doesn't break immediately
due to relying solely on precenence.   It also allows you to put a comment 
off to the side for each test case.

I also put parens around return values, because sometimes I change returns
to exit()s, and it helps to have the parens there already, rather than 
waiting for the compiler to point it out.

Of course, you didn't have to worry about things like this when you were
writing in BASIC on your C64 with 38K of free ram, since you could fill up the
entire memory space in a weekend, and be finished with your program, and never
have to look at it again... those were the days!  :-)

// Shaun //
-- 
Shaun Case:  shaunc at gold.gvg.tek.com  or  atman%ecst.csuchico.edu at RELAY.CS.NET 
 or Shaun Case of 1:119/666.0 (Fidonet)  or  1 at 9651 (WWIVnet)
---
It's enough to destroy a young moose's faith!



More information about the Comp.lang.c mailing list