03
expression

giving instructions

programming is ultimately all about giving instructions to the computer. these instructions must be precise, employing specific values to indicate exactly how long or how much or how often, etc.

a distinct instruction to the computer is called a statement. it is terminated with a semi-colon (;).

x = 50;

but as in math, the statements we are typically interested in are more complicated than what we can trivially express directly, so we compose expressions and assemble them together to form statements of the complexity we require.

 my tip  equals  fifteen percent  of  the total bill.
(my tip) equals (fifteen percent) of (the total bill).
 myTip     =         .15          *   billTotal();

expressions are built of three fundamental units: literals, operators, and variables.

literals

plain naked values of assorted flavors.
true
3.1459
"a"
[1,2,3,4,5]
{x:5, y:2}

there are only five basic flavors of data in flash, and all other data structures are made from recipes using these ingredients

Boolean true, false
Number 1, 2.345, -6700
String "abc", 'abc'
Array [val0, val1, val2]
Object { "name": val }

operators

operators work between (binary), or next to (unary) other values in an expression to indicate the operation to perform when evaluating the expression.
17.96 / 2.48
-3
3 + 2 * 5
(3 + 2) * 5
math        +   -   *   /   % 
evaluation  (   ) 
assignment  =   +=  -=  *=  /=  %= 
comparison  ==  !=  <   >   <=  >= 
logical     !   &&  || 
incremental ++  -- 
see the full list in the ActionScript Dictionary: Operators

variables

variables are simple containers that allow us to access a value by name. this makes manipulation very convenient; we can let the computer keep track of the literal value while we apply various operations to it.

we define a variable with the keyword var and a label, and we use the assignment operator to associate a value with a variable.
var n = 2;

variables also let us collect synonymous literal values into a single location. anytime we need the value in our code, we use a reference to it. this allows us to make updates to the literal value in only one place while still being able to use the value in multiple places.

we can go from this:

var beginning = 5;
var end = 5 + 17;
var middle = 5 + (17 / 2);

to this:

var beginning = 5;
var distance = 17;

var end = beginning + distance; var middle = beginning + (distance / 2);

by making the value of one variable dependent on an expression involving another, we can build up complex relationships that encode a process. we can then feed different inputs to the process, and harvest new results without rewriting any of the code.

var leftSide = 5;
var interval = 3;

var mark1 = leftSide + interval; var mark2 = mark1 + interval; var mark3 = mark2 + interval;

documenting intent and context

comments provide a way to add extra information into code that is valuable to programmers, but is ignored by the computer.

single line

use forward slashes to comment out a line of code, or add a one-line comment.
// sometimes one line comments introduce the next line
var x = 3; // sometimes they clarify a specific operation

// sometimes they just deactivate code without deleting it // var z = 11;

multiline

use the /*, */ pair for commenting out multiple lines of code. note that the numpad has these keys right next to each other, so you can type the star without using the SHIFT key.
/*
    multiline, or block, comments exclude everything 
    between the comment markers from the compiler
*/
var x = 9;

/************************************************* * some people like to make boxes out of their * * comments. i think they're hard to maintain. * *************************************************/ var a = new Array(/* block comments don't kill the whole line */);

writing code (in flash)

select a frame for the code to be associated with and use the actions panel to:

executing code

CTRL-ENTER to preview in authoring environment (access to output window and compiler errors window)
F12 to publish (create all types checked in the Formats tab of the Publish Settings Panel) and preview
SHFT-F12 to publish only (no preview)

 

breaking code

there are many ways to introduce errors, or bugs, into code. the difficulty of correcting the error is usually related to the phase of the programming process where the error occurs.

compile time

incorrect syntax—spelling, malformed expressions (check with CTRL-T)
Var w = 5;
varx=6;
var y = 7 2;
var z = 83 - var w;

run time

requesting values that don't exist
var n;
var total = n + 5;
this.loadMovie("notReallyThere.swf");
var a = [0, 1, 2];
var b = a[3];

think time

correct syntax or even lack of run-time errors does not guarantee working code!
var totalGrades = 98 + 83 + 95;
var average = totalGrades / 5;

not breaking code

writing bugs is an unavoidable part of writing code; taking steps to minimize the number of bugs introduced is therefore wise.

use type-checking

if you start by telling the compiler what you think you're using, it will double check for you and prevent many simple errors from growing complex.
 var s:String = "boo-haloo";
 var n:String = 7;

change one thing at a time, and then test it

the test need not be elaborate, or even physical, but at least mentally executing the lines of code and verifying they will do what is expected is a critical habit to get into.

it is extremely easy in flash to create a new document, write a quick method and a couple lines to call it with various inputs, and print the output with trace(). once the code snippet performs as expected, copy and paste it back into the main program.

write only what you need

keep chunks of code small enough to fit on screen all at once. monolithic code is usually a haven for bugs.

specify the purpose of your code and test against it

code is arguably never perfect. but if it functions to specifications, it can be good enough. testing is the only way to find problems, so test early and test often, but tests are only useful if you know what the answer should be!

debugging code

by employing sound debugging strategies, you can refine your code to remove bugs and increase your confidence that it is functioning sufficiently for your purposes.

expose

use informative trace statements to indicate exactly what is happening at important stages of the program.

isolate

analyze the trace statements and comment out sections of code to find the lines of code hiding the bug.

re-test

bugs often travel in groups; finding one may reveal ways to test for others.

further investigations