What are the rules for naming AS3 classes? - actionscript-3

I'm trying to write a RegEx for a code generator (in C#) to determine a proper class or package name of an AS3 class.
I know that class names
must start with a letter (capital or otherwise)
any other digit can be alphanumeric
cannot have spaces
Is there anything else?

Although you can start class names with lower case letters and include underscores and dollar signs, the "naming convention" is to start the class name and each separate word with a capital letter (e.g. UsefulThing), and not include underscores. When I see classes like useful_thing, it looks wrong, because it's not the naming convention. Maybe your question should have said what are the valid names for an AS3 class?
Other than that I think you + maclema have it.

The conventions for Class and Package naming as far as I've heard:
The package structure should use the "flipped domain" naming, with lowercase folders and CamelCased class names, i.e.:
import com.yourdomain.nameofsubfolder.YourSpecialClass;
This is reflected in all of the packages shipped with Flash and Flex. Examples:
import flash.events.MouseEvent;
import flash.display.MovieClip;
There is also a convention of naming Interfaces after the functionality they add or impose as in: Styleable, Drawable, Movable etc... Many (including Adobe) also prefer to use an upper case "I" to mark interfaces clearly as such, i.e.:
IEventDispatcher
IExternalizable
IFocusManager
which are all internal interfaces in the flash.* packages.

Here are some more valid classes.
Actionscript 3 classes (and packages) must start with a letter, "_", or "$". They may also contain (but not start with) a number.
public class $Test {}
public class _Test {}
public class test {}

Related

Using 'Class' and ES6

Is there a conflict of behavior for using the Class expression, since it became a reserved word in the latest version of Ecmascript? Can't say if there is identity of grammar between the two contexts, but I guess the built-in noun takes precedence.
JavaScript is case-sensitive. Its class keyword and MooTools' Class identifier don't conflict at all.
If you mean the "classes" generated, I wouldn't try to use one style to subclass a "class" created with the other style (though you might get it to work, particularly if using class to subclass a Class class), but other than that, there's no big conflict. They both create objects with associated prototypes.

Swift's standard library and name collision

I know that Swift doesn't use namespaces, but that names are defined within each module. First of all, I don't understand very well how this avoids name collisions -feel free to elaborate.
Nevertheless, my main question is: Let's say I want a tree structure without using NSTreeNode, so I make my own class named "TreeNode". Now let's say that Apple decides to include a class to build trees in Swift's standard library, and, as expected, they name it "TreeNode". What happens then? My custom TreeNode will collide with Standard Library's TreeNode... Will I have to change all my code in cases like that? Or maybe Apple will promise that Swift's Standard Library will not change in the future?
EDIT: Question answered here (thanks #Martin R for your comment)
Namespacing in Swift is implicit. All classes and other symbols belong to the target (module) they are defined in. So if you define a class String the fully qualified name would be MyTarget.String. When there is a name collision, you have to prefix the class name with the module (framework) it is defined in, except when there is a class with that name defined in the current module - this class takes precedence and does not need to be prefixed.
struct String {
var swiftString = ""
}
var a = String()
var b = Swift.String()
So if you create your class TreeNode and Apple later adds a TreeNode as well, your name would take precedence if you are using only one module and you wouldn't need to change anything. If you would want to use Swift's TreeNode, you would need to refer to it as Swift.TreeNode.

Is it bad to prefix all of my framework class names?

I develop a lot of frameworks for Flash games and applications. I have always prefixed my class names with a random character or two, to avoid conflict with class names that the developer may already have, for example:
class LEntity
Recently I had a co-worker blast me for poor and "annoying" naming of classes who then proceeded to rename every class in the frameworks I've created for people here to use.
I'm having trouble explaining my reasoning thoroughly enough for him to accept what I've done as a good approach.
Is what I've done above actually a bad thing? If not, how can I explain otherwise? If so, why?
Comments are asking about namespaces - I know AS3 in this example has what I know to be called a namespace but I'm not sure if this is the same thing or if it can be used as expected.
Given that Actionscript supports namespaces, there is no reason to use prefixes simply to prevent naming clashes. That's what namespaces are for.
Some people like to use namespaces to significy member variables (ie, underscore prefix, or sometimes m_) and that has some merit, but simply for the sake of name clashing no.
It sounds like you don't quite understand what namespacespackages are in AS3.
An example:
//Class1.as
package com.test.someModule { //This is the package/namespace
public class Class1 {...}
}
//Class2.as
package com.test.otherModule {
import com.test.someModule.Class1; //Class1 can be used as "Class1" now. Otherwise you would do "com.test.someModule.Class1"
import com.test.someModule.*; //You can also use the "*" to "import" all classes in that package
}
I have to agree with your co-worker, your class names are 'annoying'.
In Actionscript 3 we use the package name to define the namespace of a class. If you're not sure what namespace means, take the wikipedia definition (as of the time of writing):
"In general, a namespace is a container for a set of identifiers
(names), and allows the disambiguation of homonym identifiers residing
in different namespaces."
So you will never "conflict with class names" as long as you name your packages correctly. Most developers use what is called the reverse domain notation to name their packages (e.g com.mywebsite.MyGenericNamedClass). Domain names are unique so it's very unlikely you would clash with another class.
As a rule of thumb the class name should be as descriptive as possible, so some of your class names will be the same as someone else's class. Take the default Sprite class for instance:
import flash.display.Sprite;
import uk.co.mywebsite.Sprite;
if you then initialize an object:
var mySprite:Sprite = new Sprite();
The compiler would not know which Sprite you want to initialize (is it the flash sprite or your own custom sprite), and it would throw an error.
The solution is simple: because your packages have been named properly, all you need to do is to use the full class name including the package name to initialize your object:
var mySprite:uk.co.mywebsite.Sprite = new uk.co.mywebsite.Sprite();
var myOtherSprite:flash.display.Sprite = new flash.display.Sprite();
Mind you, you would rarely need to do that. This is only necessary if you want to use those two classes (the default Sprite and your own Sprite) in the same scope. Generally, you would only import your own class:
/* we are not importing this any more
import flash.display.Sprite;*/
//only importing my own class
import uk.co.mywebsite.Sprite;
/* now I can initialize my object without using the full class name, and the compiler knows
I mean my own Sprite class */
var mySprite:Sprite = new Sprite();

this[String] can't be added into a MovieClip

Lets say I have a MovieClip called "Box", and "String" is actually 'box' just that its not a MovieClip
The problem I'm facing now is I can't use something like circle.addChild(this[String])
I've tried tracing this[Strin]==Box and the result returns true.
And when I remove circle.addChild(this[String]), it does not add the Box into it.
BUT, when I traced is there any new object added to circle, the amount still remains the same.
Any idea what seems to be the problem here?
If I understood you, you are trying to add a MovieClip called (has a instance name of) String inside another MovieClip called Box, right?
Well, you can't give the name String to an object, because String is a class name in ActionScript 3.0
You can't name any object with any class name or protected keyword, such if, for, class, Boolean...
Those are words that ActionsScript uses globally (they are Top Level keywords).
Also, note that although you can name an object (give it an instance name of) MovieClip, Sprite, Loader, Stage and such, all theses names are used by ActionScript as class names, inside packages, that can be imported into your animation/application, and Flash will automatically import almost every one of them for you.
If you name your objects with those words you run some risks, like codes that don't work properly or don't compile at all...
As pointed by #Bosworth99 and #merv, you may note, as well, the naming conventions used by ActionScript, the UpperCamelCase and lowerCamelCase.
Classes are (by convention) written with UpperCamelCase (with the first letter capitalized), which indicates that name is a class name.
Objects are, generally, written with lowerCamelCase (with lower first letter and every new composed word with upper case), indicating that word is a object name (or another keyword, which may be reserved already).
Your syntax is a little odd - you say you have a MC called 'Box'. Are you creating this in the flash ide - or programatically? either way - you appear to be referencing Classes, and not instances of a class (an object). Try:
var _circle:MovieClip;
var _box:Sprite;
private function createDisplayObjects():void
{
_circle = new MovieClip();
this.addChild(_circle);
_box = new Sprite();
_circle.addChild(_box);
}
And - just as a generally agreed upon practice, class names are capitalized, and instance name are lowerCamelCase. I like underscores prefixing private vars, as well.
NemoStein is absolutely correct - reserved keywords will bork your code everytime...
good luck

How to avoid class name and namespace conflict?

Sometimes I have need to call namespace and class the same. For example, SomeProject.Compiler namespace, containing lots of compiler-related stuff and the main entry point class Compiler. But naming namespace and class name the same is not recommended, as it creates ambiguity and misleads compiler.
Is there any idea, how to name them better?
It is always possible to give general namespace name, which describes all its members. For example, CompilerServices instead of compiler.
Some books say you should write the domain of your company (in the reverse order) before the name of namespace.
Something like com.company.www.compiler