10.2. Making Variable Declarations Easy

 < Free Open Study > 

This section describes what you can do to streamline the task of declaring variables. To be sure, this is a small task, and you might think it's too small to deserve its own section in this book. Nevertheless, you spend a lot of time creating variables, and developing the right habits can save time and frustration over the life of a project.

Cross-Reference

For details on layout of variable declarations, see "Laying Out Data Declarations" in Section 31.5. For details on documenting them, see "Commenting Data Declarations" in Section 32.5.


Implicit Declarations

Some languages have implicit variable declarations. For example, if you use a variable in Microsoft Visual Basic without declaring it, the compiler declares it for you automatically (depending on your compiler settings).

Implicit declaration is one of the most hazardous features available in any language. If you program in Visual Basic, you know how frustrating it is to try to figure out why acctNo doesn't have the right value and then notice that acctNum is the variable that's reinitialized to 0. This kind of mistake is an easy one to make if your language doesn't require you to declare variables.

If you're programming in a language that requires you to declare variables, you have to make two mistakes before your program will bite you. First you have to put both acctNum and acctNo into the body of the routine. Then you have to declare both variables in the routine. This is a harder mistake to make, and it virtually eliminates the synonymous-variables problem. Languages that require you to declare data explicitly are, in essence, requiring you to use data more carefully, which is one of their primary advantages. What do you do if you program in a language with implicit declarations? Here are some suggestions:


Turn off implicit declarations Some compilers allow you to disable implicit declarations. For example, in Visual Basic you would use an Option Explicit statement, which forces you to declare all variables before you use them.

Declare all variables As you type in a new variable, declare it, even though the compiler doesn't require you to. This won't catch all the errors, but it will catch some of them.

Use naming conventions Establish a naming convention for common suffixes such as Option Explicit and No so that you don't use two variables when you mean to use one.

Cross-Reference

For details on the standardization of abbreviations, see "General Abbreviation Guidelines" in Section 11.6.


Check variable names Use the cross-reference list generated by your compiler or another utility program. Many compilers list all the variables in a routine, allowing you to spot both acctNum and acctNo. They also point out variables that you've declared and not used.

 < Free Open Study > 


Code Complete
Code Complete: A Practical Handbook of Software Construction, Second Edition
ISBN: 0735619670
EAN: 2147483647
Year: 2003
Pages: 334

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net