comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Global Varibles!!
Date: 1997/02/20
Date: 1997-02-20T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680002002970056270001@news.ni.net> (raw)
In-Reply-To: 330B6914.22CD@hotmail.com


In article <330B6914.22CD@hotmail.com>, rp71@hotmail.com wrote:

I've jsut written a procedure which sets a value to a varible, and I
>can't seen to keep the value for use in another procedure.  I know it
>has to be global but I don't know how to set a variable as global in
>ada95.
>
>Can anyone please help me by telling me how to set a varible as global.

I will tell you want to do, but with a warning: you must be careful with
global variables.

If you wish to save the state of a variable across subprogram invokations,
then the variable must be declared in a static scope, which means in a
package spec or body.  For example,

package Global is

   X : Integer;

end;

with Global;
procedure Set_X is
begin
   Global.X := 1;
end;


with Global;
procedure Get_X is
begin
   ... := Global.X;
end;


However, using a package to declare global variables shared by subprograms
is a very old style of software architecture.  You see it when Fortran
programmers first program in Ada: they have a global mindset.

Realize that this creates danderous coupling amoung pieces of the system. 
If you change Global.X, then how do you know what will break because of the
change?

Better is to encapsulate the data, and expose its representation to only a
few subprograms.  This is better known as object-oriented programming: the
data is described not in terms of its representation, but rather in terms
of the things you can do to it.

For example, 

   The_Stack : Stack;
begin
   Push (5, On => The_Stack);
...
   The_Value := Top (The_Stack);
   Pop (The_Stack);

The representation of the stack is hidden, and clients call primitive
operations to manipulate stack instances.

Your original problem was needing a static scope for global variables. 
While it's true we don't generally use global data anymore, we still have
global entities in the guise of "well-known" objects.  These are objects
that are known across subsystem boundaries.

We want to minimize coupling across subsystem boundaries, but of course we
can't completely eliminate it.  Subsystems have to have some minimum amount
of coupling; otherwise, they'd wouldn't be part of the same system.

For more information about this important topic, see visit the Singleton
pattern, at the nascent SIGAda Patterns Working Group home page.

<http://www.acm.org/sigada/wg/patterns/>
<http://www.acm.org/sigada/wg/patterns/ada_singleton.html>

-matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




  reply	other threads:[~1997-02-20  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-02-19  0:00 Global Varibles!! Richard Pearce
1997-02-20  0:00 ` Matthew Heaney [this message]
1997-02-21  0:00   ` Norman H. Cohen
1997-02-23  0:00     ` Robert Dewar
1997-02-25  0:00       ` Norman H. Cohen
1997-02-25  0:00         ` Robert Dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox