ActionScript.org Forums

ActionScript.org Forums (http://www.actionscript.org/forums/index.php3)
-   Best Practices (http://www.actionscript.org/forums/forumdisplay.php3?f=92)
-   -   Curly braces (http://www.actionscript.org/forums/showthread.php3?t=270349)

FEK315 02-09-2012 05:17 AM

Curly braces
 
Should I place a curly brace at the end a function or on the next line down?
EX:
public function curlyBrace() {

or

public function curlyBrace()
{

Which is more accepted?

wvxvw 02-09-2012 09:48 AM

Typographically, it makes more sense to put the bracket on the new line (or even, more properly, together with the following block it encircles), just the same way, if you had to design a book title, you'd not write it like so:

The Title of the
Book

you'd do it either like this:

The Title
of the Book

or

The Title
of
the
Book

The left bracket is opening a block, but if nothing follows immediately, then it feels strange. However Brian Kernighan and Dennis Ritchie formatted the code following their own understanding of typography (and those two are great men!) however, they are programmers, not graphic artists :) So they put the opening bracket on the same line. A lot of people who learned by their book had adopted this kind of formatting.
So, there isn't probably right and wrong (and, frankly, C-like languages are typographically ugly anyway), so that only depends on the convention accepted in the place you work. I, personally, find Alman style easier to read, but this is typical of people to find something easier to understand if they are accustomed to doing it that way.

ASWC 02-09-2012 10:39 AM

I started doing k&R, Alman (opening bracket in new line) 2 years ago and I don't think I can go back now. I'm also unable to apply different style for different language so I do K&R/Alman for Javascript, PHP, Phython, Java and in fact any other language I might use. But to answer your question, it's your choice, nothing wrong with one or the other.

[afz]snickelfitz 02-09-2012 11:58 AM

Personally, I find cuddled braces to be more difficult to interpret if the code is complex.
<div> is usually written on a new line to make the containers easier to visualize. The same logic applies to AS3 IMO. (especially classes)
ActionScript Code:
function curlyBrace():void{     // block content         if (something){         // block content                 if (something){             // block content         }     } } function curlyBrace():void {     // block content         if (something)     {         // block content                 if (something)         {             // block content         }     } }

For me, the formatting conundrum is with complex tweens
ActionScript Code:
TweenMax.allFromTo(array, 1, { x:0, y:0, alpha:0, blurFilter:{blurX:8, blurY:8}},{ x:100, y:100, alpha:1, blurFilter:{blurX:0, blurY:0}}, .1, trace, ["finished"]);

wvxvw 02-09-2012 08:53 PM

ASWC: how do you do either Alman or K&R in Python? Is there such thing anyway? The code formatting in Python is quite rigid and doesn't use curly brackets to denote a block of code, they are used for dictionaries... (well, maybe you can alter the grammar to look non-python-ish, but that's too advanced for me).

ASWC 02-09-2012 09:21 PM

Quote:

Originally Posted by wvxvw (Post 1116596)
ASWC: how do you do either Alman or K&R in Python? Is there such thing anyway? The code formatting in Python is quite rigid and doesn't use curly brackets to denote a block of code, they are used for dictionaries... (well, maybe you can alter the grammar to look non-python-ish, but that's too advanced for me).

I just listed the languages I work with without thinking about all cases. Of course Pytyon is not concerned by that since formatting of code in python is part of the code itself so not much choices for coding style. Just another brain fart from me ...

drkstr 02-09-2012 11:46 PM

The standard convention (according to Adobe) is to align the braces for all statement blocks.

I actually switched to this style a few years ago (against my will haha), but I actually quite like it now.

Barna Biro 02-10-2012 08:43 AM

I personally do a mix of the styles... Sometimes placing a curly bracket on a new line for a small method or conditions looks ugly in my opinion and just eats up unnecessary space, but then again, some things I do like to be separated nicely so that the code doesn't look like a huge continuous piece of glued text ( having to waste time then figuring out where certain things begin or end... comments could also be helpful but unfortunately, people either comment abusively or not at all - adding a crapload of unnecessary empty spaces / lines between lines of code is also quite ugly and could also make things somewhat hard to follow ).

ActionScript Code:
package {     public class Curly     {         public function Curly(value:Number)         {             if (checkValidity(value) == true) {                 loopLikeCrazy(value);             } else {                 throw new Error("ERROR! " +                     "Invalid value passed!");             }         }                 private function loopLikeCrazy(nrOfTimes:Number):void         {             var i:uint;             for (i = 0; i < nrOfTimes; i++) {                 trace("Looping like crazy! " + i);             }         }                 private function checkValidity(value:Number):Boolean {             return isNaN(value) == false && value > 0;         }     } }

FEK315 02-10-2012 05:54 PM

I have a tenancy to compact everything, but it makes it so hard to read.

I just got AS3 Design patterens by FOE but I haven't started it yet. I am half way through Learn AS3 by Oreilly and the author keeps changing between Curly bracket styles so I wanted to learn the most accepted way in case there was a difference, you know like Camel casing and underscore before the new object name.

It's easier to read when the braces or on their own line.
Mostly, I want to make it easy for other coders to see what am writing and to learn the best actionscript "grammar" practices.



Thank you all
:cool:

Xegnma 03-04-2012 12:36 AM

Quote:

Originally Posted by FEK315 (Post 1116721)
I have a tenancy to compact everything, but it makes it so hard to read.

I just got AS3 Design patterens by FOE but I haven't started it yet. I am half way through Learn AS3 by Oreilly and the author keeps changing between Curly bracket styles so I wanted to learn the most accepted way in case there was a difference, you know like Camel casing and underscore before the new object name.

It's easier to read when the braces or on their own line.
Mostly, I want to make it easy for other coders to see what am writing and to learn the best actionscript "grammar" practices.



Thank you all
:cool:

I you really want to make your code readable by other programmers I strongly suggest you pick up the book "Clean Code: A Handbook of Agile Software Craftsmanship" by Robert C. Martin.


All times are GMT. The time now is 05:58 AM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger