Funny Code.

Post Reply
User avatar
bannination
Captain
Posts: 5563
Joined: Sun Sep 16, 2012 7:58 am
Location: Hendersonville
Contact:

Funny Code.

Unread post by bannination »

Just found this gem in some javascript I was working on:

Code: Select all

function returnTrue() 
{
return true;
}

function returnFalse() 
{ 
return false;
}

JTA
Commander
Posts: 3898
Joined: Sat Oct 13, 2012 4:04 pm

Re: Funny Code.

Unread post by JTA »

Javascript man...

You can get in big trouble if you're not careful with Javascript and boolean logic.

Javascript likes to interpret null, undefined, and 0 as false. So if you do something like this:

Code: Select all

var useThisVal,
      x = thisReturnsIntZero(),
      y = thisReturnsIntTen();

useThisVal = parseInt(x || y);
useThisVal will be 10 when you would expect it to be 0! But if thisReturnsZero returns '0' instead of int 0, then useThisVal will be set to zero!

Javascript just doesn't care man. He doesn't follow the rules like the other guys. He's always high as a kite. Got a 0? Got a null? Got an undefined? Javascript doesn't care. They're all false.

Code: Select all

if (0 || null || undefined || false)
{
   //Never executes!
}

if (1 || true) {
   //Always executes!
}
Got a 1? That's a true as far as my man Javascript is concerned.

Does true == 1? Sure thing.

Does false == 0? Damn diggity.

So it must follow that false == null and false == undefined, right javascript, since they evaluate to false as well? No sir. That doesn't make sense.

Also:

Code: Select all

if ('some string') {
   //Always executes
}
But it 'some string' == true evaluates to FALSE!
You aren't doing it wrong if no one knows what you are doing.

User avatar
bannination
Captain
Posts: 5563
Joined: Sun Sep 16, 2012 7:58 am
Location: Hendersonville
Contact:

Re: Funny Code.

Unread post by bannination »

Yeah, I pretty much just all around hate javascript. Writing it is just fine, reading someone else's with no comments or context, forget it. Well, I bang my head on the desk and do it anyway. :lol:

I joked that I rewrote those functions as:

Code: Select all

function truthiness(truthiness)
{
     return truthiness;
}
There could have been very good reasons for those functions. I just couldn't find them in the rest of the code, which was ... probably > 6000 lines. Ugh...

JTA
Commander
Posts: 3898
Joined: Sat Oct 13, 2012 4:04 pm

Re: Funny Code.

Unread post by JTA »

bannination wrote:Yeah, I pretty much just all around hate javascript. Writing it is just fine, reading someone else's with no comments or context, forget it. Well, I bang my head on the desk and do it anyway. :lol:

I joked that I rewrote those functions as:

Code: Select all

function truthiness(truthiness)
{
     return truthiness;
}
There could have been very good reasons for those functions. I just couldn't find them in the rest of the code, which was ... probably > 6000 lines. Ugh...
Yeah it sucks sometimes. It just lets you get away with so much. I've been using Ext for the past couple years and it certainly makes things a lot easier. I don't know if it's a good thing or a bad thing that almost everyone uses some third party javascript framework to make javascript better. Not sure what that says about the language...

Something else you need to be careful with in Javascript is scoping. Unlike Java, PHP, C# etc which allows you to scope variables within if statements, for loops and so forth, javascript "hoists" everything to the function level. That's why it's good practice to declare all variables at the top of your functions. it's not necessary, but it helps keep track of things and prevent things like accidentally declaring variables in the global scope. If you don't declare your variables with "var", then it's automatically global. This can be a big-time problem in a large application and can kill performance if you let globals get out of control. All that junk stays in memory.

Whoever wrote that code you posted must have wanted to be extra sure they were getting a boolean returned to them.
You aren't doing it wrong if no one knows what you are doing.

Post Reply