Jump to content

Singleton variable

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Stewartadcock (talk | contribs) at 03:40, 3 May 2004 (Ported from FOLDOC). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

A singleton variable, in computer programming, is variable that is only referred to only once in a piece of code, probably because of a programming mistake. To be useful, a variable must be set and read from, in that order. If it is only referred to once then it cannot be both set and read.

There are various exceptions. C-like assignment operators, e.g. "x += y", read and set x and return its new value (they are abbreviations for "x = x+y", etc). A function argument may be passed only for the sake of uniformity or to support future enhancements. A good compiler or a syntax checker like lint should report singleton variables but also allow specific instances to be marked as deliberate by the programmer.

This article is based on material taken from the Free On-line Dictionary of Computing prior to 1 November 2008 and incorporated under the "relicensing" terms of the GFDL, version 1.3 or later.