Undefined vs. “Undefined”

Filed under:Javascript — posted by Nic "RedWord" Smith on April 24, 02007 @ 10:19 PM

There is no built-in equivalent of PHP’s isset() function in Javascript. Searching the internet for ways to work around this, you’ll see a lot of people recommend testing the (possibly undeclared) against undefined. In Venkman/Firefox, there is some unusual behavior here, which I haven’t seen mentioned elsewhere.

First of all, the recommended code:

var foo = 1;
if (foo === undefined)
{alert(”No foo”);}
else
{alert(”foo”);}

This will pop-up a window that says “Foo.”

Compare this to the following:

if (bar === undefined)
{alert(”No bar”);}
else
{alert(”bar”);}

This code will generate an error (”bar is not defined”) in Firebug, and presumably in the default debugger as well. Adding a single line fixes this:

var bar;
if (bar === undefined)
{alert(”No bar”);}
else
{alert(”bar”);}

This pops up a dialog that says, simply, “No bar.”

Rather than worry about an explicit declaration of a variable, you may try some code that uses typeof() to test if a variable exists or not. But, there’s a pitfall that I ran into — the following code is not correct.

var bar;
if (typeof(bar) === undefined)
{alert(”No bar”);}
else
{alert(”bar”);}

This code will actually output “bar”! It turns out that typeof returns the string “undefined” rather than the value undefined. So, in order to perform a test that should work right “no matter what” the code’s that actaually needed is:

if (typeof(bar) === “undefined”)
{alert(”No bar”);}
else
{alert(”bar”);}

This will output “No bar” as expected, with or without a preceding “var bar;”

When the Reset Button makes Sense

Filed under:Computing, design (visual style) — posted by Nic "RedWord" Smith on April 16, 02007 @ 10:46 PM

A while ago on Coding Horror, there was a discussion about the HTML reset button (I think…). The short version is that the reset button can only destroy a great deal of work that’s been done to enter information into a form, and that in most cases it should be avoided. I would argue that this is an important exception to this rule – if the form contains a large number number of fields for which the default value is special, including a reset button is vital.

I was reminded of this while using PhDs.org’s ranking program. There’s an option to free-fill in several options with values that fits a pre-made “profile.” And if you don’t like the profile and want to create your own, disregarding some of the options? Rather than clear them out one by one, there’s a convenient “clear all” link at the top of the page. In the same way, FanCruft’s advanced quiz allows you to reset everything back to the default value of 5, so that, if you’ve decided that you’ve messed up, you don’t have to reload the page or manually click 5 for every option.

Where do things stand now for the 2008 election?

Filed under:2008 election, prediction markets — posted by Nic "RedWord" Smith on April 6, 02007 @ 6:14 PM

There’s been a lot of buzz lately regarding Obama’s campaign. Comparisons with Hillary Clinton’s fundraising having become particularly in vogue. Here’s where things stand on the Foresight Exchange right now (note that I have substantial NO holdings in Gore08 and recently pushed the price down from 0.05 to its current level of 0.02)

Rudy08 0.15  Rudy Giuliani President 2008
Rice08  0.06  Condi Rice President 2008
TomT08  0.01  Tom Tancredo US Pres by '09
JMcn08 0.16  John McCain President 2008
Alln08  0.01  George Allen President 2008
RepP08 0.40  republican elected pres in '08


HRC08  0.25  Hillary Clinton US Pres by2009
gore08  0.02  Al Gore President 2008
Edw08   0.09  John Edwards

For some reason, there doesn’t currently seem to be a claim about Obama. This make analysis more difficult, but the numbers here indicate that he has no greater than a 24% chance of being elected (this is the chance of any Democratic candidate not listed above being elected). For comparison, there seems to be a 6% chance of a Republican candidate not listed being elected.

Serialize functions vs. JSON functions

Filed under:PHP — posted by Nic "RedWord" Smith on April 5, 02007 @ 6:01 PM

I’ve been working with the JSON functions in PHP. These functions were incorporated into PHP 5.2, although methods of working with JSON have been awhile for PHP. Given that PHP’s serialize and unserialize and json_encode and json_decode are interchangable with one another in many circumstances, I began to wonder which set of functions would be faster. Here’s the wall time of all four functions stringifying 10000 arrays, averaged over 10 runs:


json_encode json_decode serialize unserialize
average 6.60 1.70 6.60 1.20

Basically, in terms of speed, there is little difference. In the test above, json_encode() and serialize() have the same speed. A chart of an earlier test (attached) shows serialize() to be slightly slower, but in any case, the difference in speed has little practical significance.

So, what are the differences between JSON stringification and PHP serialization that are worth considering? JSON is widely supported by many different languages, while PHP’s typed serialize format works best for PHP. It’s possible to work with PHP’s serialization format in Javascript (see PHPGuru.org for an example I found with a quick Google search), but it’s nowhere near as easy as JSON, which can be either eval’ed, or, more appropriately, jsondecoded.

Using PHP serializations allows PHP objects to be serialized, including the use of magic methods like __wakeup(). The comments on this activestate page note that this is not always a good thing, and can lead to the execution of arbitrary PHP by an attacker if you’re not careful.

FWIW (not too much, I’ll admit), I personally find JSON to be more eloquent and cleaner looking, and I’ll be using it in the future.

JSON vs. Serialize

Here’s the code I used for the comparisons:

set_time_limit(0);

for ($j = 0; $j < 10; $j++) {
$json_strings = array();
$phps_strings = array();

$start = time();
for ($i = 0; $i < 100000;$i++) {
$foo = array();
$foo['bar'] = array(1,2,3,4,5,6,7,8,9,0);
$foo['bartwo'] = mt_rand(0,10000);
$foo['barnone'] = sha1(uniqid());
$foo['barall'] = md5(uniqid());
$json_strings[] = json_encode($foo);
}
$runtime = time() - $start;
echo "Wall time for json_encode: " . $runtime . "\n";

$start = time();
foreach ($json_strings as $json) {
$var = json_decode($json);
}
$runtime = time() - $start;
echo "Wall time for json_decode: " . $runtime . "\n";

$start = time();
for ($i = 0; $i < 100000;$i++) {
$foo = array();
$foo['bar'] = array(1,2,3,4,5,6,7,8,9,0);
$foo['bartwo'] = mt_rand(0,10000);
$foo['barnone'] = sha1(uniqid());
$foo['barall'] = md5(uniqid());
$phps_strings[] = serialize($foo);
}
$runtime = time() - $start;
echo "Wall time for serialize: " . $runtime . "\n";

$start = time();
foreach ($phps_strings as $phps) {
$var = unserialize($phps);
}
$runtime = time() - $start;
echo "Wall time for unserialize: " . $runtime . "\n
";
}

?>

That would have turned out badly for me if it were possible.

Filed under:Silly — posted by Nic "RedWord" Smith on April 1, 02007 @ 9:38 PM

A conversation, edited for space, between Silence and myself during a game of chess online. He had just told me that the software prevented him from making an illegal move:

RedWordSmith: Don’t tell me you tried to move your queen to e6!?
Silence: no
RedWordSmith: Good. That would have been weird.
Silence: I tried to move it to e8 :p



image: detail of installation by Bronwyn Lace