Open Mike: Brackets

Open Mike: Brackets

Allman or K&R? This is Open Mike, a series of discussion posts to throw the cat amongst the pigeons. These posts are all about you — we want to hear your opinions, ideas, and thoughts. To kick things off, let’s talk about brackets.


1: Do you Cuddle?

“Cuddling” braces means writing them like this:

function showCuddlingExample():void {
	if (example) {
		//do something
	} else {
		//do something else
	}
}

Alternatively, there’s Allman style:

function showAllmanExample():void
{
	if (example)
	{
		//do something
	}
	else
	{
		//do something else
	}
}

This Wikipedia entry lists other common styles. Which do you use?

I like Allman because you can do this:

//if (someCondition)
{
	doSomething();
}

I can easily turn the conditional check off by simply commenting out the ‘if’ statement. If I was cuddling braces, I’d have to either write a new brace to replace the ‘if’, or comment out the corresponding closing brace.


2: Do You Pad Your Parentheses?

Compare all of these:

//tight
function exampleFunction(arg1:int, arg2:String):void
//space before parentheses
function exampleFunction (arg1:int, arg2:String):void
//space between variable and type
function exampleFunction(arg1 : int, arg2 : String) : void
//padded parentheses
function exampleFunction( arg1:int, arg2:String ):void

I’ve seen all of these, in various combinations. I used to use ‘padded parentheses’, as it seemed easier to see the arguments and their types, but now I prefer ‘tight’. What about you?


3: Do You Nest Parentheses in Conditions?

To me, it feels “correct” to write:

if ( (condition1) || (condition2) )

…rather than:

if (condition1 || condition2)

…even though the first takes up a lot more space. The parentheses make it clear where the separation lies. But is that small distinction worth it?

One last thing: thanks to Sergio from www.artua.com for the awesome microphone icon!

Add Comment

Discussion 41 Comments

  1. Konstantin says:

    1. I like Allman style more, but I used to cuddle them when I was a beginner. My codes looked messier when I did it :D

    2. I use tight style most of the time, but I don’t really pay any mind to this

    3. I nest conditions in parenthesis only when it’s necessary, like in some math expression.

    I would also add another questions like:

    Do you use underscore in the names of private variables?

    private var _someVar:Number;

    or?

    private var someVar:Number;

    I use ks_ instead of underscore, which (I believe :D) makes my codes more unique, lol

    Or some other conventional stuff like using all caps for constant names.
    I always do it, since I read Colin Moock’s essential actionscript 3

  2. Terje says:

    I’ve switched between cuddle and Allman style. At the moment I cuddle, but reading this article made me go back to Allman… I love the tip about commenting out the condition. Easy and quick.

    And I am certainly a no-padding guy who finds nesting paranthesis in conditions a better read.

  3. James says:

    I mostly cuddle my braces,although i have been trying to move away from it.Hard to break a habit I’ve been using for so long though.I do sometimes find myself both cuddling and using Allman style in the same code,which is not a good thing ….for having consistent code.

    I like the tight style for parentheses.

  4. Dan says:

    1. Allman Style
    2. Tight
    3. Nest

  5. Andrew says:

    1. I cuddle, raised on C++ I think a lot of them cuddle.

    2. I used to not pad, but as a flash development instructor I have found padding helps students read the code, so I have adjusted to padding (4th example) for that reason.

    3. I don’t nest as in the example above, only when I want to show a clearly defined set of operation order. Such as, if( ( condition1 || condition2 ) && condition3 )

    • I agree with your second point: when I write my own code I do not pad, but when I am teaching students padding is essential in helping understand; specifically when students are far away from the screen or when they are looking back and forth between my code and theirs.

  6. Martin says:

    Allman
    tight
    don´t nest

    I thinks a good reference for these questions is also: http://opensource.adobe.com/wiki/display/flexsdk/Coding+Conventions

  7. alexxcz says:

    Sometimes I mix them when I want tighter code but most of the time I use Allman style. IMO it’s cleaner and it feels natural.
    I use tight padding style just for because it looks cleaner to me. Mostly I use padding for variable declarations only for the purpose of nice formatting:

    private var _someVar :String;
    private var _anotherVar :Object;

    public var publicVar :Number;

    Also I sort the variables by their modifier and their data type. I usually do this after I finish with a class or when I’m in the mood for doing something else than coding.
    As for nesting I use it only on complex conditions to make it easier to understand the order of the statements.

  8. Scarybug says:

    I cuddle, but I don’t cuddle } else {. I imaging the function or condition as the head and the end } as the tail.

    I pad, because FlashDevelop auto-pads, and I like my code to be consistent.

    I only nest if I’ve removed a boolean operator from the condition. Too many parens starts to make it look like LISP.

    • Author

      Yeah I could see how that works, with the function + { being the head line. I guess I’m just used to matching { with }, rather than (any opener) with }.

      FD auto-pads? Maybe that’s an option I turned off at some point. I don’t think event listeners will auto-generate the most likely events if you pad. In fact I think that’s one of the reasons I switched.

    • Konstantin says:

      Flash Develop auto-pads only on default settings, you can easily turn it off

  9. Oh, I’m a cuddler I am, not sure why, I personally think it looks nicer than Allman. The point about being able to comment out allman easily is a great point, but I get lost in my own code so easily, not sure how my mind would cope, might give it a go next time round.

    I don’t pad my parentheses no, it’s odd but I think that they look more attached when they are next to each other, just makes sense in my head.

    I nest as well, just looks more right that way.

    It’s odd but in my head things make more sense when I can see them connected and structured like that. Or maybe I’m crazy who knows.

  10. Rich says:

    I tend to use the Allman style(I never knew it had a name) for classes and functions except getters/setters. And then cuddle for all other blocks.

    I used to pad parentheses, but since I’ve started writing more javascript I’ve fallen out of the habit.

    Not sure the final ones a coding style, use it when you need to. Unnecessary use obscures intent IMO.

  11. André says:

    It´s nice to participate these discussions, thanks for inviting Mike!

    1.
    About cuddling braces, there is a convention wich can be found in adobe´s homepage, the convention says to use Allman style, but I prefer using Cuddle style, i know Allman keeps more organized but i always separate my functions with some commented lines, like this:
    function 1(){ … }
    //———————
    function 2(){ … }

    It´s my own style, I keep myself more organized this way, also sometimes i write the whole function inline, for example getter functions or single lines functions, like this:
    public function get myProperty():String{ return “This is my property”; }

    There are sometimes that I dont use braces, for example:
    if(myvariable==”something”) trace(“Yes, it´s equal!”); //usually use with return and break operations.

    2.
    Padding parenthesis is on the adobe´s convention too, but I dont pad it, I keep myself more organized on my way, but when I know when I am writting some code that another person will use i always use the convention for that, but I think not padding I Keep myself more organized.

    3.
    Nesting parenthesis on conditions keep more organized, but there are times that i think better not nesting, for example, to check if a variable is setted and it´s not a “false” value i use this way:
    var variable1:String=”test”;
    var variable2:Boolean=true;
    var variable3:String;

    if(variable1 && variable2 && variable3) return something;

    Remembering this condition up here only checks if the variables has a VALID value (it´s setted and it´s not false/null/undefined/0 value), in this case it will not return because variable3 is declared but not setted, if we set that variable the condition wil return something. But when the condition requires more complexity (variablex==”somevalue”), them I think it´s much better nest parenthesis even it takes more space (like said Mike), it wil be more organized for latter modifications.

    • Author

      Cheers André :)

      I like the idea of separating functions like that; on a similar note I like what I’ve seen in a lot of code, which is to put comments alongside closing braces:

      for each (var child:Object in object.children)
      {
          if (whatever) {
              /* code goes here */
          } //end if (whatever)
      } //end for (through children of object)
      

      I’ve never been keen on doing things in one line, though; I can see the benefit, but it makes it way harder for me to parse the line. I think the only exceptions I would deal with are lines for debugging and if statements that just call a break or return… which I think is what you said, actually!

      • André says:

        I have my own way to write my codes, but i know it´s completely wrong, hehehe, but it´s the way i keep myself more organized, about comments, i use only when i am creating a tutorial, but to explain the code, in another instances i never use comments, but i never got lost.

        But in my opinion the most correct is to read the convention and use like that, because there explains not only the code format but how to documentate your code, as i could see the way you code is like the convention, but my way is completeley another way, but it´s how I feel myself more organized.

        It´s a very important discussion you´ve created here, i think it´s much more importanto tham a lot of tutorials and articles here, it helps developers to understand codes from other coders.

  12. LanceSnider says:

    1. I’m a cuddler. It just looks more organized to me, though I know it’s not the standard.
    2. I’m ‘tight.’
    3. Never. It seems unnecessary to add extra code like that.

  13. Daniel Apt says:

    I recently started to use Allman style. But then only on functions, not for if, else, etc. (loops and branching).

    I don’t pad my parentheses.

    I only nest parentheses when the conditions become very complicated.

  14. socreative says:

    Allman
    tight
    don´t nest

  15. wquist says:

    I’ve always used Allman style… It just looks more organized to me.
    However, sometimes I’ll put no brackets if its just one line (like for returns or traces)
    if (this)
    return;

    I don’t pad my parenthesis, but I do pad return statements. It seems like its “grouped” to me.
    public function myFunc(arg1:int, arg2:String) : void

    I dont nest if its only a bool like “if ((this) || (that))”, but I will if its like “if ((arg1 == 2) || (arg1 == arg2))”

  16. Author

    Interesting that so many of you have mentioned leaving boolean variables out of parentheses, while keeping conditions in them. That’s a great idea, and feels like a decent compromise between readability and length. Thanks guys!

  17. Jeremy Green says:

    I usually use Allman style. However, when I write javascript, I always cuddle the brackets. I never pad my parentheses. For me, whether I nest my parentheses just depends on my order of operations I guess; sometimes I do and sometimes I don’t.

  18. Jack Doyle says:

    1) Definitely cuddle. In my view, it makes things much more readable and the Allman style wastes a lot of space (more scrolling). I know it’s popular, but Allman is one coding habit that really annoys me.

    2) I’m quickly becoming a fan of padding for readability, like exampleFunction( arg1:int, arg2:String )

    3) I typically only nest in complex situations (to make things more readable). Never for Boolean variables.

    And for what it’s worth, I like adding the underscore prefix to private/protected properties and methods to make the scope clear throughout.

    That’s my 2 cents; keep the change.

    PS It’d be really cool if this were a poll of sorts that allowed folks to quickly see the tabulated voting results.

  19. Fredrik says:

    - Cuddle Style
    - Tight
    - Nest

    …and as Mr Doyle suggests, yeah it would be cool to see some kind of voting/poll results :o)

  20. John Reyes says:

    Non-nested, loose, Allman style… =)

  21. Jayc says:

    I usually cuddle, but now I’m starting to use allman on classes & functions, still cuddle if else & loops though. The thing I don’t like with allman other than having more lines & the longer scrolls is when they fold, FD folds on the {, I’m much used to fold on the function name itself.

    I pad for more readability.

    I only nest complicated & long conditionals, then I usually break lines on primary boolean operators with long groups.

  22. Franci says:

    Interesting discussion.

    1. I used to Cuddle, now I Allman, makes it for a more readable code in my opinion. :)
    2. Used to be tight, now I’m padded, also for easier readability.
    3. And I don’t nest, I think the || , && signs are distinction enough for simple statements, but if you’re comparing a calculation that might change.

  23. MSFX says:

    I dont cuddle, I dont pad but I do nest :)

  24. Will says:

    I use the Allman style of cuddling. I used to be against it but Adobe is very much for it in a lot of their tutorial videos.

    For padding parentheses I only usually do it when when instantiating inline eg:

    var myClass:MyClass = new MyClass( new MovieClip() );

    Nesting parentheses in conditions like in point 3 is a waste of time in my opinion. The logic operators are distinct enough to offer a logical separation.

  25. Well, I am a designer more than a coder, but when it come to coding and Like to make the code lines separated, so I use the Allman style instead of cuddle.

    And for padding, I prefer the tight style instead of adding spaces around each variable and condition.

  26. RCDMK says:

    I write my code in a convention depending on the language:

    In javascript (except the type declaration) and actionscript I write like this:

    function foo(myParam:Type, myParam2:Type):bool {
    if (something) return false;

    if (otherThing && yetAnotherThing == “value”) {
    // do something
    }
    }

    In C# I usualy write the Allman’s style cause VS automatically do this, I mantain a standard flow and readability.

    Some conventions like padding and nesting are very dependeble on the case. Most of the time i don’t padd but somtimes, when there are too much parammeters, i like to padd them for readability.

    I only nest my conditions when there’s need to it, like in oposing conditions (something && (anotherThing || yetAnotherThing)).

    Love to see some pool results on these.

  27. cuddle says:

    ofcourse cuddling is the best, because it´s lesser lines of code.
    in javascript you should use curly braces too.

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.