Variable Scope MacDonald Ch. 2-3 MIS 324 MIS 324 Professor Sandvig Professor Sandvig.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Scope and Lifespan of Identifiers. Every function has a scope What does that mean? That means that every identifier that is created in a function (that’s.
Coding Practices. What are Coding Practices ? Coding practices are a set of informal rules that the software development community has learned over time.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez.
Stored Procedures & User Defined Functions MacDonald Ch. 23 MIS 424 MIS 424 Professor Sandvig Professor Sandvig.
Navigation Controls MacDonald Ch. 11 MIS 424 MIS 424 Professor Sandvig Professor Sandvig.
ASP.NET Programming with C# and SQL Server First Edition
Business Object Payne Ch. 15 MIS 424 MIS 424 Professor Sandvig Professor Sandvig.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Chapter 6: Functions.
Web Form Fundamentals MacDonald Ch. 5 MIS 324 MIS 324 Professor Sandvig Professor Sandvig.
.NET Validation Controls MacDonald Ch. 8 MIS 324 MIS 324 Professor Sandvig Professor Sandvig.
Tracing, Logging, and Error Handling MacDonald Ch. 8 MIS 424 MIS 424 Professor Sandvig Professor Sandvig.
Comp 245 Data Structures Software Engineering. What is Software Engineering? Most students obtain the problem and immediately start coding the solution.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.
Web Server Controls MacDonald Ch. 5 MIS 324 MIS 324 Professor Sandvig Professor Sandvig.
Overview of Data Access MacDonald Ch. 15 MIS 324 Professor Sandvig.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
1 Data Structures CSCI 132, Spring 2014 Lecture 3 Programming Principles and Life Read Ch. 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
MIS 424 Professor Sandvig. Overview  Why Analytics?  Two major approaches:  Server logs  Google Analytics.
SE: CHAPTER 7 Writing The Program
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Subprograms1 THE CALL STATEMENT (chp. 16) Purpose: a calling that executes another program; allows portions of code to be treated as a “black box”. Syntax.
Cohesion and Coupling CS 4311
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Guidelines for a Language- Independent Convention Identify global variables –Use g_prefix –Exp: g_RunningTotal Identify module variables –Use m_prefix.
Jump to first page (C) 1998, Arun Lakhotia 1 Design Quality Metrics Arun Lakhotia University of Southwestern Louisiana Po Box Lafayette, LA 70504,
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 7 Implementation. Implementation Approaches F Big bang –Code entire system and test in an unstructured manner F Top-down –Start by implementing.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
MTA EXAM HTML5 Application Development Fundamentals.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Abstraction and Functions Professor Robin Burke. 2 Outline Quiz JavaScript review Abstraction Function Definitions purpose syntax Functions return value.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
MIS Professor Sandvig MIS 324 Professor Sandvig
Functions Review.
Variable Scope Variable, variable, who’s got the variable?
Function There are two types of Function User Defined Function
Using local variable without initialization is an error.
MIS Professor Sandvig MIS 324 Professor Sandvig
Starting Out with Programming Logic & Design
Chapter 3 Simple Functions
C# Object Oriented Programming Concepts
Subroutines and Functions
Lesson Objectives Aims Key Words
MIS Professor Sandvig MIS 324 Professor Sandvig
Programming Logic and Design Fourth Edition, Comprehensive
CSE 403, Winter 2003 Software Engineering
Chapter 9: Value-Returning Functions
Scope and State Handling in Java Server Pages
A function is a group of statements that exist within a program for the purpose of performing a specific task. We can use functions to divide and conquer.
Starting Out with Programming Logic & Design
Leveraging ArcGIS Online Elevation and Hydrology Services
Predefined Functions Revisited
Software Development Chapter 1.
Five Guidelines for Robust Reusable Code
Chapter 6 Modular Programming chap6.
Scope Rules.
Presentation transcript:

Variable Scope MacDonald Ch. 2-3 MIS 324 MIS 324 Professor Sandvig Professor Sandvig

Overview  Variable Scope Definition Definition Principles of good design Principles of good design How to limit scope How to limit scope

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

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:

Variable Scope  Scope limited to control structure & children

Variable Scope  Scope limited to method

Variable Scope  Variable defined outside methods have page-level scope

Variable Scope  Server controls have page-level scope

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

Summary  Variable scope Avoid global variables Avoid global variables Minimize scope with control structures Minimize scope with control structures  Code easier to debug & maintain