4

I came across this interesting function in a piece of ASP Classic that I maintain. At first I laughed, shook my head, then cried (only a little). But then I started to wonder if there is any legitimate reason why 999999999999999 would apparently be considered NULL since VBScript has its quirks. As mentioned in comments, the values passed to this function are returned from the COM dll.

Can anyone confirm if there is some legitimate reason for this or is it ripe for submission to TheDailyWTF.

function NullNumberCheck(Value)
    if IsNumeric(Value) then
        if Value = 999999999999999 then
            Value = ""
        end if
    end if
   NullNumberCheck = Value
end function
Tetsujin no Oni
  • 7,300
  • 2
  • 29
  • 46
jmmr
  • 498
  • 7
  • 17
  • Just to clarify, I understand how to check for null, nothing, empty. I just want to know what the original author was thinking. – jmmr Jan 16 '12 at 18:27

3 Answers3

2

That looks like a case of "magic null" in the source of the data - is there a column in the database that backs the values being passed to this function which is not nullable?

Why are people using magic values instead of null in their code?

Community
  • 1
  • 1
Tetsujin no Oni
  • 7,300
  • 2
  • 29
  • 46
  • I did try to find this value in the DB to no avail. It is calling an API written with what I assume are C conventions, possibly it is related. – jmmr Jan 19 '12 at 17:05
  • The API is probably using this as magic null, because if the return type is int, the api has no way to indicate 'null' other than a magic value outside the normal domain of its return type... This is even one of the LEGITIMATE ways you wind up using magic null. – Tetsujin no Oni Jan 19 '12 at 17:12
1

The author is using this as a method of clearing or unsetting the variable. By setting the value of Value to an empty string, it clears any previous value. The author could have just as easily set it equal to 0, vbNull, or vbEmpty. It really depends on what you are doing with the value later in your script. If you go on to perform further checks on the variable, setting it to vbNull may not be advisable and setting it equal to vbEmpty may crash your script if you are using Option Explicit.

So to answer your question, no this is not a valid way to check for a "null" value, because it's not a comparison operation at all, it's performing a variable assignment.

Nilpo
  • 4,675
  • 1
  • 25
  • 39
0

To check for a null value you can use the IsNull function built into VBscript

http://www.w3schools.com/vbscript/func_isnull.asp

Dee
  • 1,432
  • 1
  • 9
  • 8
  • Thanks Dee, but this does not answer the question. – jmmr Jan 16 '12 at 18:37
  • The question was "Is this a valid way to check for a null number in Classic ASP/VBScript?" There are always work-arounds to achieve something so I posted the recommended way. – Dee Jan 16 '12 at 19:14
  • I've updated the question in attempt to clarify that it is on this specific function, not how to do it in ASP/VBScript – jmmr Jan 16 '12 at 19:57
  • 1
    That silly little function doesn't check for a null. It checks for a numeric value then checks to see if it a specific numeric "flag" if true then resets to an empty string. No way of knowing what the purpose of the code is about. Why it is named nullnumbercheck makes no sense. – Dee Jan 16 '12 at 20:21
  • See my answer below - this appears to be a case of using a 'magic number' to represent the null value. – Tetsujin no Oni Jan 17 '12 at 20:13