When declaring variables/constants, programmer must be aware of scope.
- Local variables are declared and used inside a module/subroutine.
- Global variables are declared at beginning of the code; available throughout.
Using local variables as far as possible - good programming style.
Some programming languages will initialise variables (when they are declared).
- This basically means that the variables are given a starting value.
Generally integers are set to 0, Booleans are set to FALSE and strings are empty.
Constants can make code easier to read and maintain.
- Many programs contain values that don't change whilst the program is running
- These may be universal constants that are always the same.
- New programmers tend to use literals in their code for such values.
- This means that the code contains the actual values spelt out in full.
- As a general rule literals SHOULD BE AVOIDED in code.
- They should be SUBSTITUTED WITH CONSTANTS unless there is a good reason not to.
Comments
No comments have yet been made