week 5 part 2

36
Week 5 Part 2 Kyle Dewey

Upload: adolph

Post on 06-Jan-2016

23 views

Category:

Documents


0 download

DESCRIPTION

Week 5 Part 2. Kyle Dewey. Overview. Scope Lifetime Testing Exam #1 overview. What’s with the { ... } ?. Recall. Function definitions look like this:. void foo() { ... }. Conditionals ( if ) look like this:. if ( condition ) { ... }. while loops look like this:. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Week 5 Part 2

Week 5 Part 2Kyle Dewey

Page 2: Week 5 Part 2

Overview

•Scope

•Lifetime

•Testing

•Exam #1 overview

Page 3: Week 5 Part 2

What’s with the { ... }?

Page 4: Week 5 Part 2

Recall•Function definitions look like this:

void foo() { ... }

•Conditionals (if) look like this:

if ( condition ) { ... }

•while loops look like this:

while ( condition ) { ... }

Page 5: Week 5 Part 2

Brackets

•The { ... } part is significant

•This is called a block

•Blocks have special meaning to C (and to the vast majority of languages)

Page 6: Week 5 Part 2

Blocks•As we’ve already seen, blocks can

be nested:

void foo() { int x; for ( x = 0; x < 10; x++ ) { if ( x % 2 == 0 ) { printf( “Even: %i\n”, x ); continue; } printf( “Odd: %i\n”, x ); }}

Page 7: Week 5 Part 2

Blocks

•Importance of this lies in variable declaration

•A block nested at level N has access to variables defined at nesting levels 0 .. N - 1, but not the other way around

Page 8: Week 5 Part 2

Example

void foo() { int x = 10; if ( x > 5 ) { int y = x * 4; // this block can access x } // ...but this block can’t access y}

Page 9: Week 5 Part 2

So what?

•This may seem obvious and/or insignificant

•This mechanism means that you don’t have to worry about what was defined in inner blocks, because they are inaccessible anyway

Page 10: Week 5 Part 2

Variable Name Reusage

•Blocks help to prevent variable names from clashing

•A variable foo defined in a given block is distinct from all other variables named foo defined in other blocks

Page 11: Week 5 Part 2

Example

int x = ...;

if ( x < 10 ) { int y = 20;} else { int y = 30;}

Distinct variables

Page 12: Week 5 Part 2

Variable Name Reusage

•Consider the following code:

int x = ...;

if ( x < 10 ) { int x = 20;} else { int x = 30;}

Name (x) Reused

Page 13: Week 5 Part 2

Variable Name Reusage•The original variable x does not

change

•The old definition is shadowed by the new one, not overwritten

int x = ...;

if ( x < 10 ) { int x = 20;} else { int x = 30;}

Page 14: Week 5 Part 2

Question

•What does this code print?

int x = 10;if ( x == 10 ) { int x = 5; printf( “%i\n”, x );}printf( “%i\n”, x );

Page 15: Week 5 Part 2

Block Advantage

•Focus only on one block at a time, not on previous blocks

•Variables defined in previous blocks are shadowed

Page 16: Week 5 Part 2

Scope•Scope defines which variables can

be accessed at any given point in the code

•Blocks manipulate the scope

if ( 1 < 2 ) { int x = 5; // x is now in scope}// x is no longer in scope

Page 17: Week 5 Part 2

Scope Exampleint x = 10;// x is now in scope

if ( 1 < 2 ) { int x = 5; // x is in scope, but it // refers to the x = 5 definition}

// x is in scope, but it refers to// the x = 10 definition

Page 18: Week 5 Part 2

Lifetime

•How long a variable exists in your program is the variable’s lifetime

•Scope is not the same as lifetime

•Scope: when you can access a variable

•Lifetime: whether or not a variable is there

Page 19: Week 5 Part 2

Scope vs. Lifetime

•A variable in scope is necessarily alive

•A variable that’s alive is not necessarily in scope

Page 20: Week 5 Part 2

Example #1

int x = ...; // alive and in scopeif ( x < 10 ) { int y = 5; // alive and in scope ...}// y is not in scope and not alive

Page 21: Week 5 Part 2

Example #2int x = ...; // alive and in scopeif ( x < 10 ) { int y = 5; // x and y are alive

// and in scope if ( x < y + 5 ) { int z = 20; // x, y, z alive and in scope } // x, y alive and in scope}// x alive and in scope

Page 22: Week 5 Part 2

Example #3int x = ...; // alive and in scopeif ( x < 10 ) { int x = 5; // x = ... is alive

// but not in scope // x = 5 alive in scope

if ( x < y + 5 ) { int x = 20; // x = ... and x = 5 alive // only x = 20 is in scope } // x = ... and x = 5 alive // only x = 5 in scope}// x = ... alive and in scope

Page 23: Week 5 Part 2

Example #4void bar() { // y is alive but not in scope int z = 5;}

void foo() { int y = 10; bar();}

void main() { foo();}

Page 24: Week 5 Part 2

Global Variables•Consider the following code:

int x = 10;

void foobar() { printf( “%i\n”, x );}

void barfoo() { x++;}

Page 25: Week 5 Part 2

Global Variables•x is a global variable

•Always in scope (unless shadowed)

•Always aliveint x = 10;

void foobar() { printf( “%i\n”, x );}

void barfoo() { x++;}

Page 26: Week 5 Part 2

Thought Question

•Global variables are seen as bad practice, and are usually avoided

•Why?

Page 27: Week 5 Part 2

Answer

•Always in scope and always alive means everything in the file probably heavily relies on it

•Another variable to keep track of for everything in the file

•Can be error prone

•Interdependent code

Page 28: Week 5 Part 2

Aside: “In the File”

•Technically a “compilation unit”

•In this class, a file is a compilation unit

•However, it’s possible to have multiple files in the same compilation unit

Page 29: Week 5 Part 2

Testing

Page 30: Week 5 Part 2

Recall...

•Testing is an important step in software development

•Builds confidence that code works correctly

•Modern software development heavily relies on testing

Page 31: Week 5 Part 2

Testing

•Testing can confirm a bug exists

•...but it cannot confirm that bugs do not exist

•May not be testing for it

•May need additional tests

Page 32: Week 5 Part 2

Testing Weaknessint badMax( int x, int y ) { if ( x == 513 ) { return x; } else if ( x > y ) { return x; } else { return y; }}

Page 33: Week 5 Part 2

Testing Strength

•Code is not usually written like that

•The goal is not to mess up the tests

•Simple (compared to verification, which attempts to prove that there are no bugs)

Page 34: Week 5 Part 2

Additional Terminology

•White box testing: you can see the whole code, as with:

// get the max of x and yint max( int x, int y ) { if ( x > y ) { return x; } else { return y; }}

Page 35: Week 5 Part 2

Additional Terminology

•Black box testing: you can see only the interfaces and what they do, as with:

// get the max of x and yint max( int x, int y );

Page 36: Week 5 Part 2

Exam #1 Overview