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(){}
OR
Option B:
intmain(){}
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)

- LocationDijon, 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.

- Email
- LocationNYC
- EducationFlatiron School
- WorkSoftware Engineer at VMware
- Joined
My personal favorite:
funcmyFunction(){if(something){doSomething()}else{doSomethigElse()}}
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.

- Joined
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.
functionmain(){// goto mars}

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++; } } }}
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++; } } }}

- 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.

- LocationSydney, Australia
- WorkChief 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.

- LocationCairo, Egypt
- WorkSoftware Engineer
- Joined
I use Option A (company code formatting), but I am more comfortable with Option B.
intmain(void){}
For further actions, you may consider blocking this person and/orreporting abuse