• 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 second style for everything. Using the first style does not improve readability, it just adds an extra line. The second style is recommended by Sun for Java coding by the way. I don't think there are official recommendations from ANSI/ISO for C/C++ so I use the second style for that.

  • 0
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.

Because there's two statements in that while loop :)

  • 0
...

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

That's why I use a text editor that does bracket highlighting, I can see quite quickly if I've got an extra, or a missing bracket.

  • 0
I use the first for everything bar CSS where I use the 2nd. If I am doing an if with one response:

if ($x == $y)

{echo "hello";}

I'll do it on one line.

Why not just do it all on one line?

if ($x == $y) echo "hello";

It's bad practice anyway, though, because it means more work for any future coders, you should always brace and indent properly even for single lines.

  • 0

I use and prefer the second style. If I'm given code in the first I change it, that's how much it annoys me. It's not rational, just how I learnt :p

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.

If you are reading someone else's code the brackets allow you to very easily what goes where in terms of the if statement and others that come after it. It just makes it easier to read, and code is never going to win a beauty competition.

  • 0

I use the second style, but I have no issues using the first, and have done so for a few of my projects.

... It just makes it easier to read, and code is never going to win a beauty competition.

Unless of course, there was a beauty contest for code :p

  • 0
I use and prefer the second style. If I'm given code in the first I change it, that's how much it annoys me. It's not rational, just how I learnt :p

If you are reading someone else's code the brackets allow you to very easily what goes where in terms of the if statement and others that come after it. It just makes it easier to read, and code is never going to win a beauty competition.

If the statement is one line then that wouldn't pose a problem.

Rahhhh;

if (blah blah)
   doSomething()
else
   doOtherThing

Yeahhhh;

With well used line spacing there is no need to use the braces here unless you plan on adding more code. It adds unwanted mess. Generally, I can make code that is very easy to follow, read and even looks good, depending on the language. DirectX is a pain to get looking remotely nice.

  • 0

First style is called BSD style (has other name, if I remember it's one hackers name) and second style is called K&R (people who contributed/created C) and the last one looks like GNU style.

For personal usage I use first and something the second. But it depends on what work you are doing and the coding standard you must use then coding.

  • 0

The last form is indeed GNU style. I recently had to edit some gcc source code to get it to compile the way I wanted it to compile (following LinuxFromScratch). I looked into the source files and found that style.

I use the first form for C/C++/C# code, and I use the second form for PHP, Java, JS and CSS.

In addition, I've adopted my own form for one-liners:

if (condition)
	{ result; }

except with CSS, where I use:

.rule_target { property: propertyValue; }

For some reason, people find my personal conventions really unusual... :p

Edit:

In addition, I was looking through some source code about a month ago, and I found this crazy form:

if (condition)
{   code1;
	code2;
	}

Also, there are different variations when it comes to case statements:

switch (item)
{
	case 0:
		code0;
	break;

	case 1:
		code1;
		break;
}

Notice how the break statements are in different places? Also, when you create variables in case statements, braces are required (at least in C/C++). In that case, does the break statement go inside or outside of the braces? *sigh* Too many conventions! :D

Edit2:

The first form is indeed BSD style, also known as Allman style. Wikipedia has a great article on indent style.

Edited by rpgfan
  • 0

Having programmed in many languages.. readibility, maintainability, and screen compactness are my key considerations.

Style 1 simply wastes too much vertical space, expecially it Get/Set section.

Style 2 is not symetrical, and looks awkward (IMHO)

I use two basic rules.

1) For normal code blocks, I use a modified Style 1, but a little tighter.

if (condition)
{   code1;
    code2;
} //if

Note: the "//if" is automatically added with a VS add-in

This gives the std left aligned {} that most developers are use to.

Displays the actual code in an easily read block.

Yes, it only saves 1 line per block, but it does add up.

2) One liners go on one line

But I add the {} in case someone else needs to add something later.

Remember: We write code once, others maintain forever..

if ($x == $y)
{    echo "hello";}

//easily becomes...
if ($x == $y)
{    echo "hello";
     echo "hello again";
} //if


Note: the "// if" is automatically added with a VS add-in

This works well for me. :)

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

    • No registered users viewing this page.
  • Posts

    • Microsoft's fast coding model MAI-Code-1-Flash comes to Copilot Business and Enterprise by Karthik Mudaliar Microsoft’s recently announced MAI-Code-1-Flash model is now generally available to GitHub Copilot Business and Copilot Enterprise customers. With this support, organizations can have more centralized policy controls and billing while finally being able to use Microsoft’s lightweight, first-party coding model. According to GitHub’s announcement, Business and Enterprise plan administrators must enable the MAI-Code-1-Flash policy in Copilot settings before developers can access the model. Microsoft says that MAI-Code-1-Flash is for fast, iterative coding work rather than the most demanding architectural or debugging tasks. GitHub’s official model comparison page says that the model is great for "general-purpose coding and writing," while it excels at fast, accurate code completions and explanations Microsoft introduced MAI-Code-1-Flash on June 2 as part of a broader collection of internally developed MAI models. GitHub subsequently expanded support to Copilot CLI, the Copilot cloud agent, GitHub.com chat, GitHub Mobile, Visual Studio, JetBrains IDEs, Eclipse, and Xcode, but said support for managed Business and Enterprise customers was still on the way. In Microsoft’s own benchmark testing, MAI-Code-1-Flash scored 51.2% on SWE-Bench Pro, compared with 35.2% for Anthropic’s Claude Haiku 4.5. Microsoft also claimed that the model used up to 60% fewer tokens on SWE-Bench Verified. Do note that these are vendor-run results rather than independent measurements. The model is billed at provider list pricing under GitHub’s usage-based system. GitHub currently lists MAI-Code-1-Flash at $0.75 per million input tokens, $0.075 per million cached input tokens, and $4.50 per million output tokens. For organizations, the main incentive to use MAI-Code-1-Flash is likely to be efficiency rather than maximum capability. A smaller model that responds quickly and limits unnecessary output is quite useful for repetitive agent tasks at scale, especially after GitHub Copilot’s move toward usage-based billing. The "Flash" model is recommended for fast work and not necessarily for huge repositories with loads of context. It's better if teams compare their output with other larger models, especially if they're working on security-sensitive changes and complex, multi-file work.
    • yes AND no the "original" or plain/normal Optiplex 7010 won't be getting any more new firmware updates BUT the Optiplex SFF/SFF Plus {small form factor}, Micro/Micro Plus & Tower/Tower Plus 7010 editions DO get new updates such as this new one   and here are similar guides from the Dell web site for Dell systems: https://www.dell.com/support/kbdoc/en-us/000390990/secure-boot-transition-faq https://www.dell.com/support/kbdoc/en-us/000347876/microsoft-2011-secure-boot-certificate-expiration
    • AT&T has been spying on US citizens with the NSA for decades.. they just know how to keep it more under wraps.. the evil level is still there.
  • Recent Achievements

    • One Year In
      bernmeister earned a badge
      One Year In
    • Week One Done
      Scoobystu earned a badge
      Week One Done
    • Week One Done
      tuben earned a badge
      Week One Done
    • First Post
      OffsetAbs earned a badge
      First Post
    • Reacting Well
      OffsetAbs earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      462
    2. 2
      +Edouard
      213
    3. 3
      PsYcHoKiLLa
      158
    4. 4
      Steven P.
      72
    5. 5
      FloatingFatMan
      71
  • Tell a friend

    Love Neowin? Tell a friend!