Where to declare local variables: at beginning or at first use?
Local variables can be declared anywhere before they are used. There are two styles of declaration. • Declaring variables when they are first used. This style has several advantages. • It guarantees that variables are initialized. • No unused variables are declared. • It’s clear what the type is at the time it is used. • The distance between uses of a variable should be as small as possible to make it easier on the human reader. Comments sometimes don’t fit on the remainder of the line so they must be put on the line before if required, but good variable names often mean that comments are not necessary. • [an error occurred while processing this directive] At the front. This style leaves space on the remainder of the line to add a comment. It also lists the cast of characters at the front, which some programmers find helpful. Clarity first. Use whichever style makes the program clearer for you, or whichever style you instructor requires. I’m flexible, but you will find most of my progr