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()
{
}
OR
Option B:
int main(){
}
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)
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.
My personal favorite:
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.
You need help
WTH?
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.
I use Option B but with a space before opening parenthesis and space between closing parenthesis & opening brace.
B option with space before the brace
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.
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.
A
function myFunc() {
}
I use Option A (company code formatting), but I am more comfortable with Option B.