MySQL Insert Only If Missing

I was building a queue of projects with statuses into a mysql table. In my situation, I was expecting to have a project number, and a status. There’s a possibility that there would be many non-unique entries, wherein the same project was published over and over successfully. I wanted to add a new line to the table only if there wasn’t already an existing request for the current project to be built. Most of the examples out there assume you can use unique identifiers, or use two calls to solve this, but I wanted a single mysql call to solve the whole thing.
I found some good stuff on this, but only one answer actually gave me what I was looking for:
If you have a situation where you can’t use unique data:

NOTE: Your database table must not be empty for this to work.

INSERT INTO `table` (`value1`, `value2`) 
SELECT 'stuff for value1', 'stuff for value2' FROM `table` 
WHERE NOT EXISTS (SELECT * FROM `table` 
                  WHERE `value1`='stuff for value1' AND `value2`='stuff for value2') 
LIMIT 1

If you CAN use unique identifiers on your table, then this should work for you.

INSERT IGNORE INTO `table`
SET `value1` = 'stuff for value1',
`value2` = 'stuff for value2';

thanks to: this page

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.

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.

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);

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.

Flash-Injected Javascript does not Minify Properly in Ant

We’re using Flash to inject javascript onto our page, and to save space, we’re using a minifier to produce smaller javascript code. The issue is that we’re using a string replacement on our code and the minifier produces an odd result.

Consider the following code:

var id = myobj['ID_PLACEHOLDER'];

When we inject the code, we do a replace on ID_PLACEHOLDER and replace it with the appropriate ID. This is great, except that our minifier sees this as a waste of chars and minifies the system to:

var id = myobj.ID_PLACEHOLDER;

This would be well and good except that our IDs can begin with numbers and Javascript variables cannot. To solve this, I had to modify our placeholder to be:

var id = myobj['1_ID_PLACEHOLDER'];

This does not minify and produces correct code on the injected end.