Code Hinting Regardless of Naming Conventions

ActionScript 2.0 Quick Tips

Depending on my mood, I may precede certain variable names with a small prefix that describes the type of object they point to.  For example, I may give a movie clip the instance name mcBall, rather than just ball.  Why?  Well, it allows me to see at a glance that I’m dealing with a MovieClip instance, which can come in handy during coding and also while I’m poking through the Debugger panel.  It doesn’t have any measurable effect on the functionality of the variable … it’s just one of those things you get used to.  I certainly don’t always adhere to this convention, but when I do, I’m practicing something called Hungarian notation, which has a decent pedigree (at least, in computer years).

Flash provides at least one naming convention that actually can make a practical difference, if you follow the suggested suffixes in the “About using suffixes to trigger code hints” section of Learning ActionScript 2.0 in Flash.  I’m not especially a fan stylistically, but, for example, if I name that ball clip ball_mc, I’ll get automatic code hinting for the MovieClip class (and so will you) in ActionScript 1.0 and 2.0.  A full list of suffixes is listed in that section.  Code hinting is definitely a useful tool, because I’m not always familiar with the class members of the object at hand.

What if you don’t like suffixes?  Or prefixes, for that matter?  Well, if you use AS2’s strong typing syntax (the :Number in something like var total:Number = 5;), it doesn’t matter what you name your variable:  you’ll get code hinting if you want it (see File > Preferences > ActionScript).  That’s fine for everything but movie clip instance names, which aren’t necessarily declared as variables.  But … see, if you declare instance names anyway — even though you don’t need to — you get the benefit of code hinting regardless of the instance name.

With a simple line like this …

var ball:MovieClip;

… even though you haven’t set that instance to anything, you’ll get MovieClip-centric code hints for subsequent references to that instance name in your code.

4 Responses to “Code Hinting Regardless of Naming Conventions”

  1. Tiemen Says:

    I prefer suffices to prefixes, simply because the prefixes accidentally creates weird words that distract from the actual object :P The mcDonalds syndrome :)

    That works great for naming variables but I’m confused about the use of variable names, instance names and symbol names.

    Take this example:

    var mcl:MovieClipLoader = new MovieClipLoader();
    var variable_name:MovieClip = this.createEmptyMovieClip("instance_name", _level1)

    mcl.addListener(this)
    mcl.loadClip("1.jpg", instance_name)
    mcl.loadClip("2.jpg", variable_name)

    You’ll find it loads 2.jpg, but the debugger outputs this:

    Level #0: Frame=1
    Movie Clip: Frame=1 Target="_level0.instance_name"

    Isn’t that confusing?

    For this sake I name my corresponding variables, instances and symbols exactly the same. But I wish wouldn’t have to.

  2. David Stiller Says:

    Tiemen,

    Symbol names are authoring conveniences only. Whatever a symbol is called in the Library is invisible to ActionScript. The only exception in AS1 and AS2 is Linkage ID, which can be added to a Library asset by right-clicking / Command-clicking. Linkage ID lets you “pull” an asset from the Library at runtime, using, say, MovieClip.attachMovie().

    Variable names and instance names are functionally identical. If you give a symbol an instance name in the Property inspector, that instance name effectively becomes a variable for you automatically. Variables point to instances of objects. Instance names are simply variables.

    In your code sample, variable_name refers to a MovieClip instance. Since you don’t seem to be using ActionScript to instantiate this object — as have with the “instance_name” clip — I can only assume you put the “variable_name” clip on the Stage by hand and gave it an instance name.

    The reason you’re having trouble with the “instance_name” clip is that you’re trying to pass in a level as the second parameter of that createEmptyMovieClip call. That second parameter requires a depth (not the same as a level: level’s have depths).

    Looks like you want to use …

    _level1.createEmptyMovieClip("instance_name", 0);
  3. Tiemen Says:

    In your code sample, variable_name refers to a MovieClip instance. Since you don’t seem to be using ActionScript to instantiate this object — as have with the “instance_name” clip — I can only assume you put the “variable_name” clip on the Stage by hand and gave it an instance name.

    No I didn’t. I just apparantly didn’t know how to set it up properly. And the same goes for the _level thingie. (as I also did wrong in the “Is _root Evil?” entry; that’s why I couldn’t get it to work)

    But back to naming: Of course (me?…right) I know that symbol names do not matter, actionscriptwise- but just in practice, when I’m creating movieclips inside movieclips, inside buttons, inside movieclips… - I find it hard to stick to names that 1. stay together in the library (preferably without having to use folders) and describe the symbol in a strong fashion..

  4. David Stiller Says:

    Tiemen,

    Library organization is subjective, of course. :) I happen to find Library folders useful, but I can understand wanting to see everything at a glance. I tend to name symbols from macro to micro. For example, a volume slider might have a knob and a track, and both are nested inside the slider symbol itself. I would probably call the slider “slider,” the knob “slider knob,” and the track “slider track” — that way they stack in proximity in the Library.

Leave a Reply