What is a good practice? Opening brace in the same line or in the next line? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 41

What is a good practice? Opening brace in the same line or in the next line?

I have seen over the time programmers choosing one over the other methods of using braces. Give your feedback as to what do you prefer and why? Example: 1. if(a==b) { //code } OR 2. if(a==b) { //code }

26th Feb 2018, 6:11 PM
Abbas Ali
Abbas Ali - avatar
130 Answers
+ 15
There is a real 3rd option as well: On same line for choices, loops and such and on a new line for functions: void Foo(int number) { if (number...) { ... } } This style is used more often than you might think. An example is the Linux kernel (https://www.kernel.org/doc/html/v4.10/process/coding-style.html#placing-braces-and-spaces ). In my opinion it doesn't really matter what you choose, but please be consistent! And if you write code with someone else: make the choice together. Oh, and sometimes you don't have a choice at all, in script language TCL for instance you *must* write the opening bracket on the same line, else it will not compile.
4th Mar 2018, 10:08 AM
Freddy
+ 40
I prefer the same line, but in my opinion it doesn't really change the readability. I just feel like it looks better. In both cases it's clear that the block belongs to the statement. Either the brace which opens the block is alligned with the closing brace, or the statement itself is alligned with it. And in terms of good practice: I love it when I read a code which has the name of the class/function/etc commented right after the closed brace. This really adds readability a lot imo
26th Feb 2018, 6:28 PM
Alex
Alex - avatar
+ 28
There's no a good practice. Write as you want. if ( short_cond ) { ... } if ( very && long && cond ) { ... }
2nd Mar 2018, 7:31 AM
Vladislav Tikhiy (SILENT)
Vladislav Tikhiy (SILENT) - avatar
+ 20
I prefer first line😂
28th Feb 2018, 1:53 PM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 19
I prefer the second one, too. It gives the code more structure, it looks cleaner and it's easier for me to see how the braces line up. However, the advantage if the second option is probably that there are less braces in your code, which makes it more compact easier to find the closing braces.
26th Feb 2018, 6:26 PM
Chris
Chris - avatar
+ 15
For me, and before I hate this, it's on the same line.
1st Mar 2018, 9:22 PM
MecyDev
MecyDev - avatar
+ 14
Depending on personal preference as there is no fix standard. Also for better readability and presentation. So I prefer 2.
1st Mar 2018, 11:27 AM
Nilavarasan
Nilavarasan  - avatar
+ 12
I prefer same line. I understand why so many people like next line, but I got used to same line before I knew next line even worked.
26th Feb 2018, 6:24 PM
Jacob Pembleton
Jacob Pembleton - avatar
+ 12
In same line it's just logic. Are you write : And Sally Say : "hello" ??? It's exactly the same. We write : And Sally Say: ;-)
5th Mar 2018, 6:16 AM
MecyDev
MecyDev - avatar
+ 11
Personally I prefer 2, it’s neater and more readable because the braces line up.
26th Feb 2018, 6:21 PM
aklex
aklex - avatar
+ 10
i write like this..next line { { } }
2nd Mar 2018, 5:58 PM
Shruti Singh
Shruti Singh - avatar
+ 9
i prefer in the next line 😃
27th Feb 2018, 8:14 PM
Vukan
Vukan - avatar
+ 9
I prefer the next line , when I have lot of code ; even when I have single line statement;😂😂 it's make code more clear and readable ..
4th Mar 2018, 8:37 AM
ㅤ Tweetu 😆 ㅤㅤ
ㅤ  Tweetu 😆 ㅤㅤ - avatar
+ 9
I will prefer the new line because it makes our code easy to understand and traceback for bugs eg:- if(condition){ } Is a bit confusion and our braces are not in same place when compared to the following example if(condition) { }
5th Mar 2018, 4:09 AM
VamsiKrishna
VamsiKrishna - avatar
+ 8
Personally I prefer the next line, but I guess it depends on how much code is between the braces.
26th Feb 2018, 6:16 PM
Bagshot
Bagshot - avatar
+ 8
Depends on language standarts and developer's taste. But I've heard lot of opinions and people mostly say that keeping the brace on the same line with condition is really better. It helps you and your colleagues to sense your code while looking through it. I used to think differently, but time has passed and now I strongly agree with these people.
26th Feb 2018, 6:25 PM
Михаил «Sarf» Дружинин
Михаил «Sarf» Дружинин - avatar
+ 8
for small ones next line and for complicated ones same line .....
27th Feb 2018, 6:31 PM
STRIKER
+ 8
For JavaScript the airbnb style guide for JavaScript shows { on the same line. https://github.com/airbnb/javascript/blob/master/README.md#blocks Seems like C++ programmers like to use a new line for { but I am unfamiliar with their popular style guides.
1st Mar 2018, 10:19 AM
Lisa F
Lisa F - avatar
+ 8
I prefer the second style. First, It gives a more clear idea of the segment. Second, I find it subconciously awkward to use the first style xD
2nd Mar 2018, 9:36 AM
Infinity
Infinity - avatar
+ 7
Thanks for the tip, Alex!
26th Feb 2018, 6:41 PM
Chris
Chris - avatar