Download presentation
Presentation is loading. Please wait.
Published byDarrell Ford Modified over 8 years ago
1
Variable Scope MacDonald Ch. 2-3 MIS 324 MIS 324 Professor Sandvig Professor Sandvig
2
Overview Variable Scope Definition Definition Principles of good design Principles of good design How to limit scope How to limit scope
3
Variable Scope Definition: Scope determines where a variable is accessible (or visible) Scope determines where a variable is accessible (or visible) Scope limited by control structures Scope limited by control structures Visible only within structure where definedVisible only within structure where defined And child structuresAnd child structures
4
Variable Scope Principles of good design Goal: minimize variable scope Goal: minimize variable scope Improves code readabilityImproves code readability Decreases risk of accidental interactions within codeDecreases risk of accidental interactions within code “The road to Hell is paved with global variables” “The road to Hell is paved with global variables” Steve McConnell Code Complete 2 nd Ed. Scope examples: Scope examples:
5
Variable Scope Scope limited to control structure & children
6
Variable Scope Scope limited to method
7
Variable Scope Variable defined outside methods have page-level scope
8
Variable Scope Server controls have page-level scope
9
Limiting Scope Lazy programmer Define all variables with page level scope Define all variables with page level scope All variables in scope all the time All variables in scope all the time No need to pass parameters into methods No need to pass parameters into methods Problem: debugging Problem: debugging Difficult to trace variables through long convoluted pathsDifficult to trace variables through long convoluted paths Good programmer Define variables as needed within control structures Define variables as needed within control structures Use and destroy Use and destroy Keep it simple Keep it simple
10
Summary Variable scope Avoid global variables Avoid global variables Minimize scope with control structures Minimize scope with control structures Code easier to debug & maintain
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.