I'm just learning flash for a variety of reasons, and I already know C++, javascript and few dozen other languages, so I was hopping for something that just highlight the quirks and common use cases of Flash and AS3. The O'Reily texts are normally pretty good, so I turned to Actionscript 3 Cookbook so I went the most excellent Barnes and Noble at Union Square to do some bible dipping
Early on, it has sections on "how to do something repeatedly" (answer "Use for loops). A much later section has "How to disolve between two bitmaps" (answer "Use Bitmap.disolve" -- which was a rewrite of the existing documentation).
More serious problems
"How do you find the minimum or maximum or an array?" "sort then take the first or last element." Uhhh, if one of my guys did this, I would considering firing them.
More problematic is the section on shuffling an array. "Use sort, with a sorting function that returns a random number between -1.0 and 1.0". This will:
- Leave large parts unshuffled
- Could run very slowly -- as the sorting algorithm goes into semi-infinite loop
- Could run slowly -- as you are making function calls for each compare operation
- Could core dump / abort the program / throw an exception
The exact disaster would depend on the implementation and what sorting algorithm is used. Lots of people get shuffle wrong -- it really should be in the core language or library.
You want something like this (see any algorithm book or the mighty Knuth)
function shuffle(a:Array):void {
for (var i:int = a.length; i;) {
var j:int = Math.floor(Math.random() * i--);
var x:int = a[i]; a[i] = a[j]; a[j] = x;
}
/* OR
var x:int;
for (var j:int, i:int = a.length; i;
j = Math.floor(Math.random() * i), x = a[--i], a[i] = a[j], a[j] = x);
*/
// TBD: probably one can cast to int instead of using Math.floor
// to get more speed.
}
Summary
Well needless to say I didn't buy the book. Maybe I picked some bad sections. But I get the feeling this is for people new to programming.
0 comments:
Post a Comment