Clean Code



Meaningful Names


1. Intention Revealing Names
2.


Intention Revealing Names


The name of a variable, function or class should describe its purpose.
If the name requires a comment, the name does not reveal its intent.

public var hp:int; //health
public var health:int; 

By using meaningful names, it makes it easier to understand the what the code does within a particular function or class.

//Before
for(c in cArray){
switch(t.color){
case 1: c.stop(); break;
case 2: c.slow(); break;
case 3: c.move(); break;
default: c.stop(); break;
}
}
//After
for(car in cars){
switch(trafficLight.color){
case RED: car.stop(); break;
case YELLOW: car.slow(); break;
case GREEN: car.move(); break;
default: car.stop(); break;
}
}


Avoid Disinformation


A simple disinformation is using letters O,0,I,l (disinformative names)

if(l==1 && 0==O)
{
l = 0;
O = 1;
}
else
{
l = O = 1;
}

Another would be using abbreviations…

var ap:AP; //it could be referring to 
// ability points?
// action points?
// attack power? ... disinformative


Use Pronounceable Names


It makes it easier to understand and remember, and while discussing
this with a friend, your friend would not misunderstand what you are
trying to convery.
Alex: “Hey Alice, could you check grispratebimoneva?”
Alice: What???
Alex: “Could you check group success rate by monster evasion?”
Alice: “Oh that? Sure”

private var _grpSrateByMonEva; //before
private var _groupSuccessRateByMonsterEvasion; //after


Use Searchable Names


If you use short names, it would be very difficult to search for a particular variable name or function name. Using long name is better. Below is an example of using short name. If you would like to search for e variable, you have to encounter a few unnecessary variables such as ae, be, ce along the way. Names should be unique!

function getTotalClick(e:Event):void
{
trace(e);
if(ae== e){doA();}
else if (be == e) {doB();}
else if (ce == e) {doC();}
}
function getTotalClick(event:Event):void
{
trace(e);
if(ae == event){doA();}
else if (be == event) {doB();}
else if (ce == event) {doC();}
}

About ayumilove

I am Ayumilove, if you see some impersonators its not me.
%d bloggers like this: