[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Brackets Placement

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 142
Thread images: 12

File: brackets.jpg (102KB, 474x500px) Image search: [Google]
brackets.jpg
102KB, 474x500px
Top or bottom, /g/? Which do you prefer?
>>
I'm relatively amateur and used to prefer the top one, but the longer my code gets the more I like the bottom one
>>
Bottom - unless you're retarded.
>>
[CODE]int
main(void)
{
if (1) {
...;
...;
}
}[/CODE]
>>
>>59818651
>functions
top one.

>code blocks inside functions
bottom one UNLESS the code is really long and nested multiple levels and the extra white space increases readability.
>>
Neither.
Top is C# only.
Bottom is Java only.

Everything should be
function()
{
if(1 == 1) {
for(;;) {

}
}
}

Functions get whole line brackets, everything inside them gets layered brackets.
>>
>>59818651
I use both.
>>
At least you can feel like you've written a third more code if you do the above.
>>
>>59818651
Top is unnecessary. Having the open bracket on its own line only wastes space. Having the closing bracket on its own line is good practise for readability.

Also both should be } else { for the ifs, faggot.
>>
None. Pythonistas don't need brackets.
>>
I feel like the top is more readable, because everything feels more grouped together. Especially with nested loops and if statements.
>>
>>59818704
patrician taste my friend
>>
>>59818651
(if condition
then-form
else-form)
>>
>>59818704
Or this:
int
main(void)
{
for (;;) {
no blank lines;
}

for (;;)
{
this needs some;

blank lines;
}
}
>>
>>59818736
>wasted space
it's not like they have to cut down trees for each empty line
>>
>>59818651
Bottom is just better, saves space and it's more readable.
>>
>>59818651
Depends on if I'm being paid by LoC
>>
I dont know why

But I do bottom for functions/methods
But top for the rest
>>
>>59818651
top is literally how to spot a shit programmer
>>
blah function()
{
for(iterations and longer ifs){
}
if(one or two statements) {...;...;}
}
>>
>>59818775
Not really. This would be the readable version:

public void RefreshCatalog()
{
if (IsCacheValid)
ResetFilterToDefaults();
else
RepopulateCatalogFromService();
}


However, those names are shit, so:

public void refresh_catalog()
{
if (valid_cache)
filter_defaults();
else
catalog_repopulate(FROM_SERVICE);
}


FROM_SERVICE would be part of an enum. valid_cache should be a function.
>>
>>59818651
definitely the bottom
>>
File: 1490303741417.png (6KB, 377x166px) Image search: [Google]
1490303741417.png
6KB, 377x166px
Best style.
>>
>>59818651
Bottom one is easier to diff.
>>
>>59818651
> Job interview
> Interviewer says salary is 500k
> Make your own hours
> Don't even have to code just sync repos all day
> BTW we make new lines for every fucking curly brace
> Me: FUCK OFF
>>
>>59818840
We are talking about brace placement, not naming convention.
Having said that refresh_catalog() is better.
>>
>>59818851
inconsistent
>>
>>59818851
based
>>
Top. Bottom is usually a sign of a trash programmer who thinks they're clever.

>>59818764
Also acceptable.
>>
>>59818851
I am disgusted with you
>>
>>59818651
I never understood why people would write the one on the bottom. Shit's completely unreadable
>>
>>59818651
top for C(++)
bottom for Java
>>
>>59818851
disgusting
>>
>>59819504
public static void main(String... args) throws Exception
{
System.err.println("no")
}
>>
File: 43666384.jpg (44KB, 440x745px) Image search: [Google]
43666384.jpg
44KB, 440x745px
>>59818651
Top
>>
>>59818651
I used to be
{
}
When I started VB
But then I found {
}
Is easier when I started Java
>>
>>59818651
#define RefreshCatalog() IsCacheValid ? ResetFilterToDefaults() : RepopulateCatalogFromService()
>>
>>59818651

FWIW both Dennis Ritchie (invented C) and Bjarne Stroustup (C++) both put brackets on the same line in their code examples.
>>
>>59818768
Runtime slows o2 for each whitespace
>>
>>59818651
gofmt
>>
File: indent.png (29KB, 511x219px) Image search: [Google]
indent.png
29KB, 511x219px
>>59818651
Like this desu fampai
>>
>>59818871
> Job interview
> Interviewer says salary is $1mil
> Make your own hours
> Don't even have to code just sync repos all day
> BTW no white space for code blocks
> Me: FUCK OFF
>>
>>59818851
void isEven (int i) {
if (i % 2) {
printf("That number is odd");
} else {
printf("That number is even");
}
} void isOdd (int i) {
isEven(i-1);
}
>>
Linux style guide.
For opening brackets:
Function brackets go on their own line.
Everything else on the same line.

For closing brackets:
else, else if, and the while in a do...while are on the same line.
Everything else on it's own line.
>>
>>59818651
top for c (if the block is big I can better spot where it begins), bottom in the rare case of js

But I also love the ternary operator so maybe I am retarded
>>
>>59818651
Depends on the language, I go top for C# and bottom for others
>>
Starting to learn C. Bottom ones.
>>
whatever the project specifications say
>>
>>59818651
No brackets at all. If your ifs and fors have more than one statement, it should be extracted.
Keep your functions clean.
>>
} else {

anyone who does this deserves the chair
>>
gofmt doesn't let me chose. It's better this way.
>>
>>59818766
this is exactly what I do
>>
>>59818851
Agreed.
>>
>>59818851
> Function is called isEven
> Returns void
What did he mean by that?
>>
>>59818726
Gross.
>>
>>59821851
By returning void it leads to this kind of answers >>59820326

> isOdd(3)
> calls isEven(2)
> prints "That number is even"
> 3 is even??
> Always print wrong answers
> WOW, seriously, wow.
>>
>>59818726
This is respectable, but I'd take a more detailed approach.

The more integral or individual the line, the more space it deserves, visually. A function name needs to be easily separable because that's the sort of thing you'd care about if you were quickly skimming the file. Interior if statements, ones that don't really have too involved of a purpose can be compressed. I'll often use one-liners or ternaries when it's something that doesn't deserve a lot of attention.
>>
The correct answer would be "it depends on the programming language conventions"

As for what I think, top is cancerous and bottom is the only way to go.
>>
>>59819605
Which language allows to invoke void functions on a ternary?
>>
Bottom because I do a lot of work on a shitty laptop with a teeny tiny screen, so space is precious.

I don't really like it, but the habit is so ingrained at this point that I'll probably never break it.
>>
>>59818651
Top - unless you're retarded.
>>
>>59818890
How so?
>>
brackets are for children and nu-males
chads use a language that has end's
>>
>>59818651
Whatever the IDE I'm using defaults to
>>
>TOP
Clean and readable. Casual friendly.

>Bottom
Not so clean, not so casual friendly.
>>
>brainlets using brackets

Lmao, pythonista here, come over and check out our subreddit /r/python.

No need for brackets.

If you do come, try to keep the swearing down. Racism will result in a ban.
>>
>>59818651

Those are braces
>>
bottom, had a code nazi as my intro c++ course a while back who insisted on top.

Fuckin forced me to use top unless i wanted to lose 40% per assignment.
>>
>>59822075
What? Unless you want to return the value and not just print, making it void doesn't do shit, retard.
>>
>>59822802
from __future__ import braces
>>
>>59822802
>>/Jewddit/
>>
>>59822802
go back to your trash plebbit and stay away from /g/ you fucking faggot.
>>
File: 590.png (81KB, 624x628px) Image search: [Google]
590.png
81KB, 624x628px
>>59822857
>>59822865
>>59823024
>>
>>59821781
Why? It shows clearly the extent of the block, right?

I'm not being a tit, it just seems that's the clearest way to write it without the clutter of additional lines for one brace.
>>
if you use anything else than OP's version 1 you're literally pajeet-tier
>>
File: whitesmiths.png (4KB, 170x130px) Image search: [Google]
whitesmiths.png
4KB, 170x130px
neither. I use the all superior whitesmith style. It effortlessly allows you to see each distinct block of code.
>>
Here's a quote from O'Reilly's "The C Programming Language":
[quote]
The position of braces is less
important, although people hold passionate beliefs. We have chosen one of several popular
styles. Pick a style that suits you, then use it consistently.
[/quote]
>>
>>59818851
this to be quite honest with you family
>>
if )1( }
{
else }
{
>>
File: 1478604784377.png (66KB, 597x255px) Image search: [Google]
1478604784377.png
66KB, 597x255px
picrelated is comfy when u get used to it
>>
>>59818768
kek'd

thats how I feel about it.
>>
File: 1464353323677.jpg (149KB, 531x471px) Image search: [Google]
1464353323677.jpg
149KB, 531x471px
>>59826184
kys
>>
>>59818704
this this this

torvalds suggested using the same for the kernel iirc
>>
Bottom. You can argue tabs vs spaces all you want, whatever I'll conform to your standards, but I'm not working on anything that uses the top format for braces.
>>
>>59820284
Get real the white board walked in and you couldn't sum primes to 2 million
>>
>>59822855
He implied, that isEven reads like a boolean function, but returs void. This will confuse the reader. (See isOdd impl.) It should be renamed to printParity or something like that.
We can argue about braces, but it won't fix the problem, that most of the programmers just suck at creating readable code.
>>
public class FizzBuzz                           {
public static void main(String[] args) {
for (int i = 1; i <= 50; i++) {
boolean fizzedOrBuzzed = false ;
if (i % 3 == 0) {
System.out.print("Fizz") ;
fizzedOrBuzzed = true ;}
if (i % 5 == 0) {
System.out.print("Buzz") ;
fizzedOrBuzzed = true ;}
if (!fizzedOrBuzzed) {
System.out.print(i) ;}
System.out.println() ;}}}
>>
>>59827523
>>59826184

my man.
only brainlets cant comprehend this kind of formatting.
>>
>>59818651
C#, C++, C - top

JS, TS, Go - bottom
>>
File: else.png (2KB, 290x246px) Image search: [Google]
else.png
2KB, 290x246px
>>
if
(
true
)
{
do_something
()
;
}
else
{
do_something_else
()
;
}
>>
>>59826184
>>59827523
Beautiful.
How do you configure it though?
>>
>>59818720
>and the extra white space increases readability.

M8 don't switch coding conventions for that. Just add a blank line.
>>
>>59827967
this
>>
Top because that's how I learned c++ and I'm used to it. It also makes more sense to me that the opening brace is visible and lines up with the closing brace.
>>59825744
This is the real correct answer though. Just fucking pick one and use it. Both are valid for different reasons.
>>
>>59818851
int isOdd(int i) {
return i%2;
}
>>
>>59818851
I used to think this was the best style until I started working on larger projects.
It quickly becomes hard to navigate and maintain with large files. Doesn't seem like it would, but I ended up switching to basically this: >>59818766
>>
File: 1463862191277.jpg (250KB, 1137x1012px) Image search: [Google]
1463862191277.jpg
250KB, 1137x1012px
>>59827967
>>
>>59828642
>has to write a function for every type
>>
>>59827967
Consider suicide.

>>59826184
wtf, lispfags should use a margin
this is great.
>>
bool theOnlyCorrectOne( int op )
{
if ( moreThanOneOption ) {
throw youAreRetarded( "you" );
} else {
std::cout << "op is a faggot" << std::endl;
}

return true;
}
>>
Top is more readable.
>>
File: 1472614053821.jpg (19KB, 250x312px) Image search: [Google]
1472614053821.jpg
19KB, 250x312px
>>59829179
>int op
>youAreRetarded( "you" )
>return true for no reason
Hello Pajeet
>>
File: rms-signs-a-macbook.jpg (77KB, 740x586px) Image search: [Google]
rms-signs-a-macbook.jpg
77KB, 740x586px
>>59818651
>>59818726
>>59818736
>>59819703
>>59820331
>>59821748
>>59822423
>>59822802

>brackets
they're called braces, you fucking autists
>>
>using languages that need brackets
>le current year
>>
>no space after function name
Disgusting.
How do you search for function declaration instead of usage?
>>
int
main(int argc, char *argv[])
{
if(expression s) {
// multi line
// block
}

return 0;
}


Also tabs for indenting and space for alignment
>>
>>59833176

Good stuff. Why's the return type on the above line, so you can easily find the function definition?
>>
>>59833221
yes, it's easier to
 grep ^func 

But it's for aesthetic too
>>
Top because that's the standard we use at work and I'm used to it at this point.
>>
>>59818651
int main()
{
if(stuff){
if(more stuff)
{
code
}
else
{
code
}
}
}
>>
>>59823575
2bh I just don't like it and find it ugly
I prefer every } to have its own line. { don't need own lines though
>>
>>59818651
bottom with no space after the parameters
>>
>>59818651
I've always used the top style.

I programmed in C++ MFC for many years, and that's the style they use in all of their library code, examples, etc. I got used to it, and I find the other style to be cramped and much harder to read.

In addition, the MFC style makes it a lot easier for me to instantly see when braces are properly matched. If the top style is used, my brain has the ability to instantly "sense" when braces are lined up geometrically in a vertical column (it's a talent that's kind of like how Rain Man can look at 100 toothpicks on the floor and count them instantly -- I have that kind of ability with telling if braces are matching, but only if the top style is used).
>>
>>59818726
>Functions get whole line brackets, everything inside them gets layered brackets.

I never saw the point of this deliberate inconsistency.
>>
>>59818651
Top for everything, except if branches where each result is only one line.

public bool IsFaggot(string name)
{
if (name == "OP")
return true;
else
return false;
}
>>
>>59832265
Your comment is rendered invalid, thanks to the power of Internet.
https://duckduckgo.com/?q=braces
>>
>>59832265
They're actually called squiggly diggly parentheses you flap jackin nigger cunt
>>
>>59836282
>{ } – braces are "two connecting marks used in printing"; and in music "to connect staves to be performed at the same time"[4] (UK and US), French brackets, curly brackets, definite brackets, swirly brackets, curly braces, birdie brackets, Scottish brackets, squirrelly brackets, gullwings, seagulls, squiggly brackets, twirly brackets, Tuborg brackets (DK), accolades (NL), pointy brackets, third brackets, or fancy brackets
>>
Top.
>>
>>59818651
well the language you posted that example in is c# which only uses the top, so top

if it was java, it'd be something like
public void refreshCatalogGetter() {
if (cacheValidiatorGetterRefresherPattern.getCacheGetter()) {
resetFiltertoDefaultsFactory.setToDefault(true);
} else {
repopulateCatalogFactoryBeanGenerator.selectCatalog().fromService(this.service.nameGetter().getName().toString().toString());
}
}
>>
Verbosity to develop large functions, so you can par it with documention and atomic functions when doing the aforementioned.
>>
>>59836441
You still use them as synonyms.
>>
if (isOdd(-5) == true)

hmmm
>>
int main()
{
for(whatever)
do thing,
do thing,
do thing;
}


The patrician way.
>>
>>59837254
Should be a reply to
>>59828642
>>
>>59832701
I like this convention too. Though this >>59833335 is another way to do it.

public void RefreshCatalog ()
{
if (IsCacheValid()) {
ResetFilterToDefaults();
SecondCall();
} else {
RepopulateCatalogFromService();
}
}

(If both the if and else branches only have one statement they don't need braces at all.)
>>
>>59818651
i like the top. was actually taught to do the bottom way tho. weird innit?
>>
>>59818704

int main (void) {
printf ("hello world");
return (0);}


Lisp style best style
>>
>>59818651
neither you cancer
>>
>>59835243

Why not
public bool IsFaggot(string name)
{
return(name == "OP");
}
>>
>>59818851
Only acceptable answer.
Damn hipsters.
>>
After using Python, I really miss brackets.

This indenting shit is painful
>>
>>59839280

I like it.
>>
>>59839270
public bool isFaggot(string name){
return name == "OP";
}
>>
>>59830868
it's an example of syntax you dingus
>>
Unlike most people here I don't like solving puzzles with brackets all over the place in my code so >>59824770 is the only answer
>>
>>59818651
        public void RefreshCatalog() {
if (isCacheValid) {
ResetFilterToDefaults()
return;
}
RepopulateCatalogFromService();
}
>>
>>59818651

Top and then a blank line to create white space for readability. It is the superior arsthetic way.

function master_race() {

echo "Beautiful code";
return(1);

}
>>
>>59839270
True, I was just making a quick example of the structure.
>>
>>59839565
this
>>
Top, if you use bottom your mom never loved you.
Thread posts: 142
Thread images: 12


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.