WTF comes from trying to plan for worse case scenarios.
I did not write this. My doppleganger did :)
var WorstCaseSenario:*=" 0x00 ";
trace(toBoolean(WorstCaseSenario) == false);
function toBoolean(bool:*):Boolean
{
if (bool == "" || !bool) {
return false;
}
if (bool is Boolean == false) {
bool=String(bool);
}
if (bool is String) {
bool=String(bool).toLowerCase().replace(/^\s+|\s+$/mig,"");
if (/\d+|\dx\d+/.test(bool)) {
if (Number(bool) == 1) {
return true;
}
if (Number(bool) == 0) {
return false;
}
}
if (bool == "true") {
return true;
}
if (bool == "false") {
return false;
}
return false;
}
return bool;
}
-- Keith H --
www.keith-hair.net
Juan Pablo Califano wrote:
This remainds me of a utility method written by a former coworker at my
previous job, that rightfully belongs in the daily WTF.
public static function mtd_validarVar (variable : Object) :Boolean
{
if (
variable == false ||
variable == undefined ||
variable == ""||
variable == null
)
{
return false
}else
{
return true
}
}
Had he checked against 0, at least this method could have been used as a
generic replacement for the obscure and unreadable implicit boolean
coercion...
He also had the chutzpah to put this method in our core library, in a class
conveniently named VarUtils, so the whole team could benefit from it.
To add insult to injury, he had a very personal take on hungarian notation
and code formatting as well. Not to mention he was also in the "tabs should
be 8 spaces here, there and everywhere, and if you and the rest of the world
use 4 spaces, rest asured I will reformat any such piece of crap as soon as
I get a chance to put my hands on it" camp.
Nevertheless, he was a cool guy.
Cheers
Juan Pablo Califano
2009/8/18 Latcho
some great optimalistations can be found here:
http://mindprod.com/jgloss/unmain.html
for ex.
class Truth
{
boolean isTrue ( boolean assertion )
{
if ( assertion != false )
{
return assertion;
}
else
{
return assertion;
}
}
}
...
var doIt:Boolean;
var trutherizer:Truth = new Truth();
if ( trutherizer.isTrue( s.equals( t ) ) )
{
doIt = true;
}
else
{
doIt = false;
}
// hint: all the above accomplishes is:
doIt = (s==t);
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders