Topic: restricted use of global variables

topics > computer science > data > Group: type declaration

expression language
global declarations and variables
interface between program modules
information hiding
local vs. global
object-oriented data types
Petri net


Within procedures, globally declared objects act as implicit parameters. Several researches have seen global definition as uncontrolled programming prone to errors. The solution in CLU is removing global definition altogether and replacing it by locally declared access paths. Some other languages use an import list explicitly declaring use of global variables. Smalltalk programs use globals as initialized constants and for system resources. (cbb 5/80)

In object-oriented programming, disallow globals since they prevent multiple users of the same code. This is particularly true of templates which may be used in a wide variety of contexts. Multiple inheritance helps keep top-level classes free of globally defined data. Use a 'cursor' data structure to represent the current location in a sequence or list. (cbb 2/93 4/98)

Subtopic: inheritance and shared information up

Quote: functional decomposition is insufficient for data abstraction because interesting data needs to be global; like single inheritance [»stroB_1991]
Quote: with single inheritance, shared information tends to move towards the root of the tree; root classes become a global name space and a muddle [»stroB_1991]

Subtopic: global coupling up

Quote: the global coupling of 17 kernel modules grows exponentially with version number; lines of code grows linearly with version number; leads to maintenance issues [»schaSR2_2002]

Subtopic: avoid global data up

Quote: prefer object-orientation implementation for shared systems; encapsulates state in a class; avoids globals [»kampPH7_2004]
Quote: avoid global data; if code references global data, it is tied to all users of that data [»huntA_2000]
QuoteRef: zelkMV1_1974 ;;57 wulf, shaw 73: global variables tend to destroy structure of a program
Quote: most global Smalltalk variables are used for system resources; their use is otherwise discouraged [»xlrg8_1981]
Quote: except for low-level types or math entities, avoid global data, global functions, public data, friends, direct access, type fields, inline functions, void*, pointer arithmetic, C arrays, and casts [»stroB_1991]
Note: global state drags around the whole system; can't be used in other contexts [»cbb_2000, OK]

Subtopic: disallow globals up

Quote: ESP disallows global variables
QuoteRef: liskBH2_1976 ;;4 CLU has no global variables
Quote: no global objects in Ocra, instead objects are passed as parameters by procedure calls or process forks [»taneAS8_1992]

Subtopic: globals as implicit parameters up

QuoteRef: richF12_1977 ;;79 "The procedure header, however, should explicitly mention all global variables that are referenced or updated

Subtopic: avoid globals for templates up

Quote: templates should not depend on global information; they will be used for unknown types in unknown contexts [»stroB_1991]

Subtopic: objects instead of globals up

Quote: instead of global data, use objects to pass context; typically as parameters to the object's constructors

Subtopic: avoid class variables -- state up

Quote: since concepts are timeless, a class, a model of a concept, should not have a current state [»madsOL10_1986]

Subtopic: class variables as initialized constants up

Quote: Smalltalk class variables are usually used as initialized constants [»xlrg8_1981]

Subtopic: examples of global state up

Quote: Icon uses two globals &subject and &pos heavily, causes trouble but is also convenient [»grisRE4_1980]
Quote: global named constants are "wired in"; prevents multiple calls to a refinement [»redeDH7_1979]
Quote: can replace inheritance of global named constants with parameters and results [»redeDH7_1979]
Quote: ban side effects by disallowing global named constants; allows expression refinements to be used more than once in a scope

Subtopic: user studies up

Quote: programmers who used more local variables than average had 63% fewer errors

Related Topics up

Topic: expression language (14 items)
Topic: global declarations and variables (33 items)
Topic: interface between program modules (55 items)
Topic: information hiding (50 items)
Topic: local vs. global (29 items)
Topic: object-oriented data types (29 items)
Topic: Petri net
(44 items)

Updated barberCB 1/05
Copyright © 2002-2008 by C. Bradford Barber. All rights reserved.
Thesa is a trademark of C. Bradford Barber.