How would you position {} and why?

  • Thread starter Thread starter Morten Wennevik
  • Start date Start date
M

Morten Wennevik

A bit non topic but ...
When using code blocks which do you prefer

while( condition ) {
stuff
}

or

while( condition )
{
stuff
}

I prefer the latter version. I like to keep the curly braces on the same
indentation level.
But out of curiosity, what do YOU prefer?

Also in a do while which one would you prefer

do {
stuff
} while( condition );

do {
stuff
}
while( condition );

do
{
stuff
} while( condition );

do
{
stuff
}
while( condition );

I would prefer having the braces on the same indentation and alone.
 
Morten said:
A bit non topic but ...
When using code blocks which do you prefer

while( condition ) {
stuff
}

or

while( condition )
{
stuff
}

I prefer the latter version. I like to keep the curly braces on the
same indentation level.
But out of curiosity, what do YOU prefer?

Well, until a year ago I used the first version. But most code samples use
the 2nd version. Also VS.NET restructures using the 2nd version. (Yes, I
know you can change that in the settings, but I didn't know it back then).
Now I'm so used to the 2nd version that I actualy prefere it these days. I
think is more clear and easier to read the code.
Also in a do while which one would you prefer

do {
stuff
} while( condition );

do {
stuff
}
while( condition );

do
{
stuff
} while( condition );

do
{
stuff
}
while( condition );

I would prefer having the braces on the same indentation and alone.

Same here, last version. Same reason as for the previous. Clearer and easier
to read.
 
The team of developers here has settled on a coding standard of

if (----) {

}
else {


}

Not sure why really though, I am used to having from Delphi;

if (-----) then begin


end
else begin


end;

So I do not mind using it the way that we have.


Martin
 
Don't forget:

while (condition)
{
stuff
}

I don't use it (I prefer the second option below), but I've worked with some
who prefer it because then the indentation is the same whether a single
statement or a block of code is inside the {}..

---
A bit non topic but ...
When using code blocks which do you prefer

while( condition ) {
stuff
}

or

while( condition )
{
stuff
}

I prefer the latter version. I like to keep the curly braces on the same
indentation level.
But out of curiosity, what do YOU prefer?

Also in a do while which one would you prefer

do {
stuff
} while( condition );

do {
stuff
}
while( condition );

do
{
stuff
} while( condition );

do
{
stuff
}
while( condition );

I would prefer having the braces on the same indentation and alone.
 
option 2.

I don't beleive the ') {' of option 1 is a worthwhile saving in typing and
it IMO doesn't add to the readability. Whitespace costs nothing so make sure
there's enough to make the code look good :)

A bit non topic but ...
When using code blocks which do you prefer

while( condition ) {
stuff
}

or

while( condition )
{
stuff
}

I prefer the latter version. I like to keep the curly braces on the same
indentation level.
But out of curiosity, what do YOU prefer?

Also in a do while which one would you prefer

do {
stuff
} while( condition );

do {
stuff
}
while( condition );

do
{
stuff
} while( condition );

do
{
stuff
}
while( condition );

I would prefer having the braces on the same indentation and alone.
 
Strange (or many not so). I followed the exact same path as Jeroen. I have
written the opening curly brace at the end of line for many many years (in
C, C++ and Java), but about a year ago, I switched to the "all curly braces
at the beginning of the line" version.

I think that this has to do with screen resolution and tools. The compact
notation lets you see more code at once if you have a low-res screen. But
with today screens and with features like plan mode, you see enough code
anyway and the extra line does not hurt.

On the other hand, I am very picky on spacing, and I write:

while (condition)

rather than:

while( condition )

The rules that I use here are:

* language keywords are followed by a space, but not method names, so I
write obj.method(arg)
* no space between parentheses and their contents (AFAIK, this is a
classical typographical rule, just open any book).

Bruno.
 
Definitely

while(condition) {
stuff
}

and this

do {
stuff
} while( condition );

same goes for everything else, so for a try catch:

try {
} catch(Ex1 ex) {
} catch(Ex2 ex) {
}

and so on. My story is the opposite of most of the people here I think:
I started with Borland Turbo C++ and I used option 2, because I had just
switched from Pascal back then, so I just put the {} where I usually put
the open-close keyword (heck, it's been so long I can't remember what
the keywords are anymore. Was it Begin - End? I'm not sure).

Then later I read Bruce Eckel's book Using C++, in which he wrote about
why he used the no-newline-before-opening-brace style (i.e.: option 1).
Most of the reasons are about space (also he gives lots of seminar, so
even one line can matter a lot for his slides), but that was good enough
for me. Plus all the subsequent C++ books I read use that convention as
well.

So I started using 1, and never go back. Also then I moved to Java
before .NET, where option 1 is more common, thanks to the fact that you
have more than one choice for IDE in Java, plus they're not *that*
insistent on making you use option 2 like VS, in which every generated
piece of code by default uses 2.

A question to VS.NET users: How do you set it in VS.NET so that it'll
use option 1 when it *creates* a new file? I know I can format it, but I
don't want to have to format a file I just created everytime.
 
Ray

To change the way the default C# projets are formatted, you just need to
change the templates that are used to create them.
You will find the templates in

X:\Program Files\Microsoft Visual Studio .NET 2003\VC#\VC#Wizards\

And will find the templates under <Wizard Name>\Templates\1033\*.cs

The main wizards you want to change are probably:
CSharpEXEWiz - Windows Application Project
CSharpDLLWiz - Class Library Project
CSharpConsoleWiz - Console Application Project

You may also want to edit the template CSharpAddWinFormWiz, or any other
"Add" Wizards for items you regularly add to your projects.


Scott Gaitskell
 
Back
Top