tags:

views:

292

answers:

7

I am wondering if you use unset variables, empty strings (or 0's), or "None" to determine if a variable is "None"?

The case I'm thinking of is, I'm retrieving something from the database, but find that the value is not set for the record, usually determined by the fact that there are no records or a null value. This will display to the user as "None" or "Not Set".

So the question is, when passing this value to another part of the script (ie, another function, farter on the script, template, etc), do I:

  • not set the variable (and therefore check if it's set in the template)
  • set the variable to an empty string or 0 (and check for the empty string in the template)
  • set the variable to "None" or "Not Set" and just echo the variable

Is there one that you usually do and why do you do it?

(I'm using PHP, so the type of the variable is somewhat unimportant.)

I'm looking for a general answer; I know that it won't always be true, but a general rule to follow.

A: 

Depends on your application and what the variable is. Usually I use "None" in my programs.

Ben Alpert
+2  A: 

Where possible I would normally use the nil value in the language to map to NULL.

The other options you mention all have the potential for the same ambiguity problem you'd have in the database if the value were set to empty string, None, or Not Set, when you really mean NULL.

There is also the risk of this propagating back to the database if the user can update values.

frankodwyer
A: 

The only option that I think is something I would not do is the third option. "None" or "Not Set" really seems like a UI descriptor and not really appropriate to set as the value to be interpreted by code later.

The only exception to this would be if you have a set of known values. If they are more or less constants.

  • NOT_SET
  • NO
  • YES

Where NOT_SET might be the default. In this case I would still not simply echo out "not set" to the user, though.

Beau Simensen
A: 

Generally, I find it's best to handle data structures consistently throughout the program. This means leaving variables that are unset in the database unchanged when I pass them to other parts of the program. I cast or check for boundary conditions as I go if a particular function is expecting the data in a differing format.

Max
A: 

As you said yourself, the value "None" or "Not set" is only relevant as far as displaying it to the user. For all internal use the value should be the native NULL value. The human readable values should only be substituted by whichever functions render values into the output stream

Gareth
A: 

In PHP, you can set the variable to FALSE or NULL and then say:

if($var === FALSE)
    ...

(Notice the three equal signs)

You should explicitly set the value.

Bill Zeller
A: 

I would recommend setting it to NULL. If you're fetching the value from a database, and it's NULL (SQL) there, most likely the PHP function to grab the record will return NULL (PHP) for that field.

You can compare your string with NULL in PHP as such:

if($myString === NULL) {
    /* It's NULL! */
}

If you want a certain string to show instead of NULL (which, when casted to a string, equals ""), you can set it as such:

if($myString === NULL) {
    $myString = 'None';
}

Take into consideration you may need to refactor your code for internationalization later on. If you use a user string throughout your code, it will be much harder to do this.

Simply not setting a variable is bad, because you could make a typo of the variable name and wonder why when you set it "None" still shows.

strager