Home > JavaScript, Object Pascal, OP4JS, Smart Mobile Studio > Smart Mobile Studio: Null is not unassigned, and unassigned is NaN

Smart Mobile Studio: Null is not unassigned, and unassigned is NaN

February 5, 2015 Leave a comment Go to comments

When I started the Smart Mobile Studio project I had medium knowledge of Javascript, meaning that I had not used it for many years. To me JavaScript was a toy, used for scrolltexts and popup’s. Smart Mobile Studio was born out of a full investigation into many different languages, and it was then I noticed that JavaScript had come of age and could finally support a full object-model and Delphi like OOP RTL.

But still, after all this time there are aspects of JavaScript that just bugs me. Not because they are stupid, but because something simple should – in effect – remain simple. Why take something simple, like datatypes, and introduce a third state?

Unassigned is not null

We all know Boolean values. They can either be TRUE or FALSE. But experienced programmers will also know, that there are cases in programming where a result is neither true nor false. For instance, if you have a function called “CheckDBConnection” that validates that you are indeed connected to your database server — but suddenly a system exception occurs somewhere in the middle, before you can determine if the connection is valid or invalid, what value should you return?

In such a scenario it’s best to default the whole function to false, in fact I would start by setting the result to false and only report true after everything checks out. This is called pessimistic discrimination. Positive discrimination is when you write your code with the world-view that everything will be ok. You ignore stuff like faulty disks, memory leaks and other acts of God (or nature, or darwin or whatever you subscribe to) and just focus on the good stuff.

But if we are to be truthful and have a datatype meant to reflect real-life-states, then boolean would either have to be altered with a third state ( TRUE, FALSE and UNKNOWN) or we would need a whole new datatype.

Unassigned

Under JavaScript all variables, be they intrinsic or not, can have a third state, namely the unassigned state. This also means that NULL, which we like to believe is the same as NIL in pointer based languages, actually is a value. It’s not the same as zero which is a number by nature. Null means “I have no value, but at least we are talking about values”, while Unassigned means “I dont even know what a value is, let alone what I am supposed to contain”.

  • Null is NIL to a language which supports VOID
  • Void is null to a language without NIL
  • Unassigned in void to a language which supports null

Annoyed yet? This means that testing even the most simple stuff in JavaScript requires a couple of extra steps to deliver the goods. For instance, in the QTX helper code you will find these snippets:

function TQTXVarHelper.IsObject:Boolean;
begin
  asm
    @result = ((@self) !== undefined)
      && (typeof @self !== null)
      && (typeof @self  === "object")
      && ((@self).length === undefined);
  end;
end;

function TQTXVarHelper.IsArray:boolean;
begin
  asm
    @result = ((@self) !== undefined)
      && (typeof @self !== null)
      && (typeof @self === "object")
      && ((@self).length !== undefined);
  end;
end;

function TQTXVarHelper.IsUnassigned:Boolean;
begin
  asm
    @result = (typeof @self === undefined);
  end;
end;

Notice that the check code to see if a JS variable is an object ( a = { } ) or if it’s an array ( a = [] ) are identical. Why? Because they both register as an object. The only way to tell them apart is by checking if a length() function exists which only arrays have.

Also notice that undefined is checked before NULL, because if an array is undefined, JS will throw an exception when comparing it to NULL, which is a value (!)

It’s really best to code JavaScript before you are fully awake. I code my best stuff between my first cup of coffee and having my morning shower..

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: