Naming Conventions & code style; What are they and why should you use them? I've been helping people on this forum for some time now and I see that some of the code that is posted is very confusing for a couple of simple reasons: naming of variables, class names and the way people format their conditionals.
First of all, everybody is entitled to their own way of coding and should do things the way they are most comfortable with. I'm not saying this is the only way, just giving some guidelines to help make it easier.
Naming Conventions. This is the way you name your variables and classes in the code. You say why? Well first of all it's about making a difference in what you mean with certain things. Is it a variable, class, function? It makes it easier to see this just by the name rather then looking it up somewhere hidden in your code. Here are the best practices:
Variables should always start with a lower case letter.
Classes should always start with an upper case letter.
Functions/methods should always start with a lower case letter.
Constants should always be upper case
Mainly when you specify a variable, class, function or constant, give it a name so people understand what it does.
var value1 = "hi"
var value2 = 10
var value3 = flashvars.name
var value4 = value2 + 1
This doesn't make a lot of sense right? You're better off picking some more meaningful names like these:
var welcomeText = "hi"
var visitors = 10
var username = flashvars.name
var newAmountOfVisitors = visitors + 1
See? it's right from the start clear what I was trying to store here. The same goes for classes and functions. It's useful to know what you can expect something is going to do or contains judging by the name.
Furthermore, make sure you CammelCase your names. This means that each word in the name should start with an uppercase letter. Say for instance you have a name like "myperfectexamplevariable" which is kind of hard to read in it's current state. Make it CammelCase like this: "myPerfectExampleVariable" Now it's easier to read.
Example:
package
{
public class MyClass
{
public static const MYCONSTANT = "hello"
private var myValue
public function MyClass() //constructor, should be the same as the class name. So use an uppercase letter
{
this.setMyValue(MyClass.MYCONSTANT)
}
private function setMyValue(value)
{
this.myValue = value
}
}
}
var myClass = new MyClass()
That covers just about all the naming conventions. See how clear that made it? It's instantly clear what is a variable and what is a class name or constant. Even the build in flash classes use this. Just think about MouseEvent.CLICK. CLICK is a constant and MouseEvent is a class.
On to the following:
Code Style. Okay this is more of a personal preference, so I'll go over the most used styles of coding. Just as long you understand why is more important.
First, let's take a look at the previous example. This is a very spaced out way of coding. All the { and } are on their own line. Same goes for the if statements etc. This makes a more distinct way of letting the viewer see the difference between statement header (function bla(args)) and the body of a statement (what is between the { and } ). I, for instance, prefer this as I find it easier to read.
Second is as following:
function doSomething(arg1, arg2) {
//...
}
Quite similar yes but it is a little more compact with the starting { on the same line as the function header. This one is very widely used, the PHP manual uses this for instance.
Bad Examples. Well that is about it, not much but just keep it clean and people will more easily understand what your are trying to do. Here are some examples I've seen recently of bad coding. I'm not trying to put people down so I'm not naming any or posting links, it's just examples.
function HowTo(MouseEvent) {
howto.x=0;
theX.x-=806;
}
Like what? MouseEvent is in this case a variable with the same name as a MouseEvent class. Very confusing. What if you want to create a new instance of a MouseEvent in the function...
and what is "theX"? X marks the spot?
players[0].addEventListener(MouseEvent.CLICK, Click);
function Click(event:MouseEvent){
score = 2
scorecounter.text = score;
}
So you're passing this mouse event to a class?... no wait it's a function... oh right.
onClipEvent(load){ moveSpeed=10;}onClipEvent (enterFrame) { if
(Key.isDown(Key.RIGHT)){ this._x+=moveSpeed; } else if
(Key.isDown(Key.UP)){ this._y-=moveSpeed; } else if
(Key.isDown(Key.DOWN)){ this._y+=moveSpeed;} else if
(Key.isDown(Key.LEFT)){ this._x-=moveSpeed;}}
Say what? oh wait, let's reformat that for you now.
onClipEvent(load)
{
moveSpeed = 10;
}
onClipEvent(enterFrame)
{
if(Key.isDown(Key.RIGHT))
{
this._x += moveSpeed;
}
else if(Key.isDown(Key.UP))
{
this._y -= moveSpeed;
}
else if(Key.isDown(Key.DOWN))
{
this._y += moveSpeed;
}
else if(Key.isDown(Key.LEFT))
{
this._x -= moveSpeed;
}
}
Oooh, that makes a lot more sense. See, I did nothing to the code other than add some returns and indenting and it makes it so much easier to read.
Well, I think you've seen enough now. Just keep in mind that this is mostly personal taste apart from the naming conventions, those are pretty much a best practice among all coding languages.
So next time you post something here, let's make sure it's something we can read properly so we know what you are doing and can help you a lot better.
PS: I like to thank my friend Nano for spell checking this :D