C Board  

Go Back   C Board > Community Boards > General Discussions

View Poll Results: Which style of if do you use?
Style #1 18 28.57%
Style #2 37 58.73%
Style #3 6 9.52%
Other 3 4.76%
Multiple Choice Poll. Voters: 63. You may not vote on this poll

Reply
 
LinkBack Thread Tools Display Modes
Old 08-20-2005, 08:28 PM   #1
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
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.
dwks is offline   Reply With Quote
Old 08-20-2005, 08:30 PM   #2
dra
Weak.
 
dra's Avatar
 
Join Date: Apr 2005
Posts: 166
I use #1 also.
dra is offline   Reply With Quote
Old 08-20-2005, 08:35 PM   #3
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
C'mon, it's a poll! Put your preference in.

[edit]
dra, I mean.
[/edit]
__________________
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.

Last edited by dwks; 08-20-2005 at 08:57 PM.
dwks is offline   Reply With Quote
Old 08-20-2005, 09:39 PM   #4
Crazy Fool
 
Perspective's Avatar
 
Join Date: Jan 2003
Location: Canada
Posts: 2,596
first off, if is not a loop.
i use
Code:
if (1) {

}
Perspective is offline   Reply With Quote
Old 08-20-2005, 09:42 PM   #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.*
Dave_Sinkula is offline   Reply With Quote
Old 08-21-2005, 12:16 AM   #6
Registered User
 
Join Date: May 2004
Posts: 1,362
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
sand_man is offline   Reply With Quote
Old 08-21-2005, 02:56 AM   #7
It's full of stars
 
adrianxw's Avatar
 
Join Date: Aug 2001
Posts: 4,833
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.
adrianxw is offline   Reply With Quote
Old 08-21-2005, 03:04 AM   #8
Registered User
 
Join Date: Jan 2002
Location: Cardiff
Posts: 2,219
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.
Brian is offline   Reply With Quote
Old 08-21-2005, 06:28 AM   #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.
Zach L. is offline   Reply With Quote
Old 08-21-2005, 06:58 AM   #10
dra
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.
dra is offline   Reply With Quote
Old 08-21-2005, 08:08 AM   #11
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,680
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.

Salem is offline   Reply With Quote
Old 08-21-2005, 08:12 AM   #12
Supermassive black hole
 
ahluka's Avatar
 
Join Date: Jul 2005
Location: South Wales, UK
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
ahluka is offline   Reply With Quote
Old 08-21-2005, 10:41 AM   #13
Registered User
 
Join Date: Sep 2004
Posts: 720
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.
__________________
Quote:
i seem to have GCC 3.3.4
But how do i start it?
I dont have a menu for it or anything.
misplaced is offline   Reply With Quote
Old 08-21-2005, 11:48 AM   #14
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
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.
dwks is offline   Reply With Quote
Old 08-21-2005, 12:26 PM   #15
Crazy Fool
 
Perspective's Avatar
 
Join Date: Jan 2003
Location: Canada
Posts: 2,596
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.
Perspective is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:05 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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