Preferred coding style

C

craig

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
{

}
 
N

Noah Coad [MVP .NET/C#]

I would wholeheartedly agree!!

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

Nicholas Paldino said:
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]
- (e-mail address removed)

craig said:
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
{

}
 
R

Richard A. Lowe

#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
 
J

Jon Skeet

craig said:
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/jargon/i/indentstyle.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.
 
J

Jay B. Harlow [MVP - Outlook]

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
 
G

Grant Richins [MS]

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. :(
 
J

Jay B. Harlow [MVP - Outlook]

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
 
M

Mike

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
 
C

craig

EXCELLENT TIP!!!!!!!!!!!!!

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

Thanks.

Mike said:
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 said:
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.



why.
Are
 
N

Niall

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
 
S

Simon Trew

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.
 
S

Simon Trew

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.
 
A

Alien

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
 
M

MikeB

Mattias Sjögren said:
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.
 
J

Jon Skeet

Simon Trew said:
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?
 
S

Simon Trew

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.
 
J

Jon Skeet

Simon Trew said:
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).
 
S

Simon Bayling

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... ;) )

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top