Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In section 6.4 of Code Complete 2nd Edition there is a paragraph about hiding global data. What I am particularly interested in, is that McConnell (the author of the book) gives examples of the benefits of hiding global data. There is one example that I cannot understand. I don't have the English version of the book, so I'll try to translate the text.

Hiding global data. (...) You can change the structure of the data without modifying the program.

What does McConnell mean by that? Is he talking about changing global data? If so, why wouldn't you have to modify your program when you are using methods to retreive that data? Or maybe he is referring to something else here?

I would greatly appreciate if someone could clear up my confusion. If you could also provide an example, it would be great (examples are awesome, you know).

share|improve this question

1 Answer 1

up vote 45 down vote accepted

The author is talking about the structure of the global data, and how changing that structure would impact the code that uses it.

If the global data is accessible directly, then changing the data structure implies potentially having to change all the code that uses it.

If the global data is only accessible via an interface (e.g. a set of functions), then changing the structure implies only changing these accessors. The rest of the code doesn't have to change.

As simple example would be code that starts out with an array of integers (a static int[] in some Java class for instance). If that array is globally accessible, then people will start using it with array syntax (i.e. Global.cool_stuff[x] = 1;). If for some reason you want to change that array to a collection type (vector, list, whatever), then you have to change all the code that uses this piece of global data since it has become, at least syntactically, invalid.

If that data had only been accessible via a set of accessor/mutator functions ("hidden behind" an interface), then you would only need to change that small set of functions. The code that actually uses the data can remain unchanged.

share|improve this answer
    
Thank you! This is exactly what I wanted to know. –  Kapol May 1 at 10:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.