< Free Open Study > |
10.2. Making Variable Declarations EasyThis 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 DeclarationsSome 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.
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 > |
No comments:
Post a Comment