On Fri, 17 Dec 2004 13:39:30 UTC, Michael <mi****@gmx.net> wrote:
Hi newsgroup,
as the subject indicates I am looking for an advice
using global variables. I am not if this problem is more
about style then C. If its wrong in thi group, sorry.
I'm now programming for more than 20 years not only in C: There are
something that speaks against od using global variable:
- globals are visible outside its intent.
- globals can be changed anywhere in the module
or if defined as extern in the whole program
- it makes the maintenance not even easy
- it makes it sometimes quite impossible to conrol acces
- it make the whole program non-transparent
But there are some arguments speaking for globals:
- saves tracking parameters through hierarchies of function calls
- it can save lots of runtime because mostenly calling
parameterless functions are done more quickly
In practice you would design your program carefully and define
variabels
- used in each and any corner of the application globally on
a central point. This is on one and only one source module.
This would be the module hosting main() or a data only one.
- as you would design your program modular, meaning that
objects (in thinking objectoriented) have each theyr own
source module or a hierarchy of source modules you would place
them in the top level - if they have to be accessible
to all modules or as static in the only module you needs them.
- you would even be thrifty in creating them.
But you would try in any way to define variables inside functions
whenever possible and give them as parameters to more global designed
funtions.
It makes no sense to give variables to functions only to give them to
functions that gives them to functions that gives them to functions to
been used here and only here whereas they are never used in higher
functions other than to pass them to functions.
So build a clean leveled structure:
- the module hosting main()
- define all variables used really over the whole application
declare them in a header file that will be included in
any translation untit that needs to know of them
- define all variables inside the module but shared by
multiple functions located inside the same file.
You saves the work to think about all and any variable that
is needed in all/many objects.
- modules that hosts an object, even as you would have multiple
translation units to handle the whole object.
This module will hold all the external interfaces to
other sections of the program, but no lower level function.
- defined as extern anything that is used by more than one
translation object
- define as static in file level all variables need by
multiple functions inside the same translation object
- define as static on function level variables used only
inside a specific function.
- build a header file containing all the declarations
needed to know by more than one translation module but
not by the whole program
- modules hosting the specific function group of an object
- same as above.
- lover levels: same as above.
Encapsulating variables as possible helps you to code errorfree.
Encapsulation functions in the same technics helps you too.
It is essential to place variables on the right place. Sometimes it
can help to write simple access methods to get the content of a
variable you have no reguar access to - but needs it value.
As C is not a object oriented language you have no service from the
language to write object oriented code - but there is nothing that
hinders you to thing the object oriented way and structure your code
and variables in that way.
Global variables helps you to get your work done without overloading
any function with more parameters as you absolutely needs to supply to
them to profit from theyr work. That means you would not have to think
about the parameters a function needs that gets called from the
fuction that gets called from the function ... you calls in the
function you needs a piece of work done.
You would plan actual parameters that changes its values on each call
but are unchanged for longer as the current run through the calling
hierarchy.
True, it is hard to find the right location for a variable that gets
used on different places - but it is worth to make this design right
even bevore the code gets written.
The same as above is guilty for functions themselfs. Make them visible
to outside a translation unit only if and only if direct access to
them is really needed from outside.
The right structure of header files helps you handle the visibility
outside of a single translation unit too.
Let a header file #include all headers it needs to bring any data and
function declaration into visibility it needs - but only that.
Plan the interfaces (global data and function declarations) carefully
and test the plan more carefully before you tries to write the first
real statement! Yes this job is quite more hard than to hack
statements into the keyboard - but you will be repayed by significat
shorter coding and testing phases.
You should write a book containing the whole design, the whole
interfaces to document the application. Sure, you would not publish
that book but it will document each detail of all the jobs the
application has to do. It will document the whole structure of your
application, each detail what it has to do, each detail how it is done
- without any programming statement.
You will start up with a rought description. That description will
fall into more and more details giving you at least a fine plan of
- variables needed
- locations of the variables
- startvalues needed
- prototypes of functions
- locations of the fuctions
You'll find helper functions to save multiple writing of the same
function or similar ones. Writing that all in plain english is more
easy and better understundable than coding in a programming language.
Check that all and recheck following the logical pathes an action
would run through to be sure that each action would do exactly what
you want that it has to do. Repair errors at that point. Review it
again. That means check if any variable you needs is visible, that
they have the right value at the right point, that each prototype
fullyfies its needs.
Anything that you does until here will save you month of debugging.
Lay down that book beside your keyboard and start coding the header
files. Go through the headers, checking tat any action will follow its
pathes they have to do. Start up and write the lowest level
translation module. Write a debug main() and debug this module
carefully with any possible lowest and highest values in the variables
the functions can get, do the same with invalid values. As all headers
are complete you can #include them. You can prepare the debug main()
to setup all data and debug anything.
You can write the debug main() in an own module to have it out of the
source tree but you may even encapsulate it with the preprocessor so
that it will not get transated when you compiles it for later binding
into your program.
When it is working perfectly then write the next module on that level
.....
The last you would do is to write the real main. Anything lower than
that is tested, so you have only to test the main itself and follow
down into lower level modules only when the debugging of main shows an
unwanted behavior that is not clearly inside the main module.
--
Tschau/Bye
Herbert
Visit
http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!