Style/Structuring question: One vs. multiple global objects

Derek Parnell derek at psych.ward
Sun Jul 1 14:22:29 PDT 2007


On Sun, 01 Jul 2007 13:07:49 -0700, Kirk McDonald wrote:

> I am much less hesitant 
> to use globals in D than I am in Python.

I'm old-school in that my first programming langugage was COBOL. We were
taught that globals were to be avoided because it added to the maintenance
effort - and I still agree with that. 

The issue is that if you change a global symbol (either at coding time or
at run time), you have to take extra effort at finding out what is affected
by that change. By keeping symbols more locally, it is easier to find the
dependancies.

However, sometimes some things need to be global as they are used in many
other modules. For example if your application supports a "-verbose"
command line switch. The choice is then whether to expose the variable or
an interface to the variable. In other words ...

    if (Global_IsVerbose) ...

or

    if (Global_IsVerbose() ) ...

In either case, I'd recommend naming the entity in such a manner that
highlights its 'globalness' effect.

-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell


More information about the Digitalmars-d-learn mailing list