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:

int main()
{

}
Enter fullscreen mode Exit fullscreen mode

OR

Option B:

int main(){

}
Enter fullscreen mode Exit 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 (13)

Collapse
 
tbroyer profile image
Thomas Broyer

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.

Collapse
 
yechielk profile image
Yechiel Kalmenson • Edited

My personal favorite:


func myFunction()                                               {

    if(something)                                               {
        doSomething()                                           }
    else                                                        {
        doSomethigElse()                                       }}
Enter fullscreen mode Exit 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.

Collapse
 
dynamicsquid profile image
DynamicSquid

You need help

Collapse
 
sprabhakaran profile image
Prabhakaran S

WTH?

Collapse
 
sfiquet profile image
Sylvie Fiquet

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.

Collapse
 
ctrleffive profile image
Chandu J S

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

function main () {
// goto mars
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
wseai profile image
jagarkin

B option with space before the brace

int main() {
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
szam profile image
Samuel Grasse-Haroldsen

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.

Collapse
 
mitchpommers profile image
Mitch Pomery (he/him)

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.

Collapse
 
dynamicsquid profile image
DynamicSquid

A

Collapse
 
aryamaan08 profile image
Aryamaan08

function myFunc() {

}

Collapse
 
inegm profile image
Islam NEGM

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

int main (void) { 
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
juwdohr profile image
Juwdohr

I personally like option 'A' as this seems to me to be more clean looking.