About curly braces

16 posts

Flag Post

I thought it was more popular to write them on their own lines:

if(variable == true)
{
    //code here
}

But auto format in Flash puts the curly braces right after the parenthesis:

if(variable == true){
    //code here
}

Out of curiosity, I was wondering which style is more popular/widely used.

I used to use the second style, but I finally got into the habit of doing it the other way.

 
Flag Post
Originally posted by GameBuilder15:

I thought it was more popular to write them on their own lines:

if(variable == true)
{
    //code here
}

But auto format in Flash puts the curly braces right after the parenthesis:

if(variable == true){
    //code here
}

Out of curiosity, I was wondering which style is more popular/widely used.

I used to use the second style, but I finally got into the habit of doing it the other way.

It’s entirely preference, I don’t know if there is a graph on who uses what but I’m partial to this:

if (foobar == true)
{
    //derp
}
 
Flag Post

Funnily enough, I use the first when programming in AS3, and the second when programming in Java, just to illustrate how insignificant the difference is

I prefer the first aesthetically, but many IDEs, e.g. Eclipse, are based around the second format, so sometimes it’s just best to stick with how the IDE wants to play ball to prevent the need to alter settings and/or constantly edit pre-formatted code

 
Flag Post

I like the second personally
Edit>Preferences>Autofomat
check “Inset { on line after if, for, switch, while, etc.”

 
Flag Post

I used to use the first style, but switched to the second because it takes up one less line for the same logic. This increases the amount of logic that I am able to view on my screen assuming that the code’s length is beyond a screen. It’s much easier to troubleshoot a problem when you view as much of the relevant code to the problem as possible, as opposed to having to scroll down or up slightly.

 
Flag Post

Yeh, I use the first on everything except getters and setters, which I use the second. Kinda odd, but who is going to stop me? :D

 
Flag Post

I put the braces on the same line (i.e. the second way) because of the same thing, I hate wasted space because it hides logic. If it’s a one line condition I won’t have separate lines at all, just do

if(whatever) { do_stuff; }

(or sometimes no braces, depending on how lazy I’m feeling).

 
Flag Post
Originally posted by evan999333:

I like the second personally
Edit>Preferences>Autofomat
check “Inset { on line after if, for, switch, while, etc.”

Oh, cool. I didn’t know you could do that.

 
Flag Post

Yeah, entirely preference. Personally, I like:

public function CreateCupcake():void
{
    trace("Yaay!  I now have a cupcake!");
}

My reason being is that it’s faster (for me) to, at a glance, mentally identify and isolate which logic is part of which block, as well as ensure that there aren’t any brace/bracket failures going on.

This for single-liners, because (unless I’m missing something) the braces aren’t necessary:

if (cupcake.flavor == Cupcake.CHOCOLATE) trace("*Squeals in delight*");
 
Flag Post

I prefer the second style. It just so happens to be the One True Brace Style.

 
Flag Post

Thanks for the link, I didn’t realize how many indent styles there where. I put open braces on their own line, I like to see them align with their corresponding closing brace. And yes, curly braces are usually superfluous when they only embrace a single instruction, such as

for (var i:int = tray.length - 1; i >= 0; --i) tray[i] = new Cupcake();

instead of

for (var i:int = tray.length - 1; i >= 0; --i)
{
    tray[i] = new Cupcake();
}

which does wonders for compacting code. So does replacing the contents of the curly braces with a single function call, and displacing the ‘guts’ to their separate function. It can reduce nesting as well.

 
Flag Post

I do:
thing
{
}
because it looks much nicer.

 
Flag Post
Originally posted by Ace_Blue:

And yes, curly braces are usually superfluous when they only embrace a single instruction

Unless you want to add a trace statement in there to see if that line of code is being run, in which case it’s convenient to have the braces already there.

 
Flag Post

Surprising this hasn’t erupted into a flame war.

I prefer non-nestled curly braces. AFAIK, nestling them IS more common overall. But for some reason, the flash community tends to prefer to not nestle them.

 
Flag Post

my own thoughts on this.
http://www.kongregate.com/forums/4-game-programming/topics/336036-expecting-rightbrace

remember kids

}//Place proper comments after the rightbrace when necessary.

 
Flag Post

I think that this:

thing
{
}

looks like garbage and it doesn’t make anything more readable. Some say its easier to match code blocks and I think it’s just as easy without. Others like that there is a line of [mostly] white space before and after each code block, that makes total sense so what I do is

if(something){

doSomething();
}