Sunday, November 16, 2008

Once upon a time...

Some coders have very strong convictions on the proper use of curly braces. I'm not one of them, but I do have some preferences.

To me, placing the opening curly brace on the same line as the function statement is just a little more elegant and poetic. I see

function someFunction(){

...

}


as much like

Once upon a time

...

The End


The opening curly brace is part of an opening statement. Putting the curly brace on the next line seems a little like putting a period on the next line after the end of a sentence.

When working with a body of code that already exists, I don't have any problem conforming to it's style. But when I have the freedom to set my own style, I certainly prefer the curly brace on the same line.

7 comments:

Keith Peters said...

You are so WRONG! Only idiots do it that way!

:) just kidding.

I prefer the brace on the next line, but I've flip-flopped on it over the years. I see it like:

function someFunction()
{
...
}

is like:

Chapter 1
blah blah blah
...
end blah

Kristin Henry said...

Hahaha! I knew you couldn't leave that post alone, Keith ;-)

I guess I don't think of functions as being chapters, since a chapter seems like too long. I like to keep my functions short, like a short story :-) If a function starts getting too long, I start looking for better ways to encapsulate.

Chris Allen said...

You guys are both wrong! You should keep all code on the same line. :P

function someFunction(){ ... }

Seriously, I'm surprised that you opened this giant can of worms! :)

polyGeek said...

I'm with you Kristin. I've been meaning to write a little AIR utility for CORRECTING code that I copy that uses the WRONG format! :)

Anonymous said...

i am a fan of the curly braces on their own line... i think it makes the code a bit easier to read. I don't really understand how see putting the brace on it's own line is like putting a period on the line after a sentence because the opening curly brace is not the end of the function, it doesn't correspond to a period =)

Kristin Henry said...

ha! ok...let's say that it's like putting a semi-colon on the next line :-)

I'm really curious. How does having the curly brace on the next line make it more readable? Does it help you visually organize the code?

Jacob said...

semi colon works =)

perhaps the code isn't more readable, but i feel like it is organized better. The curly brace being on its own line makes it really easy to see where matching closing braces are, easy to spot the function declaration or conditionals. It all boils down to personal preference obviously, but i always feel like the code is easier to go through especially if looking at code you didn't write.