473,320 Members | 1,987 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,320 software developers and data experts.

Preferred coding style

I am curious about how many of you prefer style 1 vs. style 2, and why. Are
there names for these style?

style 1:

method {

}
style 2:

method
{

}
Nov 13 '05 #1
18 2515
I would wholeheartedly agree!!

-Noah Coad
Microsoft MVP & MCP (.NET/C#)

"Nicholas Paldino [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:Od**************@TK2MSFTNGP10.phx.gbl...
Craig,

Personally, I use style 2. It's just a preference. I don't know if
there is a name for those styles, but I prefer to call style 1 "ugly" and
style 2 "pretty". =)

In all seriousness though, I find for myself that it is easier for me to distinguish code blocks by using style 2.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"craig" <e@mail.com> wrote in message
news:et**************@tk2msftngp13.phx.gbl...
I am curious about how many of you prefer style 1 vs. style 2, and why.

Are
there names for these style?

style 1:

method {

}
style 2:

method
{

}


Nov 13 '05 #2
#2 - Keeping the braces on the same vertical axis helps me
visually identify starting and ending brace pairs.

It also precludes you from identifying a statement
as 'embedded' (i.e. one line after a scoped statement with
no braces) when it is not.

And final reason, when using brace-matching in VS.NET or
elsewhere, the cursor does not jump to the right (which is
annoying in cases where I have a long line that scrolls
the first few lines of a method off the screen.

Richard
-----Original Message-----
I am curious about how many of you prefer style 1 vs. style 2, and why. Arethere names for these style?

style 1:

method {

}
style 2:

method
{

}
.

Nov 13 '05 #3
craig <e@mail.com> wrote:
I am curious about how many of you prefer style 1 vs. style 2, and why. Are
there names for these style?

style 1:

method {

}
This is usually called "K&R bracing" (Kernighan and Ritchie).
Personally I can't stand it.
style 2:

method
{

}


This is what I always use. It's called the Allman style, and I believe
it makes things much clearer than K&R.

See http://www.science.uva.nl/~mes/jargo...dentstyle.html for more
information. On the other hand, I believe it's actually wrong in the
penultimate paragraph, where it claims that most Java programmers use
K&R - I would say most use Allman.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #4
Craig,
When I started programming in C, I used style 1, as I believe that is was
the style for C (I believe its the style used by Kernighan & Ritchie in
their 'The C Programming Language' book).

When I moved to C++, I continued using style 1. As that was the norm, but it
did not seem 'natural'.

I slowly found that style 2, seemed more 'natural', as I could 'see' the
blocks of code in my C++ programs.

Now in C# I always use style 2. For methods as well as all block statements
(if, while, for).

When I use C++ I also use style 2.

Hope this helps
Jay

"craig" <e@mail.com> wrote in message
news:et**************@tk2msftngp13.phx.gbl...
I am curious about how many of you prefer style 1 vs. style 2, and why. Are there names for these style?

style 1:

method {

}
style 2:

method
{

}

Nov 13 '05 #5
I personally prefer style 1 (often called K&R as mentioned in other posts),
but I seem to be far outnumbered by those who prefer style 2. :(

--
--Grant
This posting is provided "AS IS" with no warranties, and confers no rights.
"craig" <e@mail.com> wrote in message
news:et**************@tk2msftngp13.phx.gbl...
I am curious about how many of you prefer style 1 vs. style 2, and why. Are there names for these style?

style 1:

method {

}
style 2:

method
{

}

Nov 13 '05 #6
Craig
when you have several nested code blocks that span several pages so that you can't see both the start and the end of the code blocks on the screen at the same time.
I generally try to avoid long methods, which avoids this problem.

Its something that Martin Fowler http://www.martinfowler.com has mentioned
once or twice in his writtings.

Hope this helps
Jay
"craig" <e@mail.com> wrote in message
news:u2**************@TK2MSFTNGP10.phx.gbl... Thanks for all the responses. Its great to see that the consensus appears
to be the same as what I prefer as well, style 2. It seems that alot of the sample code that I see in books and out on the web uses style 1, however, so I was wondering if maybe I was missing something in prefering style 2.

The one thing that I notice is still kind of difficult using either style is when you have several nested code blocks that span several pages so that you can't see both the start and the end of the code blocks on the screen at the same time. If you want to do something like add a new method, it can be
tough to tell at what level within the code blocks to insert the method,
because all you can see is something like the following:

}
}
}
}

The only way to tell which bracket corresponds to which code block is to
scroll back and forth between the start and the end of the block while
trying to figure out the alignment. I suppose this may be an area where VB shines because each of these block endings would be labeled, rather than
just appearing as a bracket.

"craig" <e@mail.com> wrote in message
news:et**************@tk2msftngp13.phx.gbl...
I am curious about how many of you prefer style 1 vs. style 2, and why.

Are
there names for these style?

style 1:

method {

}
style 2:

method
{

}


Nov 13 '05 #7
> The only way to tell which bracket corresponds to which code block is to
scroll back and forth between the start and the end of the block while
trying to figure out the alignment.
When trying to decide which opening brace matches a closing brace in Visual
Studio, I use the Ctrl + ] key combination to jump back and forth between
them. To use it, place the cursor next to one of the braces before pressing
Ctrl + ]. The cursor will then jump to the matching brace.
-Mike

"craig" <e@mail.com> wrote in message
news:u2**************@TK2MSFTNGP10.phx.gbl... Thanks for all the responses. Its great to see that the consensus appears
to be the same as what I prefer as well, style 2. It seems that alot of the sample code that I see in books and out on the web uses style 1, however, so I was wondering if maybe I was missing something in prefering style 2.

The one thing that I notice is still kind of difficult using either style is when you have several nested code blocks that span several pages so that you can't see both the start and the end of the code blocks on the screen at the same time. If you want to do something like add a new method, it can be
tough to tell at what level within the code blocks to insert the method,
because all you can see is something like the following:

}
}
}
}

The only way to tell which bracket corresponds to which code block is to
scroll back and forth between the start and the end of the block while
trying to figure out the alignment. I suppose this may be an area where VB shines because each of these block endings would be labeled, rather than
just appearing as a bracket.

"craig" <e@mail.com> wrote in message
news:et**************@tk2msftngp13.phx.gbl...
I am curious about how many of you prefer style 1 vs. style 2, and why.

Are
there names for these style?

style 1:

method {

}
style 2:

method
{

}


Nov 13 '05 #8
EXCELLENT TIP!!!!!!!!!!!!!

I wish I would have known his a long time ago!

Thanks.

"Mike" <mi**@bogus.net> wrote in message
news:OA*************@TK2MSFTNGP10.phx.gbl...
The only way to tell which bracket corresponds to which code block is to
scroll back and forth between the start and the end of the block while
trying to figure out the alignment.
When trying to decide which opening brace matches a closing brace in

Visual Studio, I use the Ctrl + ] key combination to jump back and forth between
them. To use it, place the cursor next to one of the braces before pressing Ctrl + ]. The cursor will then jump to the matching brace.
-Mike

"craig" <e@mail.com> wrote in message
news:u2**************@TK2MSFTNGP10.phx.gbl...
Thanks for all the responses. Its great to see that the consensus appears to be the same as what I prefer as well, style 2. It seems that alot of the
sample code that I see in books and out on the web uses style 1, however, so
I was wondering if maybe I was missing something in prefering style 2.

The one thing that I notice is still kind of difficult using either
style is
when you have several nested code blocks that span several pages so that

you
can't see both the start and the end of the code blocks on the screen at

the
same time. If you want to do something like add a new method, it can be
tough to tell at what level within the code blocks to insert the method,
because all you can see is something like the following:

}
}
}
}

The only way to tell which bracket corresponds to which code block is to
scroll back and forth between the start and the end of the block while
trying to figure out the alignment. I suppose this may be an area where

VB
shines because each of these block endings would be labeled, rather than
just appearing as a bracket.

"craig" <e@mail.com> wrote in message
news:et**************@tk2msftngp13.phx.gbl...
I am curious about how many of you prefer style 1 vs. style 2, and

why. Are
there names for these style?

style 1:

method {

}
style 2:

method
{

}



Nov 13 '05 #9
Style 1. Style 2 takes up unnecessary space, and in my opinion looks very
ugly.

"craig" <e@mail.com> wrote in message
news:et**************@tk2msftngp13.phx.gbl...
I am curious about how many of you prefer style 1 vs. style 2, and why. Are there names for these style?

style 1:

method {

}
style 2:

method
{

}

Nov 13 '05 #10
Nothing is stopping you putting a comment next to the bracket marking what
it's ending if you want... so you can mark the end of a loop and end of a
function, and all the rest are ifs, switch, etc... whatever works.

ctrl-] sounds cool

Niall

"craig" <e@mail.com> wrote in message
news:u2**************@TK2MSFTNGP10.phx.gbl...
Thanks for all the responses. Its great to see that the consensus appears
to be the same as what I prefer as well, style 2. It seems that alot of the sample code that I see in books and out on the web uses style 1, however, so I was wondering if maybe I was missing something in prefering style 2.

The one thing that I notice is still kind of difficult using either style is when you have several nested code blocks that span several pages so that you can't see both the start and the end of the code blocks on the screen at the same time. If you want to do something like add a new method, it can be
tough to tell at what level within the code blocks to insert the method,
because all you can see is something like the following:

}
}
}
}

The only way to tell which bracket corresponds to which code block is to
scroll back and forth between the start and the end of the block while
trying to figure out the alignment. I suppose this may be an area where VB shines because each of these block endings would be labeled, rather than
just appearing as a bracket.

"craig" <e@mail.com> wrote in message
news:et**************@tk2msftngp13.phx.gbl...
I am curious about how many of you prefer style 1 vs. style 2, and why.

Are
there names for these style?

style 1:

method {

}
style 2:

method
{

}


Nov 13 '05 #11
I think books and coding examples sometimes prefer style 1 because it takes
up less on the printed page i.e. you are not wasting a line simply for an
opening brace. Of course in real coding that is not significant so I prefer
style 2.

Incidentally I used to do things like this:

if (x) y= z;

or:
if (x)
y = z;

whereas nowadays I always do:

if (x)
{
y = z;
}

That might sound stupid but I think it is much easier to see the start and
end than without the braces.

One thing I dislike about C# is I can't write things like

while (x)
{
null;
}

Which I've grown up with as a convention in C++ to be saying "I've
intentionally left this statement empty" which

while (x);

would not necessarily show so obviously.

For very short lines I will use the first form if I think it adds clarity
e.g.

if (x && y) a = 1;
if (x && z) a = 174;
if (y && z) a = 43;

Of course this is contrived but I assume you get the point i.e. horizontal
alignment can be a good clue too.

S.

craig <e@mail.com> wrote in message
news:u2**************@TK2MSFTNGP10.phx.gbl...
Thanks for all the responses. Its great to see that the consensus appears
to be the same as what I prefer as well, style 2. It seems that alot of the sample code that I see in books and out on the web uses style 1, however, so I was wondering if maybe I was missing something in prefering style 2.

The one thing that I notice is still kind of difficult using either style is when you have several nested code blocks that span several pages so that you can't see both the start and the end of the code blocks on the screen at the same time. If you want to do something like add a new method, it can be
tough to tell at what level within the code blocks to insert the method,
because all you can see is something like the following:

}
}
}
}

The only way to tell which bracket corresponds to which code block is to
scroll back and forth between the start and the end of the block while
trying to figure out the alignment. I suppose this may be an area where VB shines because each of these block endings would be labeled, rather than
just appearing as a bracket.

"craig" <e@mail.com> wrote in message
news:et**************@tk2msftngp13.phx.gbl...
I am curious about how many of you prefer style 1 vs. style 2, and why.

Are
there names for these style?

style 1:

method {

}
style 2:

method
{

}


Nov 13 '05 #12
I'm not going to challenge your opinion about ugliness since I think that it
is unarguable either way, but I can't see why you care about the amount of
space it takes.

I assume you don't care about how much space it takes on disk for all those
extra tabs. And in printed matter yes it is important that it takes up
space, but who cares how much space it takes up on the screen? And how many
people print their listings these days since we lost continuous paper
printouts? (Give me a DEC chain printer and I will play you a tune.)

So it would only matter if you actually ever got the whole dam block on the
screen, and the amount of real estate taken up by god knows what useless
stuff (which you get rid of and then comes back like a ghost two seconds
later) means you can never see the whole lot in one go anyway, and one line
extra is going to make very little difference.

I had more real estate with "vi" on an 80x32 monitor than I get these days
with .NET...

S.

Alien <al***@sympatico.ca> wrote in message
news:DX*******************@news04.bloor.is.net.cab le.rogers.com...
Style 1. Style 2 takes up unnecessary space, and in my opinion looks very
ugly.

"craig" <e@mail.com> wrote in message
news:et**************@tk2msftngp13.phx.gbl...
I am curious about how many of you prefer style 1 vs. style 2, and why.

Are
there names for these style?

style 1:

method {

}
style 2:

method
{

}


Nov 13 '05 #13
Space is space, you're right that it doesn't take up MUCH, but it does
accumulate, especially if you have a lot of opening
methods/ifs/loops/whatever. With style 2 I'm able to see a few extra lines
on the screen at any given time, which is a benefit. And I don't find it any
harder to read.

+ I find it more aesthetically pleasing :)

But hey, preferences change....maybe in a couple years, as hard as it is to
imagine, I'll "see the light" and switch..who knows
"Simon Trew" <ten.egnaro@werts> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I'm not going to challenge your opinion about ugliness since I think that it is unarguable either way, but I can't see why you care about the amount of
space it takes.

I assume you don't care about how much space it takes on disk for all those extra tabs. And in printed matter yes it is important that it takes up
space, but who cares how much space it takes up on the screen? And how many people print their listings these days since we lost continuous paper
printouts? (Give me a DEC chain printer and I will play you a tune.)

So it would only matter if you actually ever got the whole dam block on the screen, and the amount of real estate taken up by god knows what useless
stuff (which you get rid of and then comes back like a ghost two seconds
later) means you can never see the whole lot in one go anyway, and one line extra is going to make very little difference.

I had more real estate with "vi" on an 80x32 monitor than I get these days
with .NET...

S.

Alien <al***@sympatico.ca> wrote in message
news:DX*******************@news04.bloor.is.net.cab le.rogers.com...
Style 1. Style 2 takes up unnecessary space, and in my opinion looks very ugly.

"craig" <e@mail.com> wrote in message
news:et**************@tk2msftngp13.phx.gbl...
I am curious about how many of you prefer style 1 vs. style 2, and
why. Are
there names for these style?

style 1:

method {

}
style 2:

method
{

}



Nov 13 '05 #14

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
I am curious about how many of you prefer style 1 vs. style 2, and why.


A little bit of both. :-) I use "style 1" for statements inside a
method body (while, if, switch, for etc.), but "style 2" for methods
and types.


Heh - I'm glad to see someone agrees with my preference. Especially someone
whose postings I respect.

--
MikeB
Nov 13 '05 #15
Simon Trew <ten.egnaro@werts> wrote:
One thing I dislike about C# is I can't write things like

while (x)
{
null;
}

Which I've grown up with as a convention in C++ to be saying "I've
intentionally left this statement empty" which

while (x);

would not necessarily show so obviously.


No, but surely:

while (x)
{
// Intentionally empty
}

is clearer than either, isn't it?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #16
Yes-- as I say, in C++ (or at least as far as MS style guidelines go) "NULL"
has become a convention for saying the same thing-- I guess it also might
suppress a possible compiler warning about an empty statement block, which
the comment would probably not. For the same reason you sometimes see things
like

public void MyFunctionThatMustConformToSomeSignature(object a, object b)
{
a;
DoSomething(b);
}

The first line of which suppresses a warning about a being an unused
parameter (in the C++ equivalent, that is). In fact MFC has a macro called
UNUSED_PARAMETER that simply translates to the above form; there is no
equivalent EMPTY_STATEMENT although it would be easy enough to define one.

S.

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Simon Trew <ten.egnaro@werts> wrote:
One thing I dislike about C# is I can't write things like

while (x)
{
null;
}

Which I've grown up with as a convention in C++ to be saying "I've
intentionally left this statement empty" which

while (x);

would not necessarily show so obviously.


No, but surely:

while (x)
{
// Intentionally empty
}

is clearer than either, isn't it?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 13 '05 #17
Simon Trew <ten.egnaro@werts> wrote:
Yes-- as I say, in C++ (or at least as far as MS style guidelines go) "NULL"
has become a convention for saying the same thing-- I guess it also might
suppress a possible compiler warning about an empty statement block, which
the comment would probably not. For the same reason you sometimes see things
like

public void MyFunctionThatMustConformToSomeSignature(object a, object b)
{
a;
DoSomething(b);
}

The first line of which suppresses a warning about a being an unused
parameter (in the C++ equivalent, that is). In fact MFC has a macro called
UNUSED_PARAMETER that simply translates to the above form; there is no
equivalent EMPTY_STATEMENT although it would be easy enough to define one.


You could do:

while (x)
{
;
}

then - that doesn't raise any warnings (with the command line compiler,
anyway).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #18
I typically use style 1 because the extra braces just look like redundant
wasted lines to me. I typically glance over the indentation rather than
the bracing;

if (some test) {
callSomeFunction();
runSomeProcess();
}

Is just as easy to see what is in the block. I know that 'if', 'class',
'for', etc all indicate the start of code blocks anyway.

But then... in the spirit of experimentation, I might try style 2 for a
bit and see if the extra breathing space amongst code gives less
quantifiable benefits...

(grumble... Python doesn't have these problems... ;) )

"Simon Trew" <ten.egnaro@werts> wrote in
news:#7**************@TK2MSFTNGP11.phx.gbl:
I had more real estate with "vi" on an 80x32 monitor than I get these
days with .NET...


That's where VS.Net's Alt-Shift-Enter really shines... ;)

Simon.
Nov 13 '05 #19

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

Similar topics

5
by: BenOne© | last post by:
Hi all, If I want to have many divs with different padding, border, and colour properties, should I just set those styles in line, ie: <DIV style"..."> </DIV> or should I define all the divs...
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
2
by: Wilfried Mestdagh | last post by:
Hi, I need to access some components on my main form. I found out that making a static public variable it is accessible. It has to be from several forms, so I dont want to pass a reference ...
4
by: Mike Labosh | last post by:
I realize that you people have not seen much of me other than some framework responses I have posted. I am primarily a VB guy (yes, you can laugh) But I have lurked here for several years,...
2
by: OzzyC | last post by:
I have creating a web page at http://www.interface-web.co.uk/atyc/cssproblem.htm which is supposed to use a preferred stylesheet to style the page on the first visit and then allows the user to...
13
by: benben | last post by:
Is there an effort to unify the c++ coding standard? Especially identifier naming. Not a big issue but it would be annoying to have to incorporate different coding styles simultaneously when...
7
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already...
3
by: moondaddy | last post by:
What's the preferred (best) way to start a website project in VS 2005? I know of 2 ways and each creates a site differently and has different behavior. Open VS 2005 1) from the start page...
1
by: Rathman | last post by:
Hi, I'm trying to create a general form with multiple subforms. The subforms contain discrete pieces of information such as Customer, Work Order, etc, while the main form is simply a front-end...
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...
1
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: 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: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.