ActionScript 3 Math.round < Number().toFixed()

I just found out some of the fancy features that Numbers have in AS3 and how they’re going to make my life so much nicer.

Typically if I wanted to round a number, I’d write it the following way:
trace(Math.round(5.4321));

But this way is much easier:
trace(5.4321.toFixed());

the toFixed method of numbers rounds to a specified decimal place. It assumes you mean 0 places, so toFixed() rounds to 0 decimal places. This means you don’t need to mess with any of the Math things for rounding.

Determining if your browser has a vertical scrollbar showing

In javascript, I needed to know if a browser was currently showing a scrollbar. This was fairly simple, but just thought I’d write it down.

window.innerHeight < document.documentElement.scrollHeight

Why a Single Point of Failure Can Be a Good Thing

I’ve been working in the corporate world for a while now, and I finally have figured out my personal difficulties with the transition from a small company to a large one. I’ve been there for the growth of two companies from a relatively small number of people to a larger one. The first company grew like wildfire and is now doing massive layoffs. The second company seems to be doing quite a bit better, but there’s significantly more tension than there used to be.

Working for a startup is a lot like having a child. The company is uncertain, it’s unprotected, and it’s completely dependent on other people to make sure that it grows up and succeeds. Like a parent, each member of a startup contributes something huge and important to the company. Without any of the various pieces, the company would fail quickly, and everyone in the company knows it. But, also like a parent, each member of a startup is almost religious in their devotion to the company. Every member is a spof (single point of failure), but the whole thing often ends up feeling a lot more like family than work.

Now that the company I work for is more of a corporation than a startup, there’s been a lot of talk about spof and how I need to fix that–we need to hire more people, train them to overlap my job, and ensure that if I’m out, my projects won’t fall to the wayside. I understand the need to feel secure in our company’s capability, but the sense of devotion and radical fanaticism to the company I work for has nothing to do with its well-being. It all hinges on the feeling that I, as an individual contributor am needed–and badly. The more I’m needed, the more I’ll throw myself into the mix, the more I’ll work late nights, the more I’ll innovate, create, and expand what I do because I have the opportunity to help make the whole thing better.

This is not better for my social life, my side projects, or my blog, but it’s significantly better than the alternative. My company no longer needs me. They want me around (I hope) but the only thing standing between me and layoffs is money, and the ability for the company to make money is no longer directly influenced by me. I’m a cog in a machine, and it’s by design that I’ve lost the ability to be more.

I wonder if there’s an alternative to the malaise I feel. Do people show up to Microsoft every day and say “god I’m happy to be a programmer here?” If so, where do they find their satisfaction? I suppose if I had kids myself, I’d be feeling the priority shift, but if it’s done right, startups don’t have to completely take all your time either, they just need a little extra love.

Making a ComboBox only show ToolTips for Long Items

I had a Flex Combobox that had contents too big to fit, but when I made it shorter to fit, the items would be cut off. There’s a brilliant solution on the FlexExamples site that handles it so:

<mx:ComboBox id="projectComboBox"
             dataProvider="{projectList}"
             maxWidth="400"
             itemRenderer="mx.controls.Label"/>

All this does is specify the use of a label control as the TextField in the ComboBox. Since Labels are made to truncate then produce tooltips automatically, this is amazingly simple.

Installing the Sun JDK in Ubuntu Linux

All I wanted was the Sun JDK on the new, shiny Ubuntu I spun up in VMWare. Eclipse (among other things) recommends you do this for performance reasons. So, I tried. I looked through the apt-get system, nothing. I looked at sun’s website and was greeted with a plethora of errors when I got the rpm, installed rpm using apt-get and then tried to rpm -ivh the package.
this is what my error partially looked like:

Failed dependencies:
/bin/basename is needed by jdk-2000:1.7.0-fcs.i586
/bin/cat is needed by jdk-2000:1.7.0-fcs.i586
/bin/cp is needed by jdk-2000:1.7.0-fcs.i586
/bin/gawk is needed by jdk-2000:1.7.0-fcs.i586
/bin/grep is needed by jdk-2000:1.7.0-fcs.i586

the final solution was on happy-coding.com and went as so (I would have linked, but it was tough to notice that you had to do 3 steps and not just two–keepin it simple):

apt-repository "deb http://archive.canonical.com/ lucid partner"
sudo apt-get update
sudo apt-get install sun-java6-jdk

Flash IDE / AS3 [object global] Inside of Dynamic Function

I ran across an odd situation inside the Flash IDE where I was trying to trace out parameters about the timeline’s class.

Here’s the code I was running:

import flash.events.*
this.addEventListener(Event.ENTER_FRAME, function(e:Event){trace(this)});

the result looked something like this:

[object global]
[object global]
[object global]

This is a result of the dynamically created function created in the event listener. The function is created in some alternate universe called “global” and has nothing to do with the scope of the stage onto which it is placed. The correct code should be:

import flash.events.*
this.addEventListener(Event.ENTER_FRAME, handleFrame);
function handleFrame(e:Event):void
{
	trace(this);
}

This will produce something like:

[object MyClass]
[object MyClass]
[object MyClass]

Coding for Understandability

Often I have a number of options in how to code a particular piece of functionality. I’m constantly balancing between brevity, simplicity, speed, size, and readability. I often find that if I put a little more focus on simplicity and readability, I save myself time and headaches, in addition to making my code more readable, more object oriented, and more accessible to other coders.

I’m going to provide some examples in ECMAScript, but I know that similar problems exist in other languages, as well.

I recently ran across some code that was coded for brevity:

var userdefined:Object = (_model.partner || {}).userdefined || {},
bsb:BannerShownBeacon = new BannerShownBeacon(
	userdefined.dims 
		? userdefined.dims
		: userdefined.width && userdefined.height
			? userdefined.width + 'x' + userdefined.height
			: 'fixed_bottom'
)
;

Now, this is fully valid, well-written code, and it’s also tabbed and line-broken for readability, but what exactly is it doing? What is the purpose of this code?

The difficulty I find is that when someone is trying to determine whether their particular implementation is going to be executed — lets say we have dims, width, and height in our userdefined param, and we’re looking at this the first time. Since coding standards aren’t set for how to structure ? : style if statements, it’s difficult to go through this by instinct and take a guess at what it’s doing. Every language has if/else statements and most coders are intimately familiar with them.

First, we look to see if _model.partner exists and if it doesn’t, we create it, then we check to see if _model.partner.userdefined exists and if not, we create it and assign that to a variable.
Then, we create a second variable called bsb that expects a BannerShownBeacon;
If userdefined has a dims param, we use it, otherwise, we check if it has width and height params, and if it does, we combine them with an x in between, otherwise, we just use the term “fixed_bottom” and make a BannerShownBeacon out of that.

The final option is to include large amounts of comments (which is excellent for non-compiled languages like JS), but in compiled languages, this often doesn’t save any time or lines of code.

I’ve rewritten this here using if/else statements

var bsbText:String = "fixed_bottom";
if(_model.hasOwnProperty("partner"))
{
	if(_model.partner.hasOwnProperty("userdefined"))
	{
		var userdefined:Object = _model.partner.userdefined;
		if(userdefined.hasOwnProperty("dims"))
		{
			bsbText = userdefined.dims;
		}
		else if(userdefined.hasOwnProperty("width") && userdefined.hasOwnProperty("height"))
		{
			bsbText = userdefined.width + "x" + userdefined.height;
		}
	}
}
var bsb:BannerShownBeacon = new BannerShownBeacon(bsbText);

Why use Math.random? Use a Date instead

I’ve always wondered about this. Cache busting is a common need in my field, and I always see people doing something like the following:

Math.floor( Math.random() * 1000000 );

this will produce something like 809959 and *should* be different every time. If all you’re looking for is a unique number, why not use the Unix timestamp? When it comes to cache busting, if the same user is able to reload the same asset twice in the same millisecond, I say they deserve to get weird results.
That said, it’s not technically random, and it’s possible for your code to produce recurrences in the same millisecond, so use it wisely.
Javascript:

new Date().getTime()

Actionscript:

new Date().time()

PHP:

microtime();

Preloading SWF in Firefox using SWFobject doesn’t support overflow = null

I was using the following code to pre-load my SWFs so I could instantiate them later.

swfDiv.style.width = '1px';
swfDiv.style.height = '1px';
swfDiv.style.overflow = 'hidden';
swfDiv.style.position='fixed';
swfDiv.style.top = '0px';
swfDiv.style.left = '0px';

And then, when I wanted to show my SWF, I would call

swfDiv.width = '100%';
swfDiv.height = '100%';
swfDiv.zIndex = 1000;
swfDiv.overflow = null;

This works great in Chrome, but in Firefox, things go really bad and REALLY strange things start happening. Turns out that Firefox doesn’t like null as an overflow attribute.
Setting this to “” instead works fine.

swfDiv.width = '100%';
swfDiv.height = '100%';
swfDiv.zIndex = 1000;
swfDiv.overflow = "";

Cannot Trace using Debugger on OSX

Oh good lord, all I wanted to do was trace the output of my flash debug player, yet there was nothing in the usual location. Typically I just run

tail -f ~/Library/Preferences/Macromedia/Flash\ Player/Logs/flashlog.txt

in the Terminal and it spits out everything Flash does. The file didn’t exist on my system, so I couldn’t tail it. The solution turned out to be a missing mm.cfg file.
in ~/Library/Application\ Support/, produce a mm.cfg file with the following content:

TraceOutputFileEnable=1
ErrorReportingEnable=1

This, along with a correct debugger, should produce traces.

←Older