DOC PREVIEW
Princeton COS 217 - Scoping and Testing

This preview shows page 1-2-16-17-18-33-34 out of 34 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Scoping and TestingOverview of Today’s LectureGlobal VariablesDefinition vs. DeclarationLocal Variables and ParametersLocal Variables & ParametersSlide 7Scope ExampleScope: Another ExampleScope: AScope: BScope: CScope: DScope: EScope: Keeping it SimpleScope and Programming StyleTestingSlide 18Testing, Profiling, & InstrumentationProgram VerificationProgram TestingTest ProgramsSlide 23Test Boundary ConditionsSlide 25Test Boundary ConditionA Bit Better...Ambiguity in SpecificationMoral of This Little Story:Test As You Write CodeTest AutomationStress TestsWho Tests WhatConclusions1Scoping and TestingProfessor Jennifer RexfordCOS 2172Overview of Today’s Lecture•Scoping of variablesLocal or automatic variablesGlobal or external variablesWhere variables are visible•Testing of programsIdentifying boundary conditionsDebugging the code and retesting3Global Variables•Functions can use global variables defined outside and above themint stack[100];int main(void) { . . . stack is in scope}int sp;void push(int x) { . . . stack, sp are in scope}4Definition vs. Declaration•DefinitionWhere a variable is created and assigned storage•DeclarationWhere the nature of a variable is stated, but no storage allocated•Global variablesDefined once (e.g., “int stack[100]”)Declared where needed (e.g., “extern int stack[]”)–Only needed if the function does not appear after the definition–Convention is to define global variables at the start of the file5Local Variables and Parameters•Functions can define local variables Created upon entry to the functionDestroyed upon departure and value not retained across calls–Exception: “static” storage class (see chapter 4 of K&R)•Function parameters behave like initialized local variablesValues copied into “local variables”C is pass by value (so must use pointers to do “pass by reference”)6Local Variables & Parameters•Function parameters and local definitions“hide” outer-level definitions (gcc -Wshadow)int x, y;. . .void f(int x, int a) { int b; . . . y = x + a * b; if (. . .) { int a; . . . y = x + a * b; }}different xsame ydifferent a7Local Variables & Parameters•Cannot declare the same variable twice in one scopevoid f(int x) { int x; error! . . . }8Scope Exampleint a, b;void f(int a) { a = 3; { int b = 4; printf(“a=%d, b=%d\n”, a, b); } printf(“a=%d, b=%d\n”, a, b); b = 5;}int main (void) { a = 1; b = 2; f(a); printf(“a=%d, b=%d\n”, a, b); return 0;}9Scope: Another Example#include “interface.h”int A;int B;void f(int C) { int D; if (...) { int E; ... }}void g(...) { int H; ...}#include “interface.h”int J;void m(...) { int K; ...}void g(...) { int H; ...}extern int A;void f(int C);module1.c module2.cinterface.h10Scope: A#include “interface.h”int A;int B;void f(int C) { int D; if (...) { int E; ... }}void g(...) { int H; ...}#include “interface.h”int J;void m(...) { int K; ...}void g(...) { int H; ...}extern int A;void f(int C);module1.c module2.cinterface.h11Scope: B#include “interface.h”int A;int B;void f(int C) { int D; if (...) { int E; ... }}void g(...) { int H; ...}#include “interface.h”int J;void m(...) { int K; ...}void g(...) { int H; ...}extern int A;void f(int C);module1.c module2.cinterface.h12Scope: C#include “interface.h”int A;int B;void f(int C) { int D; if (...) { int E; ... }}void g(...) { int H; ...}#include “interface.h”int J;void m(...) { int K; ...}void g(...) { int H; ...}extern int A;void f(int C);module1.c module2.cinterface.h13Scope: D#include “interface.h”int A;int B;void f(int C) { int D; if (...) { int E; ... }}void g(...) { int H; ...}#include “interface.h”int J;void m(...) { int K; ...}void g(...) { int H; ...}extern int A;void f(int C);module1.c module2.cinterface.h14Scope: E#include “interface.h”int A;int B;void f(int C) { int D; if (...) { int E; ... }}void g(...) { int H; ...}#include “interface.h”int J;void m(...) { int K; ...}void g(...) { int H; ...}extern int A;void f(int C);module1.c module2.cinterface.h15Scope: Keeping it Simple•Avoid duplicate variable namesDon’t give a global and a local variable the same nameBut, duplicating local variables across different functions is okay–E.g., array index of i in many functions•Avoid narrow scopesAvoid defining scope within just a portion of a function–Even though this reduces the storage demands somewhat•Use narrow scopes judiciouslyAvoid re-defining same/close names in narrow scopes•Define global variables at the start of the fileMakes them visible to all functions in the fileThough, avoiding global variables whenever possible is useful16Scope and Programming Style•Avoid using same names for different purposesUse different naming conventions for globals and localsAvoid changing function argumentsBut, duplicating local variables across different functions is okay–E.g., array index of i in many functions•Define global variables at the start of the fileMakes them visible to all functions in the file•Use function parameters rather than global variablesAvoids misunderstood dependenciesEnables well-documented module interfaces •Declare variables in smallest scope possibleAllows other programmers to find declarations more easilyMinimizes dependencies between different sections of code17TestingChapter 6 of “The Practice of Programming”18"On two occasions I have been asked [by members of Parliament!], `Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question." -- Charles Babbage19Testing, Profiling, & Instrumentation•How do you know if your program is correct?Will it ever crash?Does it ever produce the wrong answer?How: testing, testing, testing, testing, …•How do you know if your program is efficient?How fast is your program?Why is it slow for one input but not for another?How much memory is it using?How: timing, profiling, and instrumentation (later in the course)20Program Verification•How do you know if your program is correct? Can you prove that it is correct?Can you prove properties of


View Full Document

Princeton COS 217 - Scoping and Testing

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Scoping and Testing
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Scoping and Testing and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Scoping and Testing 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?