PDF::Builder
v3.028 Released, 31 December 2025
Please also see the
CPAN listing, the
GitHub entry, and the latest changes list.
They say the world is a stage. But obviously the play is unrehearsed and everybody is ad-libbing his lines.
— Calvin (Calvin and Hobbes)
Posted on 2017-Apr-21 at 12:47:54 by Phil
A major problem in all languages (compiled or interpreted) is detecting when the programmer has used a variable without putting a value into it. This discussion isn’t about whether a value is valid for the problem set (e.g., Methuselah is stated to be 969 years old, rather than the actual 969 months), but whether the variable’s value has been set at all, or whether it’s just random junk.
Interpreters with loose typing need to carry along a great deal of information about the variable, including its type and value, which need to be checked before it’s used. It’s no big deal to include a little extra information on whether the variable has been set at some point (a value assigned), or whether it’s still undefined. Note that some languages will let you undefine a previously defined variable!
Especially with compiled programs, where the symbol table is no longer available and machine instructions usually directly access the storage for a variable (to load or store it), there is no easy way to tell whether that variable contains random junk, or is a valid set value, just from looking at the bit pattern. The compiler can store a separate flag to indicate the state of the variable (assigned at some point, or unassigned/undefined), but then has to check that flag each time it reads that variable, and remember to set the flag each time it writes to the variable (or, at least once). There are optimization methods to minimize the amount of checking code needed (i.e., check only at the beginning of a basic block, and pass on the information, if it’s been assigned, to subsequent blocks), but the program will still end up doing a considerable amount of its time checking whether a variable is valid.
Except for some limited cases, it is not possible at an arbitrary time for a compiler to tell in general whether a variable has a valid value assigned to it. Loops and conditional expressions can make it very difficult to determine when something has been assigned (i.e., there are multiple paths to get to this point, some of which may have assigned the variable, and some not!). A similar problem can arise with constant folding and propagation, where some optimizations have to be left undone because the compiler can’t be certain of the path taken to a particular point. The famed Halting Problem is for similar reasons.
Does anyone know of other mechanisms in use for preventing the use of invalid unassigned variable data? Do you have any other proposals?
Posted on 2017-Apr-22 at 09:08:17 by sciurius
Posted on 2017-Apr-22 at 19:57:37 by Phil
Well, it’s still undefined/unset, just that it has a consistent value now (0). It still doesn’t solve the problem that you failed to assign a legitimate value at some point, and have garbage in your calculations.
The Intel 8087 numeric coprocesssor and its descendants I think also had some extra bits for declaring NaN, infinities, etc., or possibly it just reserved some bit patterns?
Posted on 2017-Apr-23 at 15:07:49 by sciurius
No, you don’t have garbage. But it takes a short while to get used to
the “default zero" paradigm.
Whether this is good from an
educational point of view is subject to debate.
NaN and friends are defined by IEEE 754.
Posted on Apr 24 08:54:29 2017 by Phil
One man’s trash is another man’s treasure. I still call it [default 0 value] garbage simply because that might not be what you intended when you devised the algorithm and wrote the code. What if the variable were to be used as an index value, starting at 1, and you forgot to give it a value, leaving it at 0? That could be a very subtle error to debug. To rely on the compiler to automatically initialize variables is a very dangerous habit to get into, even if they are to be 0.
The 8087 actually came out before IEEE 754 was finally codified, and mostly (but not fully) implements the standard.
Posted on 2017-Apr-24 at 14:55:54 by sciurius
For this, we have 0-based indices :) .
But seriously, I consider it decent programming to explicitly initialize variables. I like the way Perl uses the OOB value ’undefined’. Several programming languages allow trapping undefined values (by generating exceptions) but often you cannot check whether a variable is (still) undefined.
If you wish to contribute to this discussion in a constructive manner, please email your comment to this discussion group.Include which discussion thread title you are responding to, and the nom de plume you would like to be listed under (we allow only one email address per user name, and vice versa). Due to the large amounts of spam received in the past, as well as abuse of posting privileges (e.g., attacking others), all posts must be received by email and will be individually vetted. If we deem it a useful, factual, and polite post, we will enter it. Anything ugly will result in your being permanently blacklisted. All decisions are made by the management of CTS and are final. We reserve the right to discard submissions without any feedback to you, and to fix errors in spelling and grammar in something we post. Please give a minimum of a few days for us to review and post your entry. Don’t be impatient and resubmit!
All content © copyright 2005 – 2026
by Catskill Technology Services, LLC.
All rights reserved.
Note that Third Party software (whether Open Source or proprietary) on this
site remains under the copyright and license of its owners.
Catskill Technology Services, LLC does not claim copyright over such software.
This page is https://www.catskilltech.com/utils/show.php?link=detecting-unset-variables
Search Quotations database.
Last updated Sun, 17 May 2026 at 8:03 PM