Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Fum
Fum

Posted on

     

How do you format curly braces? {}

A discussion just for fun!

When coding in languages that use the curly braces{} do you place the curly brace on a new line or in line?

Like so:

Option A:

intmain(){}
Enter fullscreen modeExit fullscreen mode

OR

Option B:

intmain(){}
Enter fullscreen modeExit fullscreen mode

Just curious what others use. I always use option A and I'm always glad to see a codebase using the same because it's so much easier to read for me 😅

So what about you?

Top comments(15)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
tbroyer profile image
Thomas Broyer
Developer and architect mainly interested in web development (frontend, Web APIs), web app security, build tools, Java, Kotlin, Gradle, etc.
  • Location
    Dijon, France
  • Joined

Option B, except with a space before the brace.
But most importantly, I don't really care as I delegate this to tools like Prettier, Google Java Format, or Ktlint.
I don't do C or C++, but there's clang-format there afaict.

CollapseExpand
 
yechielk profile image
Yechiel Kalmenson
He/Him/HisI'm a Software Engineer and a teacher.There's no feeling quite like the one you get when you watch someone's eyes light up learning something they didn't know.
  • Email
  • Location
    NYC
  • Education
    Flatiron School
  • Work
    Software Engineer at VMware
  • Joined
• Edited on• Edited

My personal favorite:

funcmyFunction(){if(something){doSomething()}else{doSomethigElse()}}
Enter fullscreen modeExit fullscreen mode

Though in a more serious note, there's no "right" way. Different languages have different conventions and following the convention is more important than any perceived benefit of doing it one way or the other.

CollapseExpand
 
dynamicsquid profile image
DynamicSquid
Interested in low level development 🔸 Currently focused on language design 🔸 C++ is good
  • Joined

You need help

CollapseExpand
 
sprabhakaran profile image
Prabhakaran S
Software developer
  • Location
    India
  • Work
    Software Engineer at Zoho
  • Joined

WTH?

CollapseExpand
 
sfiquet profile image
Sylvie Fiquet
  • Location
    United Kingdom
  • Joined

Option A comes from the C tradition, that's what you find in the K&R.

I think option B came about with JavaScript. JS interpreters tend to automatically add a semi-colon at the end of each line unless there is a clear sign that it shouldn't (like an open brace signalling the start of a code block). Hence the new format. I'm not sure if that reason is still relevant today but by now it's standard in the JS world.

As for me I started with C/C++ and option A. After a long career break, I took up JavaScript and Node.js and switched to option B. It was weird at first but now it's second nature.

At the end of the day, what matters is that the team agrees on a common format. Or uses tools that automate formatting.

CollapseExpand
 
ctrleffive profile image
Chandu J S
Problem Solver
  • Location
    Trivandrum, India
  • Work
    Full Stack Developer at HootBoard
  • Joined

I use Option B but with a space before opening parenthesis and space between closing parenthesis & opening brace.

functionmain(){// goto mars}
Enter fullscreen modeExit fullscreen mode
CollapseExpand
 
umi_84 profile image
Aloof
  • Joined
• Edited on• Edited

Let's try to insert behind the inner for loop the next condition:if( count === 0 ) { break; }

Where would be easier to spot the correct place?

A:

function bubbleSort(array){    let len = array.length    for(let i = 0; i < len - 2; i++)    {        let count = 0;        for(let j = 0; j < len - 2; j++)        {            if(array[j] > array[j+1])            {                swap(array, j, j+1);                count++;            }        }    }}
Enter fullscreen modeExit fullscreen mode

B:

function bubbleSort(array) {    let len = array.length    for(let i = 0; i < len - 2; i++) {        let count = 0;        for(let j = 0; j < len - 2; j++) {            if(array[j] > array[j+1]) {                swap(array, j, j+1);                count++;            }        }    }}
Enter fullscreen modeExit fullscreen mode
CollapseExpand
 
wseai profile image
jagarkin
I tend to over React
  • Location
    Tunis, Tunisia
  • Joined

B option with space before the brace

intmain(){}
Enter fullscreen modeExit fullscreen mode
CollapseExpand
 
szam profile image
Samuel Grasse-Haroldsen
Full-stack developer fluent in Hungarian looking for exciting challenges in the international industry!
  • Joined

I started coding in C++ and option A was what I saw in books and other code online. Now that I'm using JS I see a lot more of option B so I guess I would say it depends on the language you're using.

CollapseExpand
 
mitchpommers profile image
Mitch Pomery (he/him)
I break things then fix them so they are less broken than when I started.(He/Him/They)
  • Location
    Sydney, Australia
  • Work
    Chief Problem Solver
  • Joined

I follow whatever the codebase I'm working in uses most of the time. Though if I am ever setting up the codebase (or enforcing linting in a codebase that uses both) I'll generally use Option A.

CollapseExpand
 
aryamaan08 profile image
Aryamaan08
  • Joined

function myFunc() {

}

CollapseExpand
 
inegm profile image
Islam NEGM
C Embedded, Linux, Git, Integrity Os, Information Security
  • Location
    Cairo, Egypt
  • Work
    Software Engineer
  • Joined

I use Option A (company code formatting), but I am more comfortable with Option B.

intmain(void){}
Enter fullscreen modeExit fullscreen mode

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I work as an Embedded C Engineer. Interested in expanding my knowledge in other areas of software development. Building a collaboration community for devs at https://inspirezone.tech
  • Work
    Embedded C Engineer
  • Joined

More fromFum

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp