Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > General > Best Practices

Reply
 
Thread Tools Rate Thread Display Modes
Old 03-10-2010, 12:00 AM   #1
cpucpu
Member
 
Join Date: Jan 2010
Posts: 80
Default What do you think about my coding convetion?

Hi all,
This is the way i've been coding so far, i am sure of somethings, in some not. So you tell me.

ActionScript Code:
package the.package.path {         /**      * ...      * @author me      */         import a.class.Path;     import flash.events.Event;         public class ClassName     {         public var TheVariable:*;         protected var _TheVariable:*;         private var _theVariable:*;         public const THE_CONSTANT:*;         protected const THE_CONSTANT2:*;         private const THE_CONSTANT3:*;         public static var TheVariable2:*;         protected static var _TheVariable2:*;         private static var _theVariable2:*;         public static const THE_CONSTANT4:*;         protected static const THE_CONSTANT5:*;         private static const THE_CONSTANT6:*;                 /*Somethimes i do it, so:         public var TheVariable        :*;         protected var _TheVariable    :*;         private var _theVariable      :*;         public const THE_CONSTANT     :*;                                         ...         */                 /**          * Constructs the object          *          * @param theArgument receives and stores something          * @return void          */         public function ClassName(theArgument:*)         {             //A comment positioned always before the code that is being commented             var aLocalVar = theArgument;             addEventListener(Event.Enter_frame, _onEnterFrame, false, 0, true);         }                     public function MyFunction():void         {                     }                       protected function _MyFunction():void         {                     }                       private function _myFunction():void         {                     }                       //         //Note how i strategically agroup related methods, generally do not use spaces         public static function MyFunction():void         {                     }               //         //Also note that i decore static methods as its relatives non-static methods         protected static function _MyFunction():void         {                     }               //         //And when something becomes too largeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee,         //around 80 or 100 characters, i break the line         private static function _myFunction():void         {         }                 //         //A listener         //Note the use of underscore even on handlers, if the function private         private function _onEnterFrame(e:Event):String         {             return trace("some string");         }                 /*I used to write hanlders this way, but know no more         private function _enterFrameHandler(e:Event):String         {             return trace("some string");         }         */     } }

What would you change?
cpucpu is offline   Reply With Quote
Old 03-10-2010, 12:53 AM   #2
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 1,571
Default

I guess the first thing I'd ask is : Are you on a team? Do they code this way too? Is it understandable among all the team members? If so, the convention serves its purpose in your local context.

As for me... honestly? I'd find it almost unreadable just at a glance. Using underscores as a leading character in a function name is completely alien to me. I've only ever seen those reserved for variables... encapsulated ones, at that. Also, the fact that *anything* other than the constructor/class definition starts with an upper case letter is incredibly confusing. That's just not the standard AS convention.

This thread makes me wonder why people have all of a sudden decided to start capitalizing the first letter of non-constructor function names and variables. That was never an ActionScript convention, but over the past year I have begun to see it more and more on the forums. Is it a C# / C / C++ thing?


Also... why is the enterFrame handler returning a value? Event handlers return nothing in AS.
__________________
http://www.scriptocalypse.com - It's not the end of the world. It's just Actionscript!

man.mask = mask_mc;

Last edited by maskedMan; 03-10-2010 at 12:57 AM.
maskedMan is offline   Reply With Quote
Old 03-10-2010, 01:59 AM   #3
cpucpu
Member
 
Join Date: Jan 2010
Posts: 80
Default

Hi, thanks for your comments.

Well, although i am single developer i want to write things the most standard possible preventing the future. I remember some time ago i read for coding conventions and i might have misunderstood somethings, i may have taked the variable convetion name to methods as well.
And the handler returning thing, that's a mistake of mine.

What about everything else, is there anything you didn't reported? the order of the variables, methods, text length, anything.

Last edited by cpucpu; 03-10-2010 at 02:03 AM.
cpucpu is offline   Reply With Quote
Old 03-10-2010, 02:35 AM   #4
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 1,571
Default

There are some pretty commonly held standards among the professional Flash community. The ones I pointed out are probably the most important. Most of the other things I'd mention aren't as important (at least in *my* opinion).


Just remember these things:

functions:
ONLY the Constructor starts with an upper-case letter
NEVER start a function name with an underscore. Always a lower-case letter.

constants:
ALL letters are upper-case, with words separated by underscores.

variables:
NEVER start with an upper-case letter.
If private or protected, start with an underscore.
If public, do NOT start with an underscore.
get/set accessors for private/protected variables ALWAYS have the same name as the variable *without* the underscore.
Code:
private var _foo:String = "foo";
public function get foo():String{
  return _foo;
}
public function set foo(value:String):void{
  _foo = value;
}




It's pretty common to place variable declarations at the top. It's also fairly common to place static variables and functions before everything else, but not universal. Something like this is the most common that I see day-to-day...

Code:
class declaration
  static CONSTANTS 
  static variables 
  static accessors (get/set)
  static functions 
  

  local CONSTANTS
  local variables
  local accessors (get/set)

  Constructor 

  local functions
Honestly, as long as all the constants and variables are declared at the top, that's enough for most people.


On the subject of
onEventName vs. eventNameHandler

I don't think it's important which one you choose, as long as you stay consistent. This makes it easier on you when you (or someone else) use find/replace to search out function names. I favor the first in code I write, but it's really not a big deal.




I'm sure someone will completely disagree with what I consider to be readable and acceptable, and furthermore will find my own practices almost unreadable.
__________________
http://www.scriptocalypse.com - It's not the end of the world. It's just Actionscript!

man.mask = mask_mc;

Last edited by maskedMan; 03-10-2010 at 02:38 AM.
maskedMan is offline   Reply With Quote
Old 03-10-2010, 02:46 AM   #5
cpucpu
Member
 
Join Date: Jan 2010
Posts: 80
Default

So you don't use first letter uper case for public vars, like:
public var MyVar:*;

neither for protected ones, like:
protected var _MyVar:*;

those are uncommon?
cpucpu is offline   Reply With Quote
Old 03-10-2010, 03:41 AM   #6
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 1,571
Default

Quote:
Originally Posted by cpucpu View Post
So you don't use first letter uper case for public vars, like:
public var MyVar:*;

neither for protected ones, like:
protected var _MyVar:*;

those are uncommon?
Incredibly uncommon.

Let me put it this way, the most common standard is to make your own function / variable declarations most closely resemble those of the native Flash objects / functions / variables.

Have you seen *any* functions or variables that start with a capital letter in the AS3 native library (apart from Constructors and constants)?


For further reading, here's what Adobe has to say on the matter.

http://opensource.adobe.com/wiki/dis...entions-Naming
__________________
http://www.scriptocalypse.com - It's not the end of the world. It's just Actionscript!

man.mask = mask_mc;

Last edited by maskedMan; 03-10-2010 at 03:55 AM.
maskedMan is offline   Reply With Quote
Old 03-10-2010, 03:52 AM   #7
cpucpu
Member
 
Join Date: Jan 2010
Posts: 80
Default

Ok, understood.
I'll try to imprve my coding many thanks.
cpucpu is offline   Reply With Quote
Old 03-10-2010, 04:17 PM   #8
TomMalufe
Salmon Eater
 
TomMalufe's Avatar
 
Join Date: Jun 2007
Location: Doylestown, PA, USA.
Posts: 740
Default

I agree with everything maskedMan said above.

Personally, the only thing I would add, is that if a class file is longer then 300 lines of code I will usually go through and alphabetize everything. Plus I will try and name my functions in a way that keep similarly tasked functions together when alphabetized. This is why I like to write all my event listeners as "onEvent()" so they all start with "on"

This helps me (and others) find functions quickly without having to use the Find functionality.

Otherwise, like I said, I agree with everything else said here. Just wanted to add my 2 cents.
__________________
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." -- Chinese Proverb.
TomMalufe is offline   Reply With Quote
Old 03-12-2010, 06:58 AM   #9
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,140
Default

read and follow this: http://opensource.adobe.com/wiki/dis...ng+Conventions. Don't stray far and if you do, make sure it's internal to the class.
__________________
trace("Good bye Flash.") Log.i(TAG, "Hello Droid");
Flash Gordon is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:43 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.