Subscribe to my RSS feed
|
|
Best Practices for Variables
- Declare and define each variable close to where it''s first used
- This applies especially to code setup needed right before a loop
- Localize References to Variables
- keep subsequent variable references close together (the number of lines between references is called span)
- minimize the average span
- Keep "live" time as short as possible
- "live" time is the number of lines between a variables first reference and its last reference
- A variable could have short span, but a long live time
- Goal is to minimize both span and live time
- Don''t assign a value to a variable until just before the value is used
- Always start with the most restrictive visibility, and expand only as necessary
- Favor the smallest possible scope for a variable: local to a loop, local to a routine, private to a class, protected to a class, package to a class
(Code Complete, Chapter 10)| Comments | Currently, there are no comments. Be the first to post one! Click here to post a comment |
|