• 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

    • So for the last few days, and excuse me if I'm not posting in the correct place... anyway, for the last few days I have been getting constant login approval requests from Microsoft Authenticator. I thought maybe someone was trying to get in so I changed my password and backup info... it did stop for a bit after, but now, it's back. I've had maybe 10 today alone. I check the history and there is nothing there.... under my recent activity it just lists my login to view the page, and my last login attempt which I signed into Outlook on my phone on June 18. What is going on? It's asking me to 'type in the code' that's displayed on my screen. I'm not doing it! Anyone else? Anything I should dig deeper into?
    • Hello police? I'd like to report a murder.
    • Ventoy 1.1.15 is out.
    • Win11Debloat 06.24.2026 by Razvan Serea Win11Debloat is a lightweight, easy to use PowerShell script that allows you to quickly declutter and customize your Windows experience. It can remove pre-installed bloatware apps, disable telemetry, remove intrusive interface elements and much more. The script also includes many features that system administrators and power users will enjoy. Such as a powerful command-line interface, support for Windows Audit mode and the option to make changes to other Windows users. All changes made by Win11Debloat can be easily reversed, and most removed apps can be restored via the Microsoft Store. A full guide on how to undo the changes is available here. Win11Debloat features: Below is an overview of the key features and functionality offered by Win11Debloat. Please refer to the wiki for more information about the default settings preset. Remove a wide variety of preinstalled apps. Click here for more info. Disable telemetry, diagnostic data, activity history, app-launch tracking & targeted ads. Disable tips, tricks, suggestions & ads across Windows. Disable Windows location services & app location access. Disable Find My Device location tracking. Disable 'Windows Spotlight' and tips & tricks on the lock screen. Disable 'Windows Spotlight' desktop background option. Disable ads, suggestions and the MSN news feed in Microsoft Edge. Hide Microsoft 365 ads on the Settings 'Home' page, or hide the 'Home' page entirely. Disable & remove Microsoft Copilot. Disable Windows Recall. Disable Click to Do, AI text & image analysis tool. Prevent AI service (WSAIFabricSvc) from starting automatically. Disable AI Features in Edge. Disable AI Features in Paint. Disable AI Features in Notepad. Disable the Drag Tray for sharing & moving files. Restore the old Windows 10 style context menu. Turn off Enhance Pointer Precision, also known as mouse acceleration. Disable the Sticky Keys keyboard shortcut. Disable Storage Sense automatic disk cleanup. Disable fast start-up to ensure a full shutdown. ...and more. Once you’ve downloaded the Win11Debloat file (Get.ps1), just follow these quick steps: Locate the Get.ps1 script file. Right-click the file and select Run with PowerShell from the context menu. If prompted by User Account Control (UAC), select Yes to grant the script the necessary administrative permissions. Win11Debloat 06.24.2026 changelog: With this release, the legacy app list generator and CustomAppsList file support is removed. This only affects users using legacy command-line methods. More info here: #664 Features & Improvements Clean up styling to better match Windows fluent design guidelines by @Raphire in #638 Add GPO override warning alert and WhatIf dry-run previews by @HetCreep in #611 Disable telemetry-related scheduled tasks under Microsoft\Windows by @HetCreep in #615 Guard against loading, saving & executing undefined features by @Raphire in #665 Simplify Window management & update minimum window sizes by @Raphire in #671 Update start menu backup/restore with timestamped filenames by @Raphire in #672 Clean up feature execution logic for readability by @Raphire in #641 Clean up & improve app removal methods, remove legacy CLI app removal methods by @Raphire in #662 Remove support for CustomAppsList file format, including -RemoveAppsCustom and -RunAppsListGenerator parameters Update CLI app removal to use newer app removal methods Drop support for sunset apps (Fitbit, Shazam, Twitter, Viber, Wunderlist, XING) and Plex Fix removal of Microsoft Copilot Fixes Fix: Replace P/Invoke dynamic DLL imports with WPF native methods to fix temp DLL access-denied errors by @Raphire in #661 Fix Start Menu apps not being set correctly for all users when running script for other user by @Raphire in #637 Fix store suggestions not getting disabled correctly for all users when running as other user by @Raphire in #642 Fix: Respect Feature min/max version for comboboxes by @Raphire in #639 Fix: Don't treat AllUsers/CurrentUser as a username at startup by @HetCreep in #647 Fix: treat dismissed unsafe-removal confirmation as decline by @HetCreep in #651 Fix: Surface runspace errors instead of swallowing them in GUI mode by @HetCreep in #655 Fix: Correct sub-key path matching in backup allow-list validation by @HetCreep in #645* Fix: Detect WinGet uninstall failures by exit code, not English text by @HetCreep in #658 Fix: Detect installed OneDrive in the "Only show installed" filter by @HetCreep in #656 Download: Win11Debloat 06.24.2026 | Open Source View: Win11Debloat Home Page | Screenshots 1| 2 Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Popular Contributors

    1. 1
      +primortal
      416
    2. 2
      +Edouard
      173
    3. 3
      PsYcHoKiLLa
      125
    4. 4
      Michael Scrip
      77
    5. 5
      Xenon
      76
  • Tell a friend

    Love Neowin? Tell a friend!