+ 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 }
131 Antworten
+ 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.
+ 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
+ 28
There's no a good practice. Write as you want.
if ( short_cond ) {
...
}
if ( very &&
long &&
cond )
{
...
}
+ 20
I prefer first line😂
+ 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.
+ 15
For me, and before I hate this, it's on the same line.
+ 14
Depending on personal preference as there is no fix standard. Also for better readability and presentation. So I prefer 2.
+ 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.
+ 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:
;-)
+ 11
Personally I prefer 2, it’s neater and more readable because the braces line up.
+ 10
i write like this..next line
{
{
}
}
+ 9
i prefer in the next line 😃
+ 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 ..
+ 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)
{
}
+ 8
Personally I prefer the next line, but I guess it depends on how much code is between the braces.
+ 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.
+ 8
for small ones next line and for complicated ones same line .....
+ 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.
+ 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
+ 7
Thanks for the tip, Alex!