• 0

Curly braces placement and indentation


Question

Almost all the code I've seen until now uses the following style for curly braces:

void swap(int &a, int &b)
{
	int temp = a;
	a = b;
	b = temp;
}

Visual Studio also defaults to this. However I have read Code Complete 2nd edition and Steve McConnell argues against this style. "Avoid unindented being-end pairs (...) Although this approach looks fine, it (...) doesn't show the logical structure of the code. Used this way, the begin and end aren't part of the control construct, but they aren't part of the statements after it either." Steve McConnell recommends using the pure block style, which emulates Visual Basic (where there's no curly braces):

void swap(int &a, int &b) {
	int temp = a;
	a = b;
	b = temp;
}

or this (begin-end block boundaries):

void swap(int &a, int &b) 
	{
	int temp = a;
	a = b;
	b = temp;
	}

Although I tend to agree with McConnell's reasoning, all the books I read, the classes I attended, and Visual Studio, use the first style, so I find it a bit weird. What do you think?

Recommended Posts

  • 0

I use the 2nd Style for CSS, the 1st style for every other language. Definitely the easiest to be able to quickly match up the brackets when scanning through code, for me personally anyway. Also, everywhere I've worked has used that style. One of the main things I disagreed with in the Code Complete book.

  • 0

I used to use the second style all the time, but since re-installing VS, I haven't disabled the { } bracing feature, so it just defaults to style one. Kinda stuck with that now.

If I do any work outside of it, I still tend to go with the second style.

  • 0

First style for me. I find that the first style flows better to me when I read it, and that's how I've always been taught to indent, however I use style two for Javascript as it seems to be more the defacto style for indentation. The third style I've never used, and probably never will, it looks very strange.

On a slight tangent, I also find myself doing this more often in my code:

while (!foo and index < bar)
{ foo = MyFunc(index); index++;}

As I'm writing bigger projects as I get on in life, I'm finding increasingly annoying that 2 lines of code can occupy 4 lines, and this was my solution (Anything more get's indented properly). Would this annoy you if you were reading my work?

Edited by El Sid
  • 0

first style for me, reason:

say you have something like:

if (insert expression of choice here but it's long) {
  some code
  more code
}

i need to look right across the line for the opening { whereas

if (insert expression of choice here but it's long)
{
  some code
  more code
}

i can just look under the if, also for things like:

if (insert expression of choice here but it's long)
  some one line code
some code here tooo

i can see that the if doesn't have/need { } because its 1 line, or if i see:

if (insert expression of choice here but it's long)
{
  some code
some code here tooo
}

i can see the second line is wrongly indented but its part of the if condition code, if the { bracket was on the if line i could easily miss that and think the code for the if condition being true is only one line

  • 0
First style for me. I find that the first style flows better to me when I read it, and that's how I've always been taught to indent, however I use style two for Javascript as it seems to be more the defacto style for indentation. The third style I've never used, and probably never will, it looks very strange.

On a slight tangent, I also find myself doing this more often in my code:

while (!foo and index < bar)
{ foo = MyFunc(index); index++;}

As I'm writing bigger projects as I get on in life, I'm finding increasingly annoying that 2 lines of code can occupy 4 lines, and this was my solution (Anything more get's indented properly). Would this annoy you if you were reading my work?

Well your example, for me, would be 5 lines of code.

while (!foo and index < bar)
{ 
	foo = MyFunc(index);
	index++;
}

I find example one to be far easy to read.

  • 0

I use the first style for functions (methods) and classes.

public int Foo()
{
	 int random_integer = 0;
	 Console.WriteLine(++random_integer++);
	 Console.WriteLine(random_integer--);
	 return random_integer;
}

Second style for loops, ifs, and cases.

for(int i=0;i<arr.length();i++){
  // Do something here.
}

  • 0
First style for me. I find that the first style flows better to me when I read it, and that's how I've always been taught to indent, however I use style two for Javascript as it seems to be more the defacto style for indentation. The third style I've never used, and probably never will, it looks very strange.

On a slight tangent, I also find myself doing this more often in my code:

while (!foo and index < bar)
{ foo = MyFunc(index); index++;}

As I'm writing bigger projects as I get on in life, I'm finding increasingly annoying that 2 lines of code can occupy 4 lines, and this was my solution (Anything more get's indented properly). Would this annoy you if you were reading my work?

Why would you even bother with the braces?

I prefer style 1, so much nicer to read and have braces that line up. I don't like why people use braces for one line if statements as well, looks horrendous.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.