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

    • Qualcomm takes on NVIDIA with new Dragonfly CPU and AI chips by Pradeep Viswanathan Microsoft, Google, Amazon, AMD, Meta, Apple, OpenAI, and several others have been developing their own chips for AI infrastructure. However, NVIDIA still remains the dominant player in the market. Today, Qualcomm announced a major expansion of its data center infrastructure portfolio to better compete with NVIDIA. The new lineup includes the Qualcomm Dragonfly C1000 CPU, Qualcomm High Bandwidth Compute technology, the Dragonfly AI300 inference accelerator, new connectivity products, and custom silicon solutions. Qualcomm claims that this new lineup improves performance per watt, token throughput, and total cost of ownership for AI data centers. The Dragonfly C1000 is a new data center CPU built with Qualcomm’s custom Oryon cores. This chip will feature more than 250 cores, frequencies above 5GHz, and a chiplet-based design. Qualcomm claims that this new C1000 can deliver more than 2x better performance per watt compared to existing server CPU offerings based on specifications. The Dragonfly C1000 will support PCIe Gen 7 with more than 2TB/s of connectivity, along with CXL, advanced RAS features, and both air and liquid cooling. Qualcomm expects the Dragonfly C1000 to be commercially available in 2028. Additionally, Qualcomm and Meta announced a multi-year, multi-generation agreement under which Qualcomm will supply Dragonfly C1000 data center CPUs for Meta’s next-generation server fleet. Qualcomm also announced High Bandwidth Compute, a new near-memory computing architecture designed to address AI’s memory bandwidth bottleneck. HBC Gen 1 will debut with the Dragonfly AI250, which is expected to sample in mid-2027. The AI250 will deliver 133TB/s per card, an 18x increase in effective memory bandwidth compared to the AI200 with LPDDR5X. The new Dragonfly AI300 with HBC Gen 2 is a rack-level AI inference platform from Qualcomm. Qualcomm claims that the AI300 can deliver 4x to 8x better performance per watt compared to existing GPU-based architectures based on memory bandwidth per watt per card. The Dragonfly AI300 is expected to be available in 2028.
    • IBM reveals sub-1nm chip technology, production expected in another 5 years by Pradeep Viswanathan TSMC is now leading the chip manufacturing industry with its 2nm-class process node called N2. Samsung Foundry also has a 2nm-class process node called SF2. TSMC says N2 entered volume production in Q4 2025. Samsung says SF2 started mass production in 2025. Today, IBM announced the world’s first sub-1-nanometer chip technology, marking another major semiconductor research milestone. The new technology is based on a 0.7nm, or 7-angstrom, node and uses a new transistor architecture called “nanostack.” The new design vertically stacks and staggers nanosheet-based transistors so that more components can fit into the same chip area while also improving performance and power efficiency. IBM claims that this new sub-1nm chip can pack nearly 100 billion transistors onto a chip the size of a fingernail. This offers almost twice the density, up to 50 percent higher performance, or 70 percent better energy efficiency when compared to IBM's 2nm node design announced back in 2021. Also, IBM mentioned that this new architecture can deliver 40 percent SRAM scaling. It is important to consider that this announcement from IBM is a research milestone rather than a near-term process node launch. Back in 2021, IBM unveiled the world’s first 2nm chip design, claiming 50 billion transistors on a fingernail-sized chip and major performance and efficiency gains. Five years later, IBM’s 2nm technology has still not entered mainstream commercial production. That is because IBM is no longer a major commercial chip manufacturer. It sold its chip manufacturing business to GlobalFoundries years ago and has since then focused only on semiconductor research, IP development, and partnerships. To productize its 2-nm chip technology, IBM partnered with Japan’s Rapidus, but it has not resulted in anything shipping at scale. IBM says that its new sub-1nm technology can reach production as early as within the next five years. If that happens, it will likely depend on manufacturing partners, advanced EUV tooling, and years of yield improvements.
  • Recent Achievements

    • Week One Done
      Meta Plast earned a badge
      Week One Done
    • First Post
      kinowa earned a badge
      First Post
    • Rookie
      krychek57 went up a rank
      Rookie
    • Grand Master
      Jaybonaut went up a rank
      Grand Master
    • One Year In
      Philsl earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      461
    2. 2
      +Edouard
      171
    3. 3
      PsYcHoKiLLa
      136
    4. 4
      Michael Scrip
      78
    5. 5
      Xenon
      77
  • Tell a friend

    Love Neowin? Tell a friend!