By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,401 Members | 1,158 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,401 IT Pros & Developers. It's quick & easy.

Preferred coding style

P: n/a
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
Share this Question
Share on Google+
18 Replies


P: n/a
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

P: n/a
#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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.