행복아울렛
How to use variables in programming 본문
How to use variables in programming
PERSISTENCE
- Use debug code in your program to check critical variales for reasonable values. If the values aren't reasonable, print a warning that tells you to look for improper initialization.
- Write code that assumes data isn't persistent.
- Develope the habit of initializing all data right before it's used.
USING EACH VARIABLE FOR EXACTLY ONE PURPOSE
- Use each variable for one purpose only.
- Avoid variables with hidden meanings.
- Make sure that all declared variables are used.
COMMON PROBLEMS WITH GLOBAL DATA
- Inadvertent changes to global data.
- Bizarre and exciting aliasing problems with global data.
- Re-entrant code problems with global data.
- Coode reuse hindered by global data.
- Modularity and intellectual manageability damaged by global data.
REASON TO USE GLOBAL DATA
- Preservation of global values.
- Substitution for named constants.
- Streamlining use of extremely common data.
- Eliminating tramp data.
HOW TO REDUCE THE RISKS OF USING GLOBAL DATA.
- Begin by makin geach variable local and make variables global only as you need to.
- Develop a naming convention that makes global variables obvious.
- Create a well-annotated list of all your global variables.
- If you're using FORTRAN, use labeled COMMON only.
- Use locking to control access to global variables.
- Don't pretend you're not using global data by putting all your data into monster variable and passing it everywhere.
Advantages of acess routines
- You get centralized control over the data.
- You can ensure that all references to the variable are firewalled.
- You get the general benefits of information hiding automatically.
- Access routines are easy to convert abstract data types.
How to use access routines
- Require all rotines to go through the access routines for the data.
- Don't just throw all your global data into the same barrel.
- Build a level of abstraction into your access routines.
- Keep all accesses to the data at the same level of abstraction.
----------------------
C H E C K L I S T
----------------------
GENERAL DATA
- Do all variables have the smallest scope possible?
- Are references to variables close together?
- Do control structures correspond to the data structures?
- Does each variable have one and only one purpose?
- Is each variable's meaning explicit, with no hidden meaning?
- Are all the declared variable being used?
GLOBAL DATA
- Are all variables local unless they absolutely need to be global?
- Do variable naming conventions differentiate among local, module, and global data?
- Are all global variables documented?
- Is the code free of pseudoglobal data-big data structures containing a mishmash of data that's passed to every routine?
- Are access routines used instead of global data?
- Are access routines and data organized into modules rather than lumped together?
- Do access rotines provide a level of abstraction beyond the underlying computer-science implementations?
- Are all related acces routines at the same level of abstraction?
----------------------
K E Y P O I N T S
----------------------
- Minimize the scope of each variable. Keep references to it close together. Keep it local or modular. Try to avoid global data.
- Use each variable for one and only one purpose.
- Avoid global variables, not because they're dangerous(even though they are) but because you can replace them with something better.
- If you can't avoid global variables, work with them through access routines. Access routines give you everything that global variables give you and more.