This is a trivial matter, but it’s been bothering me for some time.
Why does Flex Builder insist on putting the opening curly brace of a function declaration on the next line:
function foo():void
{
.. instead of:
function foo():void {
What is the point of this waste of space, which doesn’t make anything easier to read, but justs wastes space on the IDE and therefore requires more scrolling to read code? It is listed as one of the coding conventions for contributions to the Flex SDK; as the document points out, consistency is the rationale for choosing one over the other: a choice has to be made, and stuck to.
That’s all very well for the conventions for a single project; but this is a styling choice that is hard-wired into Flex Builder. I prefer Sun’s convention. When I’m coding I have two choices: (1) modify the line spacing style for every god damn package, class and method declaration; or (2) accept someone else’s arbitrary standard for my own projects. Why isn’t there an option for this, as per the Java editor in Flex Builder?
[ Update: According to the Flex Builder bug database, this problem has been fixed. Doesn't look like it me, though. ]




I couldn’t agree more. The curly brace below the function is ugly and I hope they add this as a preference.
I’m with you Joshua!
I like the brace on the next line because in my IDE of choice (FD) I can visually align the braces. I think it makes things much easier to read, but thats just me. Code however you like.
I always (and always have) put the { on the next line down. Thankfully Flex does it for me now :) but I do the same in PhpED, FlashDevelop and any other IDE I use. It’s useful because yes, it adds 1 line, but that one line is a visual break for your eye and the code within the method doesn’t butt-up against the declaration itself. It’s visually cleaner imho. But like all “coding standards”, it’s 100% personal preference at the end of the day!
I prefer dropping the first curly brace because its easier to visually line up a pair of braces when scrolling big blocks of code.
As you say it’s a trivial point, and I completely agree it should be customizable. But reference to your points as to why you don’t approve of the style, I personally prefer it because when you have a lot of embedded conditionals, loops and class/method declarations it helps to make sure everything is closing where it should close by knowing the corresponding opening/closing brace is vertically inline with the other.
See what you mean though, just checked my prefs in FB3 standalone on Mac and apparently it’s set to same line, which it definitely isn’t.
There is a styling preferences option in the FB configuration where you can set the default styling of braces, colours, etc. Its all up to personal preference in the end. Have to admit, after working through someone’s badly formatted C# code yesterday, I’m on the braces on a new line side of the fence.
Hi,
I’m also really upset about this decision to put the curly braces on a new line, no one seemed to complain about it, anyway now that Adobe has published it’s coding conventions, I think I’ll stick to it, even thought I find this decision very useless, because it adds tons of extra lines to my code, and honestly doesnt improve readabily at all, but oh well … I’ll get used to it :)
When you find out what/where to change the templates (or whatever Eclipse/FB uses) be sure to write about it. I, too, am well and mighty tired of editing every left curly brace I see.
I myself struggled with this question. I had always been a fan of using the least amount of lines possible. After a series of revelations I had a change of heart. In particular, I now prefer to have the curly brace on a separate line. Why? When you are writing a function that takes so many parameters that keeping them on one line causes the text to start creeping off the screen, at some point you wrap that line to the next line, or multiple lines. When doing so, and keeping the curly brace on the same line, the function ends up looking a bit confusing. The multi-line parameters list can look like lines of code for the function. Therefor, I now always keep my curly brace on a fresh line. Not only for functions, but any block of code that uses curly braces.
I agree with you that you should be able to change that default behavior (there really isn’t a setting for that?), but thought I should offer my opinion.
I too liked this approach for about 8 year of coding
foo(){
//
}
I then worked at a job where I had to conform to this approach.
foo()
{
//
}
After a week or so whining about it I got used to it and actually no prefer it over the other style. The biggest thing that I like about it is that it is very easy to visually match up the starting ‘{‘ and ending ‘}’.
As far as more white space in the code and more scrolling – I actually use the Outline mode and the ctrl-o shortcut to navigate around my class files, so that issue does not bother me too much.
I hope FB gets the bug fixed you mentioned.
–jason
While I prefer the adobe convention you can always uncheck Automatically Insert Closing Braces found at
Preferences > Flex > Editors > Action Script Code. It doesn’t automatically format your brackets the way you want, but you won’t be modifying your functions as you go. It might be a good alternative for the time being?
Curly braces on new lines make matching opening and closing braces easier. This allows you to easily find functions, for loops, etc. One of the worst things I find when editing other peoples code is finding where functions, for loops etc. begin and end.
I also prefer the new line approach, but I just played around creating a class in Flex builder, I then formated it to use the inline syntax and proceeded to delete my ActionScript project and created a new one by the same name, to my surprise Flex retained my preference and used the inline syntax. Looks like we underestimated Flex all along lol.
If you put the caret of the Eclipse SDK (Flex Builder 2) on the right of your opening brace {| it automatically highlights the closing brace {, and you can also click the – & + symbol in the gutter to collapse a set of braces… so I think the “visually aligned” argument is a total wash. It makes coding no easier in my opinion, and I too believe it adds too many unnecessary lines of code.
I do not prefer braces on a new line, but Eclipse forces me to “correct” it twice every time I create a new class. I have not found a preference to force it to follow my own preferred brace wrapping conventions in Flex Builder 2 (2.0.155577). I would love to know if this is possible in this version of Flex Builder 2.
@YAOMK… what version are you using?
Its on the next line because that actually IS better for the code, also, if you look around several languages, you’ll notice the bigger part of them (that uses those braces anyway) want you to put the brace on the next line, and still allow you to do both (as does flex afaik)
Chad presents a very good argument for using a separate line. (It provides flexibility for multiple lines in the function line itself.)
Kaleb Wyman argues that the IDE provides the highlighting for the starting bracket (making the separate line to mark the construct unnecessary). However, the developer is not the only one who reads the code. When I review someone’s code on our project (many files are over 100000 lines long), sometimes it is a comparison review. I review what has changed using differencing software (such as kdiff3). This has no highlighting feature for braces.
The extra line for the brace is worth the space. Vertical space issues can be mitigated somewhat by a coding convention. This is to place a comment after the closing bracket to indicate the context of the opening bracket as shown below. Often they are not on the same screen. When used especially for long functions and classes, I find this a time saver.
Starting Class_function_whatever
{
…
}// Class_function_whatever [several screen pages down]
I prefer the same line opening braces. I can get more readable code on the screen at one time. If one were so inclined, one could argue that it might even help save the planet using less paper when printed.
Great entry here though:
http://en.wikipedia.org/wiki/Indent_style
This will only serve to confuse the undecided.
Sun declared that Java should be typed with opening braces on the same line.