• 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

    • It sounds like you’re trying to rewrite a narrative to align this layoff with your political beliefs. Games can be horrible, whatever backwards notions you have don’t change that bungie has problems, mostly with just bad games, and arrogance. When they pushed Microsoft to let them be independent they lost their way. They hired on a bunch of people and they couldn’t justify the employee count consistent with their revenue.
    • Trying out Noctalia v5 on CachyOS  
    • Calibre 9.10 by Razvan Serea  Calibre is an open source e-book library management application that enables you to manage your e-book collection, convert e-books between different formats, synchronize with popular e-book reader devices, and read your e-books with the included viewer. It acts as an e-library and also allows for format conversion, news feeds to e-book conversion, as well as e-book reader sync features and an integrated e-book viewer. Calibre's features include: library management; format conversion (all major ebook formats); syncing to e-book reader devices; fetching news from the Web and converting it into ebook form; viewing many different e-book formats, giving you access to your book collection over the internet using just a browser. Calibre 9.10 changelog: New features Content server: A new "modern" interface with a sidebar to ease navigation Content server: When used with HTTPS allow installation as a PWA (Progressive Web App) Edit book: Saved searches: When filtering the list of saved searches match by keywords CSS parsing: Add support for CSS Level 4 selectors Cover grid: When using an image larger than the viewport as a texture scale it to fit the viewport Annotations browser: Allow restricting displayed annotations by custom annotation styles as well Edit book: Compress images: Add option to convert PNG images to JPEG or WEBP Bug fixes E-book viewer: Fix IME on Windows not working when typing in notes for highlights Conversion: Heuristics: Improve performance in some pathological cases SNB Input: Fix error on some input files Windows: fix rare crash when too many notifications are displayed at once Fix duplicating of books not duplicating value from enumerated columns when the column has a default value defined Fix a regression in 9.8 that caused errors from AI plugin providers to be silently swallowed and not displayed to user Fix CSV export invalid when exporting comments field Disallow Python templates when reading book metadata (CVE-2026-53511) Improved news sources The Week Economist Espresso Horizons Download: Calibre 9.10 | Portable | ~200.0 MB (Open Source) Download: Calibre for MacOS | 327.0 MB Download: Calibre for Linux View: Calibre Home Page | Calibre Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Malwarebytes Anti-Malware 5.6.1.257 by Razvan Serea Malwarebytes is a high performance anti-malware application that thoroughly removes even the most advanced malware and spyware. Malwarebytes version 5.**** brings comprehensive protection against today’s threat landscape so that you can finally replace your traditional antivirus. You can finally replace your traditional antivirus, thanks to a innovative and layered approach to prevent malware infections using a healthy combination of proactive and signature-less technologies. While signatures are still effective against threats like potentially unwanted programs, the majority of malware detection events already come from signature-less technologies like Malwarebytes Anti-Exploit and Malwarebytes Anti-Ransomware; that trend will only continue to grow. For many of you, this is something you already know, since over 50% of the users already run Malwarebytes as their sole security software, without any third-party antivirus. What's new in Malwarebytes 5.****: Unified user experience - For the first time, Malwarebytes now provides a consistent experience across all of our desktop and mobile products courtesy of an all new and reimagined user experience powered by a faster and more responsive UI all managed through an intuitive dashboard. Modern security and privacy integrations - Antivirus and ultra-fast VPN come together seamlessly in one easy-to-use solution. Whether you’re looking for a next-gen VPN to secure your online activity, or harnessing the power of Browser Guard to block ad trackers and scam sites, taking charge of your privacy is simple. Trusted Advisor - Empowers you with real-time insights, easy-to-read protection score and expert guidance that puts you in control over your security and privacy. Malwarebytes 5.6.1.257 changelog: Features and improvements Updated the sign-in section of the My Subscription page to clarify that users can activate their subscription by signing in with their Malwarebytes account. Updated the uninstall flow to collect more meaningful insights and address customer concerns. Refreshed the app's tutorial layout for a better look and feel. Issues fixed Fixed an outdated link when clicking Take action after running a Digital Footprint Scan. Miscellaneous bug fixes. Download: Malwarebytes 5.6.1.257 | 472.0 MB (Free, paid upgrade available) Links: Malwarebytes Website | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      xvvxcvv earned a badge
      Week One Done
    • One Month Later
      xvvxcvv earned a badge
      One Month Later
    • Enthusiast
      Xonos went up a rank
      Enthusiast
    • Conversation Starter
      Admir earned a badge
      Conversation Starter
    • First Post
      The_Focal_Point earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      405
    2. 2
      +Edouard
      168
    3. 3
      PsYcHoKiLLa
      129
    4. 4
      neufuse
      69
    5. 5
      Xenon
      68
  • Tell a friend

    Love Neowin? Tell a friend!