473,322 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How would you position {} and why?

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.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #1
7 1291
Morten Wennevik wrote:
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.
Nov 15 '05 #2
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
Nov 15 '05 #3
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 {}..

---
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opr0bd82sxhntkfz@localhost...
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.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #4
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 :)

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opr0bd82sxhntkfz@localhost...
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.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #5
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.

"Jeroen Smits" <jeroen AT bc-br DOT com> a écrit dans le message de
news:uW*************@TK2MSFTNGP11.phx.gbl...
Morten Wennevik wrote:
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.

Nov 15 '05 #6
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.

Morten Wennevik wrote:
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.

Nov 15 '05 #7
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
"Ray Hsieh (Ray Djajadinata)" <ch***@my.signature> wrote in
news:eB**************@TK2MSFTNGP11.phx.gbl:
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.

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Felix Natter | last post by:
hi, I have a structure like: <topic name="xxx"> <subtopic name="subxxx"> <subsubtopic name="subsubxxx"> </subsubtopic> </subtopic> </topic>
3
by: Markus Ernst | last post by:
Hello Reading the follwing document: http://www.w3.org/TR/WD-positioning-970131#In-flow it seems very clear that position:relative should be relative to the parent element. So in the following...
8
by: Jaime Rios | last post by:
Hi, I created a COM AddIn for Word that performs the functions that it needs to, but I needed to add the ability for the toolbar created by the COM AddIn to remember it's last position and...
3
by: akunamatata | last post by:
Hello everyone, I contact this discussiongroup because I encountered a little problem with XSL. Let me explain it: I have following file "position.xml": <?xml version="1.0"?>...
4
by: Guy | last post by:
Hi, I read an XML file to an XMLDocument and iterate through its nodes. How do I get the XPath position (index) of a certain element? For example If I on the second "b" node I want to get "2": ...
6
by: Gérard Talbot | last post by:
Hello fellow stylers, When trying this page http://www.gtalbot.org/BrowserBugsSection/PercentualRelativePositioning.html I get different rendered layouts with IE 6, IE 7 beta 2, Firefox...
4
by: Petra Meier | last post by:
Hi, I use the common code to determine the position of an element getTop = function(o){ var nTop = null; if(o){ do{ nTop += o.offsetTop;
2
by: agbee1 | last post by:
Hello: I've finally made the effort to ween myself from overly using tables and use CSS for my positioning. However, I am having a problem with my navigational menu properly aligning in Firefox,...
0
by: crisscross27 | last post by:
Hi, I found a page called "myflashfetish" where you chan choose mp3 players for my space, well the problem is this, I wanted to place 2 or more players in myspace in a particular place, I read...
1
by: spynx | last post by:
import javax.swing.JOptionPane; import java.io.*; import java.util.Arrays; public class election5 { public static void main( String args ) {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.