The Psychology of Quality and More |
CHAPTER 9 : Data Usage
9.4 Using global dataWhen data is to be shared between a number of functions, and has no owner, it is common to define a set of global data which is referenced by all relevant functions. Global data, however is generally recognized as a bad thing, particularly if it appears in large amounts and with little control. It is difficult to debug and maintain, as it is usually far from clear who does what to which, when and where. It can also be hazardous in multi-threaded programs. There are some alternatives to using global data: 9.4.1 Pass the pointerThe first alternative to global data is to have an owner function for the data, which either declares it as static data or allocates it from dynamic memory. This function now passes a pointer to this data to functions that it calls (and these may pass on the pointer to functions that they call).
/* in kbdmgr.c */ ------------------------------------------------------------------------ This provides more control over the data than using straight global data, but can suffer from many of the problems. When the pointer is passed around below the owner's level, it can be very unclear who is doing what to the data, especially if it is large and complex. 9.4.2 Insulate the dataThe second alternative to global data is for all operations on the data to be performed by a limited set of access routines. Other routines may still be passed a pointer to the data or, preferably, a 'handle' which only the access routines can interpret:
KbdStatus = ReadKbdStatus( hKeyboard );
9.4.3 Hide the dataThe final alternative is to completely hide the data, although this is only possible if there is one set of data. The data is now completely owned by the accessing functions:
KbdStatus = ReadKbdStatus();
|
Site Menu |
Quality: | Quality Toolbook | Tools of the Trade | Improvement Encyclopedia | Quality Articles | Being Creative | Being Persuasive | |
And: | C Style (Book) | Stories | Articles | Bookstore | My Photos | About | Contact | |
Settings: | Computer layout | Mobile layout | Small font | Medium font | Large font | Translate | |
You can buy books here |
And the big |