Unset or empty string - which is better if it ends up showing "no" to the user?

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

The thing I am thinking about is that I fetch something from the database, but find that the value is not set to write, usually determined by the fact that there are no entries or a null value. This will display to the user as "None" or "Not Configured".

So the question is, when passing this value to another part of the script (i.e., another function, more remote by the script template, etc.), do the following:

  • don't set a variable (and therefore check if it's set in the template)
  • set variable to empty string or 0 (and check for empty string in template)
  • set the variable to "No" or "Not set" and just highlight the variable

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

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

I'm looking for a general answer; I know that this will not always be true, but the general rule.

+1


source to share


6 answers


Whenever possible, I've usually used nil in the language to display NULL.

The other options you mention could potentially have the same ambiguity problem that you would have in the database if the value were set to an empty string, None, or Not Set when you really mean NULL.



There is also a risk of this propagation in the database if the user can update the values.

+2


source


The only option I think is what I would not do is the third option. "None" or "Not Set" really looks like a handle to the user interface, and is not quite suitable for setting as a value that will later be interpreted by code.

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



  • NOT_SET
  • NOT
  • YES

If NOT_SET might be the default. In that case, I would still not just select "not installed" for the user.

0


source


Generally, I find it best to handle data structures throughout the entire program. This means that the retained variables are not saved in the database unchanged when I pass them to other parts of the program. I throw or check the boundary conditions when I go if some function expects data in a different format.

0


source


As you said yourself, the "None" or "Not set" value only matters for displaying it to the user. For internal use, the value must be NULL. Human readable readings can only be replaced by functions that map values ​​to the output stream

0


source


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

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

      

(Note three equal signs)

You must explicitly set the value.

0


source


I would recommend installing it on NULL

. If you are fetching a value from the database and there NULL

(SQL), most likely the PHP function to capture the record will return NULL

(PHP) for that field.

You can compare your string with NULL

in PHP as such:

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

      

If you want a specific string to appear instead NULL

(which is "" when cast to a string), you can set it as such:

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

      

Please note that you may need to re-translate your code for internationalization. If you use a custom string throughout your code, it will be much more difficult to do.

Just not setting a variable is bad because you might misspell the variable name and wonder why, when you set it, "None" is still displayed.

0


source







All Articles