Parentheses preference

This is a discussion on Parentheses preference within the Tech Board forums, part of the Community Boards category; I work with two major styles of whitespace/parentheses at work, depending on the project I'm working. The two styles are: ...

  1. #1
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239

    Parentheses preference

    I work with two major styles of whitespace/parentheses at work, depending on the project I'm working. The two styles are:

    Style 1:

    Code:
    int x = function( arg1, arg2 );
    if( conditional )
        doIt();
    Style 2:

    Code:
    int x = function(arg1, arg2);
    if (conditional)
        doIt();
    Take careful note of whitespace, on the left and right sides of the parentheses, and in the two different contexts -- function call, vs. syntactic expression.

    In style 1, the inner contents are always set off from the parens by a single space, regardless of context. The parentheses nestle directly against the left and right exterior.

    In style 2, the parens distinguish between function context and conditional context. In function context, they nest tightly on either side, but in conditional context, they nestle with a space on the exterior, but tightly on the interior.

    It's an interesting combo to see within one company, but both styles have justifications. Opinions?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    I prefer the second style. But mostly because that's what I have been doing for years. With one exception. On the case of a single element inside the parenthesis, if this element is a single letter word I usually put spaces between the parenthesis.

    Code:
    bool x = function(arg1, arg2);
    if ( x )
        doIt();
    I do it for matters of readability. But certainly have no data to support that claim. It just feels right to me.
    Last edited by Mario F.; 07-01-2010 at 05:51 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Style 2!

    Spaces are bytes which are just wasteful buffers to the size of my source code... I have to be careful with disk space... I only have a few TB. I also only use single letter identifiers (or two letter if I've run out of single letter). My precious bytes will not be wasted so some newbie coder can read the code easier. "Waahhh!!! The code is too hard to read!!!!" NOOB!
    Last edited by SlyMaelstrom; 07-01-2010 at 09:36 AM.
    Sent from my iPad®

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Style 2. I too use single Identifiers, but often I'll use some_really_long_name then create a pointer to that (as in *s = &some_really_long_name so that i don't have to use it over and over again. But, then again, I also break lines at 80 chars so that my code can be read on any STANDARD terminal (80x25).

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Style 2, for no particular reason. Actually, I snugly nest parentheses on both sides in all contexts, except for Clang, where Style 2 is followed exactly.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Style 2.

    I HATE that there are no curly's on the if though.
    Woop?

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Well, you gonna hate his, then:

    Code:
    int x = function(arg1, arg2);
    if (conditional) doIt();
    That's how I usually do it on such short if statements
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I always use Style 1. It really annoys me to see Style 2 in code that I'm working on.
    Whitespace makes things more readable.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Not to mention it's required in some languages, yet to find one where it isn't allowed

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    I'm notorious for not keeping myself to any standard style, so I use a mix of 1/2:
    Code:
    if (x)
        y = z;
    if (x.DoSomething())
        y = z;
    // Sometimes even
    if ( x.DoSomething() )
        y = z;
    // When it gets complicated...
    if ( x.DoSomething(y.get()) )
        y = z;
    // ...I try to group them together so it's easier to see what parenthesises ends where.
    (I require a space between the if and the parenthesis, though.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    As my alter ego, I would not only use spaces, but use extra parenthesis as both a sanity check and a building block for future modification of the conditional statement.
    Code:
    if ( ( ( ( condition ) ) ( ) ) ) {
    { f_int32_This_Is_The_Function_I_Will_Use_That_Does_It(); }
    }
    Last edited by SlyMaelstrom; 07-02-2010 at 07:15 AM.
    Sent from my iPad®

  12. #12
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by SlyMaelstrom View Post
    As my alter ego, I would not only use spaces, but use extra parenthesis as both a sanity check and a building block for future modification of the conditional statement.
    Code:
    if ( ( ( ( condition ) ) ( ) ) ) {
    { f_int32_This_Is_The_Function_I_Will_Use_That_Does_It(); }
    }
    I know people who write code like this -- but they don't use the _ in the names
    Code:
    ThisIsTheFunctionThatMakesTheSquareOnTheUpperRightSideOfTheLayout()
    I'm like "DUDE! Why don't you just do something line makebox(struct rect *where, int screen)!?!?"

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Readability 9
    Visibility 10

    I'm sold
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I'd say style two, except for the silly space after the "if". I really never got that one, I don't think it increases readability at all. Of course, that's just a matter of opinion.

  15. #15
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    958
    Style 2.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Value initialization using an empty pair of parentheses
    By zephon in forum C++ Programming
    Replies: 43
    Last Post: 09-07-2009, 11:58 AM
  2. Parentheses checker with stacks
    By cannsyl in forum C++ Programming
    Replies: 11
    Last Post: 12-05-2008, 01:17 PM
  3. Parentheses in IF statements.
    By thetinman in forum C++ Programming
    Replies: 11
    Last Post: 11-30-2005, 03:08 PM
  4. Using a Calculator - Formula Input Preference
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-21-2004, 04:38 PM
  5. Nesting Parentheses
    By XZSNPP in forum C++ Programming
    Replies: 6
    Last Post: 01-18-2003, 09:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21