In 99.9% of cases (probably much higher), null values are equal to empty values when programming. If you've developed a solution that treats null values different from empty values, you've created a nightmare of confusion and a nearly insurmountable learning curve for the developer that has to clean up behind you. Maybe that's what you were after. If not, you could use some helpful tips to clean up your code so that "no value" coming from either a database or a user-input string will be treated identically w/in your code w/o each line having to visibly account for both conditions.
I detest checking both conditions in my code. As a core tenet of programming, I create reusable functions that enable me to abstract myself from any unnecessary code like this. I've seen countless examples of the following lines in code:
IF (some_var = "" or isnull(some_var)) THEN
...
END IF
Ughh! It hurt my fingers just typing that crap and it's difficult for me to look at on screen so I am shielding my eyes carefully while I wrap it in <pre> tags. If you have coded a line like this post1circa 1998, you should stop now that you know a better way or just leave programming forever and stop dumbing down our ranks.
The following two functions need no explanation as they are otherwise straight-forward for anyone w/ an intelligence level higher than a primate.
FUNCTION void(byval arg1)
IF (arg1 = "" OR isnull(arg1)) THEN void = true ELSE void = false
END FUNCTION
FUNCTION nvl(byval arg1, byval arg2)
IF (void(arg1)) THEN nvl = arg2 ELSE nvl = arg1
END FUNCTION
If you are even slightly confused by the code in the functions above, I've either introduced you for the first time to single-line conditional statements or you are an idiot. If the latter, click on one of the Google Ads (one near the top please!), leave, and don't come back. Do not leave a comment, do not pass go.
I only introduce you to the void() function in this post so I have an opportunity to introduce you to one of my favorite functions: nvl(). If you're at all familiar w/ the Oracle database, you will recognize this function name and its operation for what it is: an exact replica.
Why is nvl() useful? Consider the following code that you may have written in the past:
IF (NOT void(some_var)) THEN
response.write some_var
ELSE
response.write "There are no records available."
END IF
What a mess! Consider the following re-write:
response.write nvl(some_var, "There are no records available.")
Cool, eh?
This function is also useful for embedding default values directly when values are assigned, as in the following example:
...
IF (NOT rs.EOF) THEN
listing_mileage = nvl(rs(0), "0")
listing_make = nvl(rs(1), "Unknown")
listing_notes = nvl(rs(2), "Notes not available")
listing_photo = nvl(rs(3), "/Photos/No-Image/jpg")END IF
...
If you weren't single-line coding this type of value defaulting, then you've been creating unnecessarily long code that will prove heavy to write, tedious to document, and difficult to troubleshoot. I have many more concrete and very practical examples of code-shortening functions which will combine typical programming constructs into more concise, human-readable code. Stick around - you will learn alot.
In the hallowed voice of Colin Cowherd, if you don't agree w/ my opinions let me know. I reserve the right to be wrong but you should know what you're talking about. I wrote this article start to finish in 7 minutes, including the code examples. If youwish to trash-talk this post, go ahead but trust that I will not let your comments go unanswered and you will most likely be humiliated.

Comments