The Nightstar Zoo

Nightstar IRC Network - irc.nightstar.net
It is currently Wed Aug 16, 2017 8:50 pm

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 13 posts ] 

Which style do you use?
hanging brace, no spaces 0%  0%  [ 0 ]
hanging brace, space after if 69%  69%  [ 9 ]
hanging brace, space inside parens 8%  8%  [ 1 ]
newline brace, no spaces 0%  0%  [ 0 ]
newline brace, space after if 15%  15%  [ 2 ]
newline brace, space inside parens 8%  8%  [ 1 ]
Total votes : 13
Author Message
PostPosted: Sun Oct 02, 2005 12:02 am 
Offline
Nightstar Graveyard Daemon
User avatar

Joined: Mon Jun 03, 2002 8:30 pm
Posts: 1071
Location: Wouldn't you rather observe my Velocity?
Code:
// hanging brace:
if(x) {
    do_something();
    do_something_else();
}

// newline brace:
if(x)
{
    do_something();
    do_something_else();
}

// spacing inside parens:
if( x ) {
    do_something();
    do_something_else();
}

// space after if
if (x) {
    do_something();
    do_something_else();
}


..just curious. I used to be a newline brace, no spaces guy. I changed to space inside parens 6 years ago, then to hanging brace 2 years ago. Recently I've begun considering changing to space after if.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 12:09 am 
Offline
Energizer Bunny
User avatar

Joined: Wed May 22, 2002 12:24 am
Posts: 1634
Code:
if (x) {
  doSomething();
  doSomethingElse();
} else if (y) {
  doTheOtherThing();
} else {
  wasThePrideOfThePeaches();
}


Vorn


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 8:41 pm 
Why change to hanging brace, out of curiosity? I've always viewed it as a throwback to when screen space was limited. I tend to find less code density makes it easier to read, possibly for the same reason that double-spaced text is easier to read.

I tried spaces within parenthases for a while but it tended to uglify more complex code (at least to my eyes), which is where the visual cues are more helpful.

Code:
if (cond)
{
    whatever();
    no_really();
}
else
    seriously_whatever();


Top
  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 8:58 pm 
Offline
Energizer Bunny
User avatar

Joined: Wed May 22, 2002 12:24 am
Posts: 1634
When I look at code, I'm not concerned with where the block ending began, but what it began with. Hanging braces make this a bit more obvious.

Vorn


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 5:48 am 
Offline
Nightstar Graveyard Daemon
User avatar

Joined: Mon Jun 03, 2002 8:30 pm
Posts: 1071
Location: Wouldn't you rather observe my Velocity?
Raif wrote:
Why change to hanging brace, out of curiosity? I've always viewed it as a throwback to when screen space was limited.


Me too. I changed to hanging brace for a pretty silly reason: When I switched to linux, I started using emacs... and I couldn't figure out how to get emacs to indent code properly with newline braces. Emacs really, really wants you to use hanging braces. I could not for the life of me get the indent functions working properly, and I finally decided to just do it the way emacs liked and come back to it later.

Once I'd been using it for a few days, however, I decided I liked it better, even with today's larger displays.

Raif wrote:
I tend to find less code density makes it easier to read, possibly for the same reason that double-spaced text is easier to read.


Well, that's actually it's own best counterexample. For me, double-spacing a document doesn't make it any more readable, but it does make it harder to skim because there's less information on the page. Good use of paragraphs, indentation, and a clear writing style have a far greater effect for me than line spacing.

I hit a point a few years ago when I stopped thinking in lines of code and started thinking in blocks of code. Now anything that spreads out the code only serves to put less code on the screen at one time, which cripples my understanding. The logical unit of code that my brain likes to work with visually is currently about 50-80 lines of code. But if I can't see it--even if it's scrolled off by one measly line--I lose that immediate visual grasp. Hanging brace puts about 30% more code on the screen.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 9:04 am 
My style is similar to Vorn's:

Code:
if (x) {
    results = searchPasswords();
    emailToMe(results);
} else if (y) {
    formatHardDrive();
} else {
    spreadToOtherComputers();
}


...except that I tend to ditch the braces altogether when there is only one statement in each conditional:

Code:
if (x)
    emailToMe(searchPasswords());
else if (y)
    formatHardDrive();
else
    spreadToOtherComputers();


... (although I'm trying to break that habit).

There's no real reasoning behind my style, it's just The Way I Learned It<sup>TM</sup>.


Top
  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 9:48 am 
Offline
Concession Worker
Concession Worker
User avatar

Joined: Tue Jul 01, 2003 5:26 pm
Posts: 1479
Location: R'lyeh
Hanging brace, space after if.

Once upon a time I too used newline braces, but I found hanging braces to be more readable... the newline brace seemed to disassociate the subsequent code block from the if that preceded it.
Besides, it's aesthetically more pleasing. :)

I find that adding a space after an if also makes the code a bit more readable, as it becomes even more instantly recognizable as a conditional clause. The "if-SPACE-something"<sup>*</sup> notation also ties in rather nicely with the natural language paradigm the C keywords strive for.



<sup>*</sup> nomenclature nicked from the latest PartiallyClips. :)

_________________
Living in a state free from the burdens of privacy and democracy since 2008-06-18.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 1:04 pm 
Code:
// Code Monkeys forum handler
while (1) {
    if (TRUE == forum.isBoring()) {
        cout << new ForumPost(incite(HOLY_WAR, TYPE_HOLY_WAR_BRACE_STYLE)) << endl;
        Poke(Raif.getEgo(), new Stick(STICK_LONG | STICK_POINTY));
    } // (TRUE == forum.isBoring())
    else {
       Flame(forumUsers[indexof("CodeMonkeys")].RandomChoice());
       wait();
    } // else
} // while(1)


It's mostly the style that has always just "felt" right for me. I've adapted a couple of things to match the guidelines required at work (which the above meets), but by now it's second-nature.

Raif wrote:
Why change to hanging brace, out of curiosity? I've always viewed it as a throwback to when screen space was limited.

This is the only thing I actually took issue with in the code above. I absolutely agree with Raif's sentiment here. However, I have to wholeheartedly disagree with the implicit suggestion that screen space is no longer limited. At work, I have three 19" LCD monitors on my desk, each at a fairly comfortable 1280x1024 resolution, and even with devstoo spread across all three (BTW, the center one is twisted 90° to Portrait mode), there is still never enough screen space to view everything I want available at once.

To that end, anything that puts more code on the screen is desirable.

Oh, and anticipating Raif's response about how pathetically low 1280x1024 is, I will simply say that though I have the best eyesight money can buy, and can quite easily make out 2048x1536 on a 19" monitor, I still get eyestrain after a ten-hour stretch of staring at it. It's about comfort.


Top
  
 
PostPosted: Mon Oct 03, 2005 7:35 pm 
hanging brace, space after if

I find it easier to read. I look for indents when I'm looking for blocks of code. While I have no problem with whitespace, the brace on a line by itself adds pseudo-whitespace that's easy to visually confuse with non-functional whitespace that gets used to break up unrelated lines within the same block.

After the closing brace, I also like to have another line of whitespace unless I'm closing multiple blocks on consecutive lines (this stands out) or it's got an "else" or "catch" or similar after it (this is conceptually related so it should be close together).


Top
  
 
 Post subject:
PostPosted: Tue Oct 04, 2005 1:03 pm 
Hanging brace, space after if. Always have, always will.


Top
  
 
 Post subject:
PostPosted: Tue Oct 04, 2005 2:11 pm 
Interestingly, despite having handed us a guidelines document that specifies everything from how to name our identifiers (Java style CamelCase for classes and functions, m_* and g_* for member and global) to how we should indent (four spaces, no tab characters) to a hard limit for line width (120 characters), even my company has avoided the age-old holy war of brace style. Most of the COM developers tend toward newline brace. My team gravitates more toward hanging brace. The guidelines specify only that "Regardless of which is used, care must be taken to ensure that the style is consistent within the file."

That a software bureaucracy this size would leave the issue well alone should speak volumes about how entrenched and irrationally defensive people are about their personal styles.


Top
  
 
 Post subject:
PostPosted: Tue Oct 04, 2005 2:23 pm 
Pi wrote:
Oh, and anticipating Raif's response about how pathetically low 1280x1024 is, I will simply say that though I have the best eyesight money can buy, and can quite easily make out 2048x1536 on a 19" monitor, I still get eyestrain after a ten-hour stretch of staring at it. It's about comfort.

I wasn't about to make such a claim. :) That resolution is uncomfortably limiting to me, but I'm well aware that most people can't stand my usual resolution of 1600x1200.

Maybe I'll try this hanging brace again, though, considering its popularity.


Top
  
 
 Post subject:
PostPosted: Tue Oct 04, 2005 9:45 pm 
Raif wrote:
I wasn't about to make such a claim. :) That resolution is uncomfortably limiting to me, but I'm well aware that most people can't stand my usual resolution of 1600x1200.

LCDs are much sharper for text, and allow much smaller fonts as a result. I'd want a fairly big screen before I wanted 1600x1200.


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group