View Poll Results: Which style of if do you use?

Voters
63. You may not vote on this poll
  • Style #1

    18 28.57%
  • Style #2

    37 58.73%
  • Style #3

    6 9.52%
  • Other

    3 4.76%
Multiple Choice Poll.

Thread: Which style of if loops is most common?

  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057

    Which style of if loops is most common?

    Which style do programmers use more often?

    Style #1:
    Code:
    if(1) {
        /* code */
    }
    Style #2:
    Code:
    if(1)
    {
        /* code */
    }
    Style #3:
    Code:
    if(1)
        {
        /* code */
        }
    I personally use style #1.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #2
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    I use #1 also.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    C'mon, it's a poll! Put your preference in.

    [edit]
    dra, I mean.
    [/edit]
    Last edited by dwks; 08-20-2005 at 08:57 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    first off, if is not a loop.
    i use
    Code:
    if (1) {
    
    }

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    http://www.cs.bris.ac.uk/Teaching/Re...yle/style.html
    Code:
    Allman/BSD    Horstmann       GNU           Whitesmith    K&R
                     		  
    if (b)        if (b)          if (b)        if (b)        if (b) {
    {             {  x = 1;         {              {             x = 1;
       x = 1;        y = 2;           x = 1;       x = 1;        y = 2;
       y = 2;     }                   y = 2;       y = 2;     } else {
    }             else              }              }             z = 3;
    else          {  z = 3;       else          else          }
    {             }                 {              {
       z = 3;                         z = 3;       z = 3;
    }                               }              }
    Mine (Allman/BSD with more spaces):
    Code:
    if ( b )
    {
       x = 1;
       y = 2;
    }
    else
    {
       z = 3;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    Mine is K&R except I put the else on a new line.

    Code:
    if (1){
      printf("Hi\n");
    }
    else{
      printf("Bye\n");
    }
    btw: whitesmith and gnu are really ugly

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I have always preferred Allman, and had a really nice link to a site which had research on it demonstrating that Allman was better understood and created less errors to someone reading it. Sadly, I lost the link and have never been able to find it again.

    K+R is an antique format that was devised to save paper when programs were printed out.

    Of course, professionally, which format you use is often dictated by the coding standards in force at the company. I have successfully argued however, that people who are writing in an alien style are more likely to make mistakes and that therefore they should develop in their own style and that a code formatter be used to translate all of the completed work to the required format.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    I used to use Allman but these days I use K&R. It looks nicer and I can still read it just as well. Also you'll find, thanks to linux, that lots of open source programs use that style.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Allman.

    This Horstmann fellow was a sick, sick man...
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Quote Originally Posted by dwks
    C'mon, it's a poll! Put your preference in.

    [edit]
    dra, I mean.
    [/edit]
    woops! sorry about that. fixed.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I pretty much stick to K&R for my own work, and whatever my current client dictates for their work (mostly Allman or derivatives).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    K&R all the way, unless working in a team, in which case the decided style takes precedence (which is usually Allman).
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i'm funny...if i'm trying to write rock solid, inpenetrable code, i use Allman...but if my primary goal is speed and performance, i use K&R...not in the same source file of course..i typically just default to Allman though.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Perspective, what style do you use? I see you voted "Other".
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by dwks
    Perspective, what style do you use? I see you voted "Other".
    Like your number #1 but with a space before the opening bracket, as in my previous example. Its basically K&R but with the else on a new line and sometimes a little extra space to avoid clutter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to handle multiple cases which use common code?
    By tmaxx in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 07:42 AM
  2. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. WS_EX_COMPOSITED style (double buffering) problems
    By JasonD in forum Windows Programming
    Replies: 2
    Last Post: 10-12-2004, 11:21 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM